views:

73

answers:

3

I am looking at quizzes and tests at various sites (like blackbeltfactory, etc..) about java. I come across with questions which have choices like "doesn't compile" or "throws exception at runtime".

Are there any way to guess which will occur, at first look? Or is it a matter of getting familiar with java? I think this is an important point on how java works.

Thanks in advance, Bugra

+4  A: 

For a human, you have to "get familiar with Java".

For a machine ( or a program that is ) it has to follow the rules the language specifies.

For instance the given class:

class Main  {
     String s;
}

What would be the result of invoking:

Main m = new Main();
m.s.length();

A) doesn't compile?

B) throws exception at runtime?

To answer this specific question you have to know, how classes are defined in Java ( to know if the one I show is a valid class definition or not ) , also you have to know how attributes are defined and default values etc, so you have to get familiar with the language.

When you know all these concepts, you can easily answer with a quick view.

BTW, the famous Sun Certified Java Programmer certification, is all about know this kind of stuff, rather than knowing how to develop an application. It is about converting your self in a "human compiler"

OscarRyz
The thing is i am a physics student who wants to learn java (and software development process) with some experience about programming, and,I always made a progress until a certain point, then i stuck. Now i can say those experiences are helping me a lot with Java. I am telling this to you looking at your last two sentences.I want to have a career in Java and my science-student-part wants to think like a 'compiler', on the other hand, i want to learn development process and java-specific techniques. A need some advice and tips.Also please check, http://bit.ly/9uXPgQThanks,Bugra
bgo
That's the good part of programming languages, they have this formal part which we like about things. I mean the part where everything is either working or not, 0 or 1, they have formal definitions. But also, software development offers huge possibilities for creativity, almost as much as music or literature I would say.
OscarRyz
A: 

In my honest opinion is a matter of getting familiar with java but some compiling errors are clear at first sight and they are very similar to other languages.

Dario
+1  A: 

Obviously, knowing Java better will help. However, there are some general rules.

"Doesn't compile" means that the compiler literally could not understand the syntax of the code. This can happen as the result of misplaced or missing brackets or parentheses, methods with the wrong number of arguments, and other such things:

int a = (3 + 2) - 1);

"Throws exception at runtime" means that the written code makes sense syntactically, but that when it is actually executed it tells the machine to do something that is, for whatever reason, impossible. some exceptions are built into the language, such as if you try to divide by zero, but many are also explicitly defined and thrown in the code. Programmers use such exceptions to create programs which visibly break when they try to do something they shouldn't, rather than breaking silently and then causing other problems down the road:

int a = b / c; // very bad if c == 0

Generally speaking, compiler errors will look more like typos (and often will result from typos), whereas runtime exceptions will result from lines which fail under specific conditions (e.g. some variable is equal to zero). Once again, though, there is no real substitute for truly knowing the language.

tlayton
*'"Doesn't compile" means that the compiler literally could not understand the syntax of the code.'* - that is wrong on a number of levels.
Stephen C