The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ?: 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 "?: operator" Watch "?: operator" New topic

?: operator

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481
int x = true ? 10 : 20 ; // ---> line 1
byte b=true?1:12345;//-->line 2

why the second line is giving error.i think,if the line 2 condition is false,it will assign value b=12345 and the value of int(12345) doesnot go in byte.Thats why it is giving error even we set the condition as true.Just want to make sure���.
Andrea Z�rcher

Joined: Oct 06, 2006
Posts: 10
I think you're right;-) 12345 is an int and cannot implicitly convert into a byte�

SCJP 5.0
Ade Barkah
Ranch Hand

Joined: Mar 17, 2004
Posts: 65
Yes, that is the reason, at least for JDK 1.4.

JDK 1.5 compilers are smart enough to realize that the test condition will always be true, and will accept the code without errors.

In my opinion the JDK 1.4 behavior is more appropriate.
[ October 08, 2006: Message edited by: Ade Barkah ]

-Ade<br /><a href="" target="_blank" rel="nofollow"></a>
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481
I agree. Here's the link:
subject: ?: operator
It's not a secret anymore!