• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

AutoBoxing question

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output: f t t f.
WHY? Thanks a lot.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
15.21.2 Boolean Equality Operators == and !=

15.21.3 Reference Equality Operators == and !=

[ October 25, 2006: Message edited by: wise owen ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by wise owen:
15.21.3 Reference Equality Operators == and !=


[ October 25, 2006: Message edited by: wise owen ]



"The run-time values of the two operands would necessarily be unequal."
What exactly does this mean?

Many thanks!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new operator always creates a new object.
 
Bing Qiao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

Right. But given the next sentence: "At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false."

It's very much possible that "the run-time values of the two operands are actually equal because they refer to the same object!"

Saying "would necessarily be unequal" without giving a proper context only makes people confused, doesn't it? Or it's my English not good enough to understand this kind of subtlety?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sentence you refer to is not a general statement. It is part of this paragraph.

A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (�5.5). The run-time values of the two operands would necessarily be unequal.

This means that the == operator cannot be used between references where one is not a subtype of the other.

This means, for example, that you cannot do something like this.



Since neither FileReader nor String is a subtype of the other, there is no chance that the objects file and s refer to could be the same.
[ October 25, 2006: Message edited by: Keith Lynn ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Yan:

output: f t t f.
WHY? Thanks a lot.



in the first case you are comparing the reference variables of two different Objects so the expression evaluates to false

in the second and third cases since b3 is a primitive, b1 and b4 are unboxed and their primitive values are returned for comparision so these expressions evaluate to true.

in the third case the same logic applies as the first because b1 and b4 are different objects on the heap.

But if you had another object
Boolean b5=true;
then
b4==b5 will evaluate to true because the jvm assigns the same object to bothe the references b4 and b5.(something like Strings literals in String Pool)
 
Bing Qiao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But if you had another object
Boolean b5=true;
then
b4==b5 will evaluate to true because the jvm assigns the same object to bothe the references b4 and b5.(something like Strings literals in String Pool)



Good reminder!
 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IC.
Thanks alot.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic