File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with ++ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with ++ operator" Watch "Doubt with ++ operator" New topic
Author

Doubt with ++ operator

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Source Majji:Q15
What will happen when you invoke the following method?

1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }

A) The loop never ends(infiniteLoop).
B) Prints "Welcome to Java".
C) Compilation error at line 5. ++ operator should not be used for byte type variables.
D) Prints nothing.

Ans: B

Doubt: at certain point b's value will exceed the range of byte, so should it not give compilation error.

regards,
gitesh
Thiru Mu
Greenhorn

Joined: Aug 30, 2007
Posts: 23
Yes you are right,
at a point, b's value will be 127 and when it still do ++b it will become
-128, which is less than 0, so the wilhe loop quits and prints the text.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Why it returns "Welcome to Java" only one time. How can we know the println statement is not a body of the while loop. It is written in book, "if there is only one line in loop body you are free to don't put that line in {}. When I did this program I thought it prints "Welcome to Java" 127 times.
Now I am little confused. Can anybody help me?


SCJP-1.5<br />SCWCD-1.4
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Originally posted by dolly shah:
Why it returns "Welcome to Java" only one time. How can we know the println statement is not a body of the while loop. It is written in book, "if there is only one line in loop body you are free to don't put that line in {}. When I did this program I thought it prints "Welcome to Java" 127 times.
Now I am little confused. Can anybody help me?


Check out line number 6.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
There is exactly one line in the body of the while loop; it's line 6. You may wish to read up on the JLS 14.6: empty statement.


Ping & DNS - my free Android networking tools app
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
What is that? The ( is by mistake place there. But my question is different.
Lucy Smith
Greenhorn

Joined: Aug 07, 2007
Posts: 17
Isn't your println statment outside the while loop though? Because you don't have these {} after while loop, the first empty stament is the only one that's in loop.

Try removing line 6 and then you will see "Welcome to Java" printing 127 times. And then add "Welcome to Java - 2" after that so you will see this printing only once after printing the first one 127 times. Then try adding {}, you will see all the difference.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, well it is really important if the ; is there or not:



prints:
Oachkatzerlschwoaf
squirrel tail #2
squirrel tail #3
.....
squirrel tail #125
squirrel tail #126
squirrel tail #127
main ready




After #127 the b is -128 and the loop is left.
Oachkatzerlschwoaf is the bavarian word for squirrel tail.
Now you also see what this code tags are good for.



Yours,
Bu.


all events occur in real time
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


It still gives me "Welcome to Java" only once.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Originally posted by dolly shah:


It still gives me "Welcome to Java" only once.


Hey dolly!
It doesnt matter whether you put semicolon( ; ) on the top (with while)or new line, what matters is whether you are putting it or not...


and


both are same... please read again what Ulf has said.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383



That is the original code. Here ( plays a main role. Now I got it.
Thanks Hassel ,Ulf & Akhilesh.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Hats Off to everybody for Help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with ++ operator