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 System.out.prinln ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "System.out.prinln ..." Watch "System.out.prinln ..." New topic
Author

System.out.prinln ...

sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Hi,
I got an interesting Q for you ...
int y = 4;
System.out.println("Value is "+((y >4 )? 99.99:9));
this will print 9.0.
whereas
System.out.println("Value is "+((y >4 )? 99:9));
will print 9.
Why is this?

Sean <br />SCJP2, SCJP2p1.4, SCWCD
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Originally posted by sean cee:
Hi,
I got an interesting Q for you ...
int y = 4;
System.out.println("Value is "+((y >4 )? 99.99:9));
this will print 9.0.
whereas
System.out.println("Value is "+((y >4 )? 99:9));
will print 9.
Why is this?

Sean,
That is an intresting observation....what happens is when even one of the variables is double the other gets converted to double automatically.....in the first case 9 is getting promoted to double...coz the other condition variable is of type double...for eg:
int y = 3;
System.out.println("Value is "+((y <4 )? 99:9.5));
The result will be 99.0.......
Correct me someone if I am wrong....
Thanks,
Harpal
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

That looks right to me. I guess because ? returns a value it has to return the same type either way and it has to choose the wider type.


SCJP
Visit my download page
 
Consider Paul's rocket mass heater.
 
subject: System.out.prinln ...
 
Similar Threads
Be the JVM: what is the output
Trying to print a 2-dimensional table
can anyone explain this...
can anybody explain this??
precedence prob. again..this time 4rm RHE...very urgent