File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding Byte and == Op Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Understanding Byte and == Op" Watch "Understanding Byte and == Op" New topic
Author

Understanding Byte and == Op

Ethan Bosco
Greenhorn

Joined: Nov 01, 2004
Posts: 15
I am unable to understand the output



Output :- False True
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ethan Bosco
Greenhorn

Joined: Nov 01, 2004
Posts: 15
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 22, 2004
Posts: 28
i have the same thought with marc weber and my compiler tell me that the result is "falsefalse"


Step one step back to learn more thing..!!<br />Post to learn<br />learn from mistake<br />take whatever i understand...!!!<br /> <br />SCJP 1.4, Brainbench Java 1 & 2.<br />SCWCD 1.4 (preparing...!!)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"thirxtymilk" please read our JavaRanch Naming Policy and change your displayed name to conform with it.

Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Same code without the redundant if stuff:


Prints "falsefalse" on Java 1.5.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding Byte and == Op
 
Similar Threads
Wrapper-Method-Q
Why this prints false?
* Byte == comparision
Byte and String
Doubt on "=="