aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary operator 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 "ternary operator" Watch "ternary operator" New topic
Author

ternary operator

challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
public class Ternary

{

public static void main(String args[])

{

int a = 5;

System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));

}

}


I thought the output is
value is - 9

but the output is
value is -9.0

why? can anyone please explain?
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi purnima

------------------------------------------------------
System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
------------------------------------------------

Whenever u do ternary operation, then values should be in the same data type, if not compiler automatically promotes to the larger data type.If compiler cannot do so it simply throws error.

System.out.println("Value is - " + ((a < 5) ? 9.9 : "nine"));

In this case Error
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ternary operator