aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes the way of java code parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "the way of java code parsing" Watch "the way of java code parsing" New topic
Author

the way of java code parsing

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
public class Question21 {
public static void main(String[] args) {
int i=3;
System.out.println(getBoolean()? i=2*i++:i+++ ++i);//line 4
}
//Heads or tail?
public static boolean getBoolean(){
if((int)(Math.random()*2)==0)
return false;
else
return true;
}
}
Prints randomly 6 or 8 at each execution.
No problem here, the code compiles fine. One thing to be aware of is that i+++ ++i compiles fine because of the way Java code is actually parsed. The parser tokenizes the source in bunches of longest valid character sequences. i+++ ++i will be tokenized as i,++,+,++,i and interpreted as i++ + ++i, that is i post-incremented plus i pre-incremented.
my question is that if I use"i+++++i" or "i++ +++i" in line 4, there is compilation error, but if I use"i++ + ++i" or the above one ,there is no error, why?
The problem is that I still dont understand the above explanation. Could anyone give me a clearer explanation?
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Here's my take on this:
The parser tokenizes the source in bunches of longest valid character sequences.

i+++++i : (i)(++)(++)(+)(i)
i++ +++i : (i)(++) (++)(+)(i)
i+++ ++i : (i)(++)(+) (++)(i)
The first two contains a unary operator ++ that isn't pre/post-fixed to a variable.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: the way of java code parsing