Тёмный

Why Integer has this weird behaviour? | Java Interview Question 

Byte Pint
Подписаться 85
Просмотров 5 тыс.
50% 1

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!
[Java,software engineering,programming,computer science,Java projects,Java tutorial,coding,programming secrets,Java programming,software development,programming tricks,Java code tricks,coding tricks,Java development,Java tricks,Java tips,programming tips,Java secrets,Java hacks,python, javascript,equals and hash, ==, Integer, Integer.valueOf, comedy, programming memes, programming humour]

Опубликовано:

 

25 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 28   
@martinschroederglst
@martinschroederglst 14 часов назад
Operator overloading doesn't sound like a bad idea now, huh?
@DeanBeckerdjbckr
@DeanBeckerdjbckr 3 дня назад
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.
@bytepint
@bytepint 3 дня назад
Yes, that is the rule: always use .equals() for object equality checks. The goal of this video was to educate. Even if == sometimes gives the correct result, it should not be made standard practice. For example, if you create two strings like String a = "Hello"; String b = "Hello";, both a == b and a.equals(b) will always give the correct result no matter what. However, in real-world scenarios and more complex repositories, we cannot be certain. Let's say someone somewhere creates String c = new String("Hello");, then most probably == will fail. Therefore, to prevent any mishaps, do not use the == operator for object content comparison. cheers!
@mr.rabbit5642
@mr.rabbit5642 День назад
I reckon there's a safer option with static method Object.equals() that takes two arguments instead. It won't derp out with a NullPointerException
@bytepint
@bytepint День назад
@@mr.rabbit5642 Yes there is java.util.Objects.equals() for null safe and deepEquals for checking arrays equality.
@MortvmMM
@MortvmMM День назад
I mean sorry, if the Integer class has not yet overriden the == operator, that's not the users problem. Gezus how is Java so behind??
@DeanBeckerdjbckr
@DeanBeckerdjbckr День назад
@@MortvmMM You indeed bring up a good point. The designers behind Java are highly reluctant to change behavior like you suggest, so it stays that way. And this brings up the reason for languages like Groovy, Scala, and most recently Kotlin. You can design your own operators on any class to behave the way you think it should be.
@friedrichmyers
@friedrichmyers 20 часов назад
Lmao that's why I'm still on C. But I like Java too.
@cmyk8964
@cmyk8964 10 часов назад
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 `==`.
@SoumendraHarichandan
@SoumendraHarichandan 3 дня назад
Nice animations and explanation. Keep it up!
@DHARMA252
@DHARMA252 2 дня назад
I kinda knew it but thanks man. There's never enough knowledge about something.
@bytepint
@bytepint 2 дня назад
Yes 😄
@21k60
@21k60 3 дня назад
Nice🎉🎉🎉🎉
@bhimesh99
@bhimesh99 3 дня назад
Nice
@dhineshd94
@dhineshd94 3 дня назад
nice bro i always use equals method for comparing 2 strings... now got some info
@bytepint
@bytepint 3 дня назад
Thanks bro. Yes, Make a thumb rule. If primitive then == Anything else .equals no matter what.
@AswinS-l6u
@AswinS-l6u 4 дня назад
good explanation
@bytepint
@bytepint 4 дня назад
Thanks brother!
@FaizanFurqan034
@FaizanFurqan034 3 дня назад
👍
@williamdrum9899
@williamdrum9899 13 часов назад
Doesn't Python do the same thing?
@larvajoke4255
@larvajoke4255 2 дня назад
It is possible in c++
@bytepint
@bytepint 2 дня назад
In c++ afaik there is not concept of Integer caching and autoboxing hence behaviour is different.
@UltraAryan10
@UltraAryan10 19 часов назад
​​@@bytepintEven if there was, the concept of operator overloading to get desired behaviour is common in C++. You can also already check if two objects are same kindof with &a == &b
@gorlix
@gorlix 18 минут назад
what is the point of having Integer class? this seems like such a bloat
@Gigasharik5
@Gigasharik5 10 часов назад
because java sucks
Далее
My Brain after 569 Leetcode Problems
7:50
Просмотров 2,4 млн
10 Neat Ways To Use Underscore In Python
18:17
Просмотров 17 тыс.
It's the opposite! Challenge 😳
00:12
Просмотров 2,9 млн
When You Get Ran Over By A Car...
00:15
Просмотров 7 млн
My 10 “Clean” Code Principles (Start These Now)
15:12
My Software Engineer Salary Progression
0:59
Просмотров 503 тыс.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Dependency Injection, The Best Pattern
13:16
Просмотров 756 тыс.
Pointers and dynamic memory - stack vs heap
17:26
Просмотров 1,4 млн
5 Uncommon Python Features I Love
15:09
Просмотров 135 тыс.