wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i+++ ++i 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 "i+++ ++i" Watch "i+++ ++i" New topic

i+++ ++i

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(){
return false;
return true;
Prints randomly 6 or 8 at each execution.The explanation as follows:
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.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
let's see the order of compiler parsing.
1) i+++++i
(i)(++) (++) (+) (i)
this means i (++) (++) i. But here we are missing the operator(+ or - etc.) in between i++ and ++i.
2) i++ +++i
(i) (++) (++) (+) i
The same case like the above
3) i+++ ++i
(i) (++) (+) (++) i
Here we have the operator + in between i++ and ++i. So this compiles fine.
So, basically the compiler looks for longer tokens, means if there is +++ means compiler interprets (++) (+) not (+) (++) and if finds a space in between it will cut off the operator there itself like inperpreting this +++ +++ as (++) (+) (++) (+).
Hope this helps

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93

If I use i+++++i or i++ ++i then it gives a compiler error,but when I use i++ + ++i,then it compile fine

This is quite simple.
This will surely result in a compile time error because +++++ is not an operator!
(Dont forget the basics,the there must be a binary operators between the two variable and +++++ is just not a operator)
2. i++ +++i
This will result in a compile time error because +++ is also not a operator.Java has two increment/decrement operators,++ and -- and there is not operator as +++.
3.i++ + ++i
'+' is a binary operator that works on two numbers.i++ and ++i will both produce valid integer values,which will be added by + operator,so there is no chance of a compiler error!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: i+++ ++i
It's not a secret anymore!