This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expression. incomparable types and operator " ? : "

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what difference in "10"==10 and (a==10?"10":0)==10. Why second compile?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This doesn't compile for me. Nor does it look like it should. What version of Java are you using at the command line?

I tried it with Java 8 at the command line.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jdk1.7.0_60 and i also try this online compiler http://www.compileonline.com and this http://ideone.com
both compile this file.
jdk1.7.0_51 and this for javac
i will try last jdk1.7 and report. as i see this was maybe bug of my version
and last jdk1.7.0_67
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take them one at a time:

"10"==10
A String and an int are different types, so can never be the same and you'll get a compiler error.

(a==10?10:50)=="10"
Same issue here. The conditional operator (left operand) results in an int, the right operand is a String. A String and an int are different types, so can never be the same and you'll get a compiler error.

(a==10?"10":0)==10 and (a!=10?"10":0)==10
This is the most tricky one The left operand (conditional operator) can be one of 3 types: Object, Serializable (Integer & String both implement this interface) & Comparable<?> (Integer implements Comparable<Integer> & String implements Comparable<String>). And just to prove, the following statements compile without any problem:


So I assume for the left operand results in any of these common types and the right operand gets auto-boxed to an Integer. So you'll comparing 2 object references which are not from a different type hierarchy, so that's allowed. But it only compiles & runs using javac/java; in my IDE (Eclipse) I also get a compiler error So it's not code I would encourage to write

(a==10?"10":0)=="10" and (a!=10?"10":0)=="10"
This one compiles and runs, both using javac/java & Eclipse. That's even more strange to me. This only gets me more and more !


(I'm using java version 1.7.0_65)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here are 2 other cases:

(a==10?"10":"0")==10
Now the conditional operator (left operand) always results in a String, the right operand is an int. A String and an int are different types, so can never be the same and you'll get a compiler error. Both using javac/java as an IDE. Phew!


(a==10?10:0)==10
Now the conditional operator (left operand) always results in an int, the right operand is an int. So this code compiles and runs successfully. Both using javac/java as an IDE. Phew!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic