[HD Pg 110, Sec. 5.1.1 - overview-of-operators-available-in-java]
Posted: Wed Oct 10, 2018 4:01 am
In table in page 113. Boolean flag=false; sout(false != flag); prints false. You have a mistake in book.
Java Certification Resources and Java Discussion Forum
http://enthuware.com/forum/
should beIt returns true if the object pointed to by the reference
variable on the left is of the type (or a subtype) of the type given on
the left and false otherwise.
?It returns true if the object pointed to by the reference
variable on the left is of the type (or a subtype) of the type given on
the right and false otherwise.
Code: Select all
Object obj = new Double(10);//
System.out.println(obj != 10); //1 can’t compare a reference with a number
int a = 10; Double d = 10.0;
System.out.println(a != d);//2 prints false (book example)
My blind spot is trying to understand why the compiler does not treat these the same.admin wrote: ↑Sun Jul 07, 2019 8:56 pmHow can a reference be same as a primitive? obj is a reference. It can never be same a integer 10. Same with a and d. a is an primitive variable and d is a reference variable. Both are inherently different things and can never point to the same object (which is what == checks).
I think the compiler does not treat these the same because of a very important piece of information that apparently I overlooked:
The one that compiled (a != d) adhered to this rule, as does the first example box mentioned for ==, != (Binary) in the book? Whereas the other (obj != 10) did not adhere to this rule, as does the second example box mentioned for ==, != (Binary) in the book?. Thus, the compiler complained. Correct?When used on two primitive values or a primitive value and a primitive wrapper, they check whether the two values are same or not.
Pretty sure you switched left and right there.It simply copies the value on the left to the variable on the right.
Yes, added to the errata only a couple of days ago.DazedTurtle wrote: ↑Mon Oct 07, 2019 1:11 pmIn the bit about the = operator:
Pretty sure you switched left and right there.It simply copies the value on the left to the variable on the right.