This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
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