|
Newf posted:Well I've done that and it works, but I'm completely confused. Isn't the "thing" that you put in theses <> doodads the 'type' of object that the arraylist will store/retrieve? There's a difference between the primitive type double and the Object type Double. These object types exist for all of the primitives, as you must use an object type in generics and not primitives. Java will "auto-box" these for you so that you can treat them essentially the same. You should probably look that up for some more information on how they differ.
|
# ? Jul 22, 2011 03:34 |
|
|
# ? Jun 2, 2024 21:39 |
|
Jeez, be more helpful guys. Thanks both.
|
# ? Jul 22, 2011 03:36 |
|
brosmike posted:
Boxing uses valueOf or equivalent. Keep in mind that boxing/unboxing does take time. So adding thousands of ints to an Integer list will be slower than using arrays.
|
# ? Jul 22, 2011 05:13 |
|
The past page or two has made me remember how much I wish I was developing with Python right now.
|
# ? Jul 22, 2011 05:15 |
|
Aleksei Vasiliev posted:use Integer.valueOf(1) or autoboxing, don't ever use new for Integer, Boolean, etc. Whoops, my bad - this is absolutely correct, listen to this guy.
|
# ? Jul 22, 2011 05:30 |
|
The fun part of autoboxing is that it caches "frequently-used" values, which lets you do something like this:code:
|
# ? Jul 22, 2011 08:53 |
|
Blacknose posted:I need a good Spring MVC book. Any ideas? This got skipped over, but I recommend: http://www.amazon.com/Spring-Action-Craig-Walls/dp/1935182358/ref=sr_1_1?s=books&ie=UTF8&qid=1311350259&sr=1-1 I have the second ed. The third just dropped last month I think.
|
# ? Jul 22, 2011 16:58 |
|
I was laying on the floor just now thinking about the line of text that I'd been instructed to enter at the beginning of every program: public static void main(String[] args) THIS IS BECAUSE the main method (is the method that runs without getting called?) (potentially) takes any number of string arguments, which are in fact (this is my guess) command line arguments?
|
# ? Jul 22, 2011 22:45 |
|
Newf: You are correct. If you launch your program 'Foo' like:code:
code:
This may have helped, or it may have made you more confused.
|
# ? Jul 22, 2011 22:53 |
|
Internet Janitor posted:This may have helped, or it may have made you more confused. A little of both. Thanks very much
|
# ? Jul 23, 2011 00:22 |
|
OK, so more trouble from the resident java noob. I'm trying to make a program that uses scanner to take a positive integer as keyboard input, but instead of crashing at faulty input (like a string, for example) it asks for the input again. When I run the following: code:
code:
code:
|
# ? Jul 24, 2011 18:21 |
|
Newf posted:OK, so more trouble from the resident java noob. Have you got an import for java.util.InputMismatchException ? It's not in the java.lang package
|
# ? Jul 24, 2011 18:24 |
|
import java.util.InputMismatchException;
|
# ? Jul 24, 2011 18:25 |
|
I certainly haven't, but wouldn't it be covered by importing java.util.Scanner? How else would the first example program there have thrown the error? e: well this seems unanimous, and thank yous, it worked, but my-oh-my I have no idea how java works at all.
|
# ? Jul 24, 2011 18:25 |
|
Scanner has its own imports in its source. Importing a class that imports something else doesn't import its imports.
|
# ? Jul 24, 2011 18:35 |
|
Import statements are not necessary to use a class- they are what allow you to use a short name when referring to classes rather than the fully qualified name. Your previous program would've compiled fine if you'd used the fully qualified name java.util.InputMismatchException instead of just InputMismatchException.
|
# ? Jul 24, 2011 18:36 |
|
Ok, so I've gotten myself really confused over how dependencies work in Java - am I correct in thinking that "import org.apache.xxx" gives you absolutely no clue which .jar file actually contains the class you want? I have 15-20 .jar files, is there any way to search through which ones contain which classes easily? Also, I have a project in eclipse that was made when nested packages were a thing, which they're now not. I am completely lost on how to make the dependencies work - there are 15-20 packages inside a single Java project in eclipse and they all depend on each other - how do I satisfy the imports? At the moment, they can't see each other. One last thing - does eclipse have a feature where it'll automatically look inside say /usr/share/java for dependencies so that you don't have to add the external jars manually? I've been banging my head on this problem for a while now and I do so hate outdated documentation. Is a possible workaround to go back to an older version of eclipse that does have this nested modules feature?
|
# ? Jul 24, 2011 18:44 |
|
AlsoD posted:Ok, so I've gotten myself really confused over how dependencies work in Java - am I correct in thinking that "import org.apache.xxx" gives you absolutely no clue which .jar file actually contains the class you want? I have 15-20 .jar files, is there any way to search through which ones contain which classes easily? Other than package itself having a name similar to the name of JAR file itself, no, it doesn't give you any kind of clue. As long as the JAR is included in the classpath, the class will normally be found by gthe JVM, regardless of which JAR it physically resides in (so in theory you could pack everything in one big JAR file and it will still work--I am NOT recommending this of course). I am not aware that Eclipse has any functionality to search arbitrary JAR files for classes--however, if the jar files are included as build path of one of the projects in the workspace, the Ctrl+Shift+T should be able to find where a particular class is based on its name. There are also tools which can search a whole directory of JARs for a particular package/class, such as this one.
|
# ? Jul 24, 2011 19:04 |
|
I don't think I know how to use BufferedReader.code:
|
# ? Jul 24, 2011 23:13 |
|
Newf posted:I don't think I know how to use BufferedReader. First of all, I think that calling the method recursively in case of exception is probably not the best idea. Second, when you say 'not work', what do you mean? This code will for sure not work for any file names that contain spaces since Scanner.next() will normally give you part of input until the next whitespace character every time it's called, until it consumes the entire input. You can use input.nextLine() to get the whole line of input (not including the line separator character).
|
# ? Jul 24, 2011 23:42 |
|
Newf posted:I don't think I know how to use BufferedReader. I'd do something like this for the body of the method: code:
edit: Although I don't immediately see any reason why your original method won't work. What's the error? ShardPhoenix fucked around with this message at 07:37 on Jul 25, 2011 |
# ? Jul 25, 2011 07:32 |
|
I eventually got my original code to work. The problem was that in my main method header I didn't have "throws IOException". I have no idea what that does or why it needs to be there. Why would it be a bad idea for a method like that to call itself recursively? It just works until it's been given a legitimate input file.
|
# ? Jul 25, 2011 17:52 |
|
Newf posted:I eventually got my original code to work. The problem was that in my main method header I didn't have "throws IOException". I have no idea what that does or why it needs to be there. You probably know what an exception is, since you used a try/catch block earlier. When you throw an exception, it will send that exception to whatever called that function. For example, inside the BufferedReader class, if it runs into an Input/Output error, it will throw an IOException. This passes that exception up to your function, and now you need to deal with it. Now, when you call a function that can potentially throw an IOException, you have to either put that call inside a try/catch or throw it again yourself. By throwing the error, you are essentially passing it up the line. If you throw the exception while in your main() function, the system will catch the error, kill the program, and print an error message. Recursively functions are bad in your specific case. Imagine that the function failed, your catch block will try again. There is no reason to expect it to work the second time, since nothing has really changed. It will fail again, and call that function again ad infinitum. Eventually it'll crash from out-of-memory or something. e: Misread the recursive bit. It'll technically work the way you wrote, but it's better to use a while-loop or something, instead of recursively calling the function. zeekner fucked around with this message at 18:10 on Jul 25, 2011 |
# ? Jul 25, 2011 18:06 |
|
The "throws" clause tells methods calling that method that somewhere in your method, an exception of that type may be thrown. Those methods should either catch that exception of throw it themselves so another method would have to handle it. It's a bad idea to use recursion when you don't have to since it pollutes the stack with method calls needlessly. If the user enters a wrong file enough times, it will crash the JVM. On Java this isn't so bad, but in other programming languages this is a serious security flaw.
|
# ? Jul 25, 2011 18:08 |
|
This also shows how Java is kind of a pain for beginners, because the basic input/output stuff that beginners want to do is way too crufty.
|
# ? Jul 26, 2011 01:58 |
|
When I was taught Java, the really basic stuff was taught using a package called Console that took care of the really basic IO and graphics stuff. Later on, they taught us Swing with more graphical ways to get input, and then Scanner/BufferedReader/FileWriter/etc.
|
# ? Jul 26, 2011 04:16 |
|
ShardPhoenix posted:This also shows how Java is kind of a pain for beginners, because the basic input/output stuff that beginners want to do is way too crufty. I would totally recommend Python over Java to any beginners. A good intro to programming in Python should give them a soft introduction to exceptions so that by the time they get to the mandatory exception handling in Java it is merely a verbose PITA instead of inscrutable voodoo.
|
# ? Jul 26, 2011 15:49 |
|
Just show them how to convert everything into a Runtime exception .
|
# ? Jul 26, 2011 15:52 |
|
MEAT TREAT posted:Just show them how to convert everything into a Runtime exception . Why bother when you can just add throws Exception everywhere?
|
# ? Jul 26, 2011 22:39 |
|
Why bother throwing anything? Just suppress it! If the stack trace is never printed, that means it never happened!
|
# ? Jul 26, 2011 23:53 |
|
Ensign Expendable posted:Why bother throwing anything? Just suppress it! If the stack trace is never printed, that means it never happened! Sounds like you are an enterprise architect.
|
# ? Jul 27, 2011 00:03 |
|
So when making games, you usually need some sort of 3d math library. You can define this like so:code:
code:
The best way is to not use objects at all, but with the lack of multiple return types, or pass by references, you cannot make an easy to use library, and end up having to inline everything, so the code becomes: code:
How the F can you make something like this fast and usable?
|
# ? Jul 27, 2011 20:58 |
|
emonkey posted:How the F can you make something like this fast and usable? JNI to a native 3D library.
|
# ? Jul 27, 2011 21:01 |
|
Use fixed-point representations of vector components packed into longs, allowing you to store and pass vectors as single primitive elements! edit: But seriously, this seems like a justification for a mutable vector type. Immutable datatypes are nice for a lot of things, but they result in more GC pressure. It's a tradeoff, and your use case seems to indicate mutability is a better deal. Internet Janitor fucked around with this message at 22:24 on Jul 27, 2011 |
# ? Jul 27, 2011 21:22 |
|
baquerd posted:JNI to a native 3D library. JNI has a significant performance hit associated with it as well iirc.
|
# ? Jul 27, 2011 21:48 |
|
Internet Janitor posted:Use fixed-point representations of vector components packed into longs, allowing you to store and pass vectors as single primitive elements! Well even with mutable vector types you just reduce the number of vectors being allocated, not remove them entirely. You end up having to create static temporary vectors just for a function to use, and god forbid you call anything recursively.
|
# ? Jul 28, 2011 03:21 |
|
Add a preprocessor to your compilation chain and use macros.
|
# ? Jul 28, 2011 03:35 |
|
I'm probably going to regret posting this, but the more I think about packing stuff into longs the more I think it might not be that bad:code:
|
# ? Jul 28, 2011 04:25 |
|
MEAT TREAT posted:Just show them how to convert everything into a Runtime exception .
|
# ? Jul 28, 2011 13:47 |
|
|
# ? Jun 2, 2024 21:39 |
|
Java 7 was just released. So how long until Eclipse supports it? e: And is there a good list of what's new in 7? Malloc Voidstar fucked around with this message at 16:57 on Jul 28, 2011 |
# ? Jul 28, 2011 16:54 |