This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
hi guys..sorry frnds..i m not cleared with the concept of boxing and unboxing concept yet..what is the concept of equals and ==.??please explain this..thanks in advance
Joined: Jul 09, 2009
When I do "if(c == b)", the == is doing a comparison of the references of these vars. The Byte.equals() method returns true if the equals parameter is a "instanceof" Byte. That's how it was implemented.
The autoboxing is related the Wrapper classes of primitive types. Each primitive type in Java (int, byte, long, float, etc) has a corresponding (wrapper) class (Integer, Byte, Long, Float, etc) to be used when an object is expected instead of a primitive value/type.
Before Java 5.0, the process to handle the interaction of primitive types and its wrappers was "manual"... Autoboxing came to make things easier.
You should read more about Wrapper classes and Autoboxing to understand better that process. Not too complex. But some aspects are implementation details you need to know.
Not easy to summarize all this in one post, but read more about Wrapper classes and Autoboxing and tell us your questions. I'm still studying that too.
cchetan jain wrote:hi guys..sorry frnds..i m not cleared with the concept of boxing and unboxing concept yet..what is the concept of equals and ==.??please explain this..thanks in advance
Use equals if both operands are Objects (wrapper instances).
Use == if at least one is a primitive.
Use the valueOf method combined with equals if one is a primitive and the other is a wrapper instance that may perhaps be null to prevent NullPointerExceptions:
Joined: Jul 05, 2009
thanks hiram for the concern..can you send me a link for the better understanding of wrapper classes..i have read kathy and bates already..but still confused..so please send me link