Why Integer has this weird behaviour? | Java Interview Question

preview_player
Показать описание
In this mind-blowing video, we dive into the fascinating world of Integer and uncover a surprising concept that will leave you in awe. Have you ever wondered why in case of Integer a = 2; Integer b = 2; a == b evaluates to true, while Integer a = 200; Integer b = 200; results in false? Join us as we unravel this epic programming mystery and reveal the secrets behind this unexpected(or may be expected?) behavior. Get ready to have your mind blown by this Java code revelation!

Рекомендации по теме
Комментарии
Автор

Well.... when comparing objects, you are supposed to use the .equals() method. This is the ONLY way to test the equality of objects. You can use == ONLY on primitive types.

DeanBeckerdjbckr
Автор

I was so confused until I realized it's the Integer not int

hikari
Автор

"OOP will be the future" -- Oracle probably

creeperbomb
Автор

Operator overloading doesn't sound like a bad idea now, huh?

martinschroederglst
Автор

The main problem is that in Java == works like in C, whereas in all modern programming languages, == works like .equals() in Java, which makes much more sense, as there are barely any situations where it really matters that two object references really point to the same object, most of the time it only matters if two objects are equal or not.

xcoder
Автор

Small Fun Fact: you can access the Integer Cache via Reflection. Just add a static Code block and rewrite the Cache with random numbers and enjoy the Autoboxing chaos unfold

beldraith
Автор

This shouldn’t be an interview question, it should be a question for the C-Suite of any company that thinks it’s a good idea to build critical systems using Java.

deltics
Автор

Never touched java before, I've learned from this vid that I'm not gonna touch it ever

mementomori
Автор

Python has similar behavior! Integers from 0 to 255 and (if I recall) -1 to -4 get cached, so any instance of 255 `is` 255, but multiple instances of 256 may not be true when compared with `is` and not `==`.

cmyk
Автор

Another way of saying this is that == is "reference equals" for objects and "value equals" for value types. This doesn't surprise experienced programmers, this is Java 101. This difference exists in virtually every language (though with different syntax).

Misteribel
Автор

such a quality, short, to the-point video... thanks i did not knew this happens

zombiezoo
Автор

Good to be aware of that. My 1st lessons with Java were learning how to compare objects and primitives, and our teacher told us about ".equals()" and "instanceof". Cool video!

Kagmajn
Автор

So perhaps its safer to use `if a< b else if b< a else`

HagenvonEitzen
Автор

Lmao that's why I'm still on C. But I like Java too.

friedrichmyers
Автор

Okay, but... _why_ do you need an object version of a primitive data type, anyway?

WackoMcGoose
Автор

Why is Integer an object anyways? What functionality would be lost by using a primitive value?

juliantheivysaur
Автор

Object types store memory addresses, not values.

ncmathsadist
Автор

[edit: dang, youtube interpreted the underscores as indicating italics. Does youtube support escaping characters? Apparently not quite.] Seems to me like Python has the right idea here: use “is” for checking if the lhs and the rhs are the same object, and use “==“ to do lhs.\_\_eq\_\_(rhs) (or rhs.__req__(lhs) if lhs has no __eq__ attribute, maybe. Idk if Python actually has a __req__ thing, as I believe Object has a default implementation of __eq__, so it would only fail to exist if you removed it?).
Or I guess your language could use “===“ in place of “is” if you prefer sigils to keywords.
Having == check for identity for type Integer seems inconvenient without a good reason..
(other than possibly legacy code, I guessss…)

drdca
Автор

I kinda knew it but thanks man. There's never enough knowledge about something.

DHARMA
Автор

Thank god I never learned Java, and I never planned to do so.

AlvinYap