aspose 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 Spring in Action this week in the Spring 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: 4347
    
    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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: System.out.prinln ...