File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

?: operator

 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're right;-) 12345 is an int and cannot implicitly convert into a byte�
 
Ade Barkah
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic