Java FAQ
by
, April 1st, 2011 at 08:53 AM (12950 Views)
Here are some foolishly answered questions about Java and Java programming:
1. I'm having problems compiling my Java code! I've tried the command-line compiler and the Eclipse JDT compiler, but neither of these gives me the correct results.
Answer:Originally Posted by Javac
The javac compiler does not require you to put the extension in. So the correct command should be:
Additionally, try renaming your file from helloworld.jpg to helloworld.java. Java source files must have the .java extension.javac helloworld
For more answers which may help you with this problem (in particular, what to do if you have a png file instead of a jpeg), see Stack Overflow - Why is this program erroneously rejected (it's for C++, but everyone knows that C/C++ solutions universally apply to everything).
2. I've heard about this thing called JNI. What is it and when should I use it?
Answer:
JNI stands for Java Native Interface. The developers of Java new how bad they were, so decided that the best course of action after a failed attempt at SE1.0 decided was to create JNI to allow programmers to use other languages to code their Java program.
JNI is generally the preferred course of action when it comes to developing a Java application because it has none of the problems associated with running an interpreted language. Future releases of Java have only focused improving the JNI interface.
An alternative to JNI was introduced in SE6, known as OpenJDK. OpenJDK lets developers directly compile their code into the JVM, thus eliminating any associated interface lag between Java and JNI. This method is still under heavy development, though, and it is quite difficult compared to using JNI.
3. People keep telling me that Javascript and Java are different! Are they really?
Of course they're not different! If two things were different, they would have very different names. For example, no one would ever confuse a sandwich with spaghetti because their names are no where near the same. Java is the actual programming language. Javascript is just the scripting version of Java, and can be compiled into Java programs or run directly.
4. What are good practices when it comes to naming Java class/variables?
Always pick the shortest names possible. Each additional character you add introduces another chance that you could type the wrong character. If a variable is named a, there is very little chance you'll type it's name wrong. However, if your variable is named computedLength, there are tons of ways you could misspell it, leading to massive headaches during debugging.
5. If naming java variables as short as possible is desired, why doesn't the Java API do this?
As stated in answer 2, the Java developers are morons. Just because someone is wrong doesn't make it ok to follow them.
6. Should I use comments? From what I hear, they're not even compiled into JavaByte code and don't affect performance at all.
If something has no positive impact on performance, don't use it.
7. What's a good formatting scheme? 1 spaces between item, and then tab each line in with an extra tab after the start of a new block?
Don't. Only when the said formatting has a positive impact on performance. Anything else is just wasted keystrokes.
Ex.:
This code is the best way to program Java
8. Is there anything else I should keep in mind before starting to program Java?
Yes, ignore everything posted in this faq. Like the Java developers, I too am a moron. My goal one day is to become one of them.
Happy April Fools day (and for those who's calendar says it's April 2, you've set it to the incorrect date. Everyone knows that US MST is the only timezone that should be used).