There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bhanuprasad saketi wrote:i am try to write scjp exam but i have one doubt in == and equals()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Thanks and Regards
OCPJP 100%
Winston Gutkowski wrote:After you've passed it, if you're smart, you will never, ever, ever, EVER use '==' for anything except comparing primitives.
fred rosenberger wrote:I forget how many, but some Integer objects are cached...so anything below (let's say) 256 is pre-built, and thus all references point to the same object. So, clearly, 100 is below that threshold, and 1000 is over it.
The difference between == and .equals is sort of like comparing bank accounts. I may have two pieces of paper, each having a bank account number written on it.
== will tell you if the bank account numbers are the same.
.equals will tell you if the two accounts have the same amount of money in them.
R. Jain wrote:The range for which it happens is :-
(-128 to 127)... Outside this range any integer with same values will be different...
Mike Simmons wrote:Or enums.
Or you'll move on to any of the other JVM languages like Scala or Groovy that remove these odious rules and let you use == to mean what most people expect it to mean, rather than one Java defines it to mean.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
And enum constants.Winston Gutkowski wrote: . . . After you've passed it, if you're smart, you will never, ever, ever, EVER use '==' for anything except comparing primitives. . . .
You are right to forget, Fred.fred rosenberger wrote:I forget how many, . . .
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
And was mistaken.shaileshkumar mistry wrote:
HI
till Integer 127.. Integer objects are cached.. . . .
Winston Gutkowski wrote:Personally, I'd also make it a compiler warning if '==' is used for anything except specifically comparing primitives.
And that is mistaken.R. Jain wrote: . . . (-128 to 127)... Outside this range any integer with same values will be different...
Matthew Brown wrote:
Winston Gutkowski wrote:Personally, I'd also make it a compiler warning if '==' is used for anything except specifically comparing primitives.
I'm not convinced it's that simple. Let's say you've got two reference variables. You want to check if they refer to the same object. Oh yes, and either or both of them might be null. Try doing that without using ==.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:
And enum constants.Winston Gutkowski wrote: . . . After you've passed it, if you're smart, you will never, ever, ever, EVER use '==' for anything except comparing primitives. . . .
Campbell Ritchie wrote:
And that is mistaken.R. Jain wrote: . . . (-128 to 127)... Outside this range any integer with same values will be different...
So many people read -128…127 and think that is the range. It isn’t. You need to read the Java Language Specification and Integer#valueOf(int) documentation carefully, where you find it is -128…127 and maybe more. It is in fact undefined, -128…127 being only a minimum. As Stuart Burkett said, you can alter that. So the behaviour of this code can legitimately vary from machine to machine, or even on the same machine from run to run if the JVM is configured differently:-It might be because when Java was developed, 8MB was a generous amount of memory in a computer, whereas nowadays computers are available with > 1000× as much memory.
"Il y a peu de choses qui me soient impossibles..."
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:[standard equals/hashCode contract stuff removed]
This kind of thing puts me off of standard-library types of calls to compare objects entirely. I tend to write my own comparators that are, if I want them to be, only dependent on certain aspects of an object to measure its equality to another object, and I definitely don't want to find out that I violated some "contract" made by a method I never heard of, don't need, and won't use.
Stevens Miller wrote:While having == do what .equals() does might be preferable, you'd still need some way to test if two references were truly to the same object. Rare, perhaps, but I don't want to lose it. Java rather blessedly (imho) does not allow operator overrides.
Mike Simmons wrote:
Stevens Miller wrote:[standard equals/hashCode contract stuff removed]
This kind of thing puts me off of standard-library types of calls to compare objects entirely. I tend to write my own comparators that are, if I want them to be, only dependent on certain aspects of an object to measure its equality to another object, and I definitely don't want to find out that I violated some "contract" made by a method I never heard of, don't need, and won't use.
Well, umm, that's a pretty important pair of methods to know about in Java.
Do you never use HashMap or HashSet?
Stevens Miller wrote:While having == do what .equals() does might be preferable, you'd still need some way to test if two references were truly to the same object. Rare, perhaps, but I don't want to lose it. Java rather blessedly (imho) does not allow operator overrides.
Well, we're not talking about an override; we're talking about a language-level change in how == works.
Which will never happen, but it's irritating. Languages like Scala, Groovy, and JRuby simply define == to do the most commonly-desired operation, compare object contents, and then define other operators ('foo === bar' in Groovy; 'foo eq bar' in Scala, foo.object_id == bar.object_id in Ruby) for the less-commonly-desired operation, comparing object identity. Certainly this operation needs to be possible. But it can afford to have a seldom-remembered operator; leave == to the most useful operation.
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:I've always enjoyed operator overloading. Not sure what makes them different than other methods.
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:I'd rank this whole issue a lot lower in priority for changes than, say, the lack of unsigned integer primitive data types. But that's a topic for another thread, I suppose.
Stephan van Hulst wrote:
Stevens Miller wrote:I'd rank this whole issue a lot lower in priority for changes than, say, the lack of unsigned integer primitive data types. But that's a topic for another thread, I suppose.
At the risk of hijacking this topic (not sure we haven't already, maybe we should split this discussion off), I really think it was a great choice to dump unsigned integer values. They're only used in low level applications, and Java was written with a high abstraction level in mind. High level code really shouldn't care about the internal organization of primitives. On top of that, it's really trivial to convert signed integers to unsigned equivalents and vice versa.
"Il y a peu de choses qui me soient impossibles..."
Mike Simmons wrote:One can argue that it should have been designed differently (I would have it throw UnsupportedOperationException by default, so you'd be promptly notified if you ever put something in a hash table without a proper equals() and hashCode()).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:My point is mostly that people use operator overloading/overriding wrongly. Doesn't make it a bad feature, it makes the implementations that people write bad. People misuse inheritance a lot, but that doesn't make inheritance inherently bad.
Matthew Brown wrote:I suppose the bottom line is: I wish I could use operator overloading, but I'm glad other people can't
.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Matthew Brown wrote:I suppose the bottom line is: I wish I could use operator overloading, but I'm glad other people can't
.
Well, except me that is.![]()
"Il y a peu de choses qui me soient impossibles..."
Don't get me started about those stupid light bulbs. |