Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding Byte and == Op

 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to understand the output



Output :- False True
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled and ran your code using 1.5.0, and got "False False" as output. (The toString method is creating new Strings at runtime, so these are different references.)

Can you verify your output?
[ November 01, 2004: Message edited by: marc weber ]
 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for taking the time to answer, I am also using 1.5 but I am getting the output that I had mentioned - what output are you getting and and can u explain it ? thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output I get is "False False." The toString method of Byte is returning a new runtime String instance each time it's called, and == is a shallow comparision of references.

b1.toString() is one reference, and b1.toString() is another, so the == comparison returns false. (These are not literals in the pool.)

Of course, the deeper equals() comparison returns true.

 
Hai Le
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the same thought with marc weber and my compiler tell me that the result is "falsefalse"
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"thirxtymilk" please read our JavaRanch Naming Policy and change your displayed name to conform with it.

Thanks
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same code without the redundant if stuff:


Prints "falsefalse" on Java 1.5.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic