• Post Reply Bookmark Topic Watch Topic
  • New Topic

Precise Sematics for == - operator for "Primitive Wrapper" Classes  RSS feed

 
Fred Wolf
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm in the process of preparing for the Java 8 OCA exam. I keep making too many mistakes with operator == for "Primitive Wrapper Classes"
(like Integer, Float, ...) and the boxing and unboxing aroud it.

My preparation book does not answer anything that comes only close to the sophistication of the questions in the Enthwasre Mock Exams.
As an "old C++ programmer by trade I was lloking for some Tutorial or something that would tell me PRECISE semantics of operator ==
for primitive wrappers - NADA - Nothing. Or I only did not find it. Also the Enthuware exams are a bit scarce if it comes to explaing the exact semantics
and not only "that's wrong" ...

Where can I find something that explains it so, that I can go thrugh that stuff, understand it and make zero mistakes
I'm tired of getting 80% results because of combinations that look to me like being nowhere really explaned ...

Thanks ...

Fred
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred van de Code wrote:
I'm in the process of preparing for the Java 8 OCA exam. I keep making too many mistakes with operator == for "Primitive Wrapper Classes" (like Integer, Float, ...) and the boxing and unboxing aroud it.


If both sides of the "==" comparative operator are the wrapped instance (like Integer, Float, ...), then the instances are compared like any other object comparison. If the two sides are the same instance, then they are equal; otherwise, they are not, regardless of the wrapped value.

If only one side (or neither side) of the "==" comparative operator are the wrapped instance, then it will be unboxed. And the comparison will done via the comparison for the two primitive values (for equality).

Henry
 
Fred Wolf
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thanks .. solved the practical problem! For the benefit of others see demo code below
Still .. where would I have found exactly a description of such behaviour
In C++ (I know Java is NOT C++) I would have found it in a class description - in Java I did not find the operators in the class descriptions

Demo code for others - all test yield "Success" - no compilation errors

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Wolf wrote:
In C++ (I know Java is NOT C++) I would have found it in a class description - in Java I did not find the operators in the class descriptions


Java, unlike C++, does not support operator overloading. This means that classes do *not* get to change the meaning of the operators. It is what it is. So, no reason to have a section in the class documentations for operators.

Henry
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . If only one side (or neither side) of the "==" comparative operator are the wrapped instance, then it will be unboxed. And the comparison will done via the comparison for the two primitive values (for equality).

Henry
This does not conflict with what I told you here. That is a situation which didn't exist pre‑2004; had you written the following before Java5:-. . . the code would have failed to compile. Since Java5 that would print true, but that is a new situation not encountered before Java5. A new situation doesn't conflict with backward compatibility.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!