Video není dostupné.
Omlouváme se.

Why Integer has this weird behaviour? | Java Interview Question

Sdílet
Vložit
  • čas přidán 25. 06. 2024
  • 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]

Komentáře • 238

  • @DeanBeckerdjbckr
    @DeanBeckerdjbckr Před měsícem +387

    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  Před měsícem +78

      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 Před měsícem +14

      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  Před měsícem +19

      @@mr.rabbit5642 Yes there is java.util.Objects.equals() for null safe and deepEquals for checking arrays equality.

    • @MortvmMM
      @MortvmMM Před měsícem +27

      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 Před měsícem +6

      @@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.

  • @1creeperbomb
    @1creeperbomb Před měsícem +271

    "OOP will be the future" -- Oracle probably

    • @Andreas_Mann
      @Andreas_Mann Před 29 dny +18

      not an oop issue

    • @hikari1690
      @hikari1690 Před 28 dny +5

      @@1creeperbomb it was the future! Shame Java couldn't keep up lol

    • @diablense
      @diablense Před 26 dny +3

      "Shooting in my own leg and making worst realizations possible will be the future" - Oracle, perhaps.

    • @michaelburggraf2822
      @michaelburggraf2822 Před 24 dny +3

      In that particular case...
      OOP = obfuscated Oracle programming😂

    • @janisir4529
      @janisir4529 Před 22 dny

      ​@@Andreas_Manncorrect, it's Java being dumb and not having operator overload...

  • @hikari1690
    @hikari1690 Před 28 dny +143

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

  • @martinschroederglst
    @martinschroederglst Před měsícem +221

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

    • @DaSquyd
      @DaSquyd Před 28 dny +32

      Operator overloading, while powerful, is also dangerous. The amount of times I've opened up someone's C++ code base and they thought they were clever for overloading

    • @bowiemtl
      @bowiemtl Před 28 dny +26

      @@DaSquydwell OOP can be abused as well. Giving the programmer more features rather than fewer features to me sounds like the better tradeoff. Tldr; skill issue

    • @awesomedavid2012
      @awesomedavid2012 Před 28 dny +20

      ​@@DaSquydI don't think a language should presume that programmers will misuse features. Any feature can be misused. Consider that there aren't real limitations on identifiers; a programmer *could* make a variable name random English letters. That's also very bad code, but so what? Should the language prevent you from doing it? Or should you give the programmer the tool and some guidance and call it a day

    • @RadenVijaya
      @RadenVijaya Před 28 dny +1

      Correct!

    • @DaSquyd
      @DaSquyd Před 27 dny +6

      @@awesomedavid2012 oh, I agree. I much prefer C++ over languages like Java for that very reason. I'm just saying that it does come with its own problems. While you can argue that it's a skill issue all you'd like, that doesn't somehow mean that you won't have to deal with others doing things in a confusing way because of that freedom.
      I should also note that some features are definitely more prone to misuse. Part of designing a language is making the features intuitive.

  • @beldraith8051
    @beldraith8051 Před 10 dny +3

    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

  • @xcoder1122
    @xcoder1122 Před měsícem +63

    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.

    • @Durayne
      @Durayne Před měsícem

      For Integer types == does not have that problem. Otherwise most of the embedded code could be shredded.

    • @xcoder1122
      @xcoder1122 Před měsícem

      @@Durayne YYou mean for "primitive" types (the types in the video were integer types, but they were objects, not primitives). Yet that's just another weakness of Java. There shouldn't even be primitive types. A language can cheat all it wants under the hood, but at least to the programmer it should be consistent.
      In SmallTalk, everything is an object, but that's a lie. SmallTalk only pretends that everything is an object. In actual SmallTalk implementations, not everything is an object, but the language just hides that fact from you as a programmer. Swift is similar: In Swift, all simple numbers can be used as complex data types; in fact, the compiler will often just store them as primitives in the compiled code, even in CPU registers when possible, but there is no boxing/unboxing for the developer at any time.
      Java could simply change "a == b" to "a.equals(b)" for all object types after compilation to bytecode, and for primitives it would just compare them directly at runtime instead, as it does today. And if you mix primitives and objects, it would box/unbox each as needed to compare them. After all, Java is completely type-safe, so it always knows the type of everything, even at compile time. At runtime, the bytecode would have looked the same as it does today, only the compiler (javac) would have produced slightly different bytecode depending on the two types to the left and right of ==.
      primitiveA == primitiveB => Do what you do today.
      objectA == objectB => Compile to "objectA.equals(objectB)".
      objectA == primitiveB => Unbox objectA if possible, then compare primitives, otherwise box primitiveB and compare with equals().

    • @zekiz774
      @zekiz774 Před 29 dny +1

      It does. Have you not watched the video?

    • @Durayne
      @Durayne Před 29 dny

      ​@@zekiz774 I was talking about C. Which the autor clearly was referring to.

    • @xcoder1122
      @xcoder1122 Před 29 dny +4

      @@Durayne You mean for "primitive" types (the types in the video where Integer types, but they were objects, not primitives). But that's another weakness of Java. There shouldn't even be primitive types. Under the hood, a language may cheat as much as it likes but at least to the programmer it should be consistent.
      In SmallTalk everything is an object but that's a lie. SmallTalk only pretends everything is an object. In actual SmallTalk implementations not everything is an object but the language just hides that fact from you as a programmer. Similar with Swift: In Swift all simple numbers can be used just as if they were complex data types; the compiler will in fact often just store them as primitives in the compiled code, even in CPU registers when possible, but it will store them as object-like types whenever required.
      Java could just made "a == b" to mean "a.equals(b)" after compilation in case a and b are objects, it could compare them just as it does today in case they are primitives and it could just box/unbox automatically if one is a primitive and one is an object (if the object can be unboxed, do that, otherwise box the primitive and use equals()). This would not have required any change to the Bytecode specification at all and thus would not have affected interpreters or JIT compilers. This would only be a change to the Java compiler (javac), after all Java is fully type-safe and the compiler knows the type of every variable and return value at compile time, so it would always know how to compile == according the rules I just specified.

  • @deltics735
    @deltics735 Před 24 dny +3

    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.

  • @rock64573
    @rock64573 Před 27 dny +4

    For those wondering why you would need to use wrapper classes for primitive types:
    1. Only `Object` types can be modified when passed as an argument to a method.
    2. `Object` types can have null value.
    3. Generic types only allow `Object` types.
    4. Can call multiple methods, like `compareTo()`, `equals()`, `toString()`, etc.
    5. Cloning and serialization are only supported for `Object` types.
    6. Objects are needed to support synchronization in multithreading.

  • @mementomori7160
    @mementomori7160 Před 28 dny +30

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

    • @yootoobvyooer
      @yootoobvyooer Před 24 dny +2

      Try it with C++ class, not int primitive.

  • @zombiezoo1384
    @zombiezoo1384 Před 29 dny +5

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

  • @cmyk8964
    @cmyk8964 Před měsícem +24

    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 `==`.

    • @Walter_
      @Walter_ Před měsícem +18

      "is" is used for checking same object pointer.
      "==" is used for checking contents.
      In 9/10 cases you want to use "==".

    • @williamdrum9899
      @williamdrum9899 Před měsícem +3

      @@cmyk8964 This behavior feels more like a bug rather than a feature. If I'm using a language like Java or Python, it's clear that I don't want to deal with pointers.

    • @HansBezemer
      @HansBezemer Před měsícem

      @@williamdrum9899 Agreed - if you create a language with the intention that any idiot can use it, "because it's safe", this kind of behavior is unacceptable - for it violates the primary design objectives.
      Now I don't mind languages that require you to know what's under the hood in order to do some useful work with it. That's why I (personally) like C and Forth. What I absolutely hate, though, is "simple languages" that do lots of stealthy transformations and conversions behind the curtains in order "to make it simple", but by doing so open up gaping holes every unaware programmer falls into. Which is not that bad when those things are caught early enough, but devastating when left undetected.
      I think that language designers should refrain from implementing these "hidden" conversions and design a language that does what it says it does. Sure, it's cool you can do *"A$ == A"* and simply transform the string to an integer, but what you're actually doing is supporting bad habits. Programmers need discipline - especially when they're actually noobs (you can give a true professional a language like C and it'll work out fine).

    • @minhnguyenphanhoang4193
      @minhnguyenphanhoang4193 Před měsícem +3

      ​@@williamdrum9899You normally don't use it at all. Only for things like None object but you can also just use ==

    • @largewallofbeans9812
      @largewallofbeans9812 Před měsícem +8

      In python, they make it pretty clear that “is” compares references so I don’t think you could call this “similar behavior”.

  • @friedrichmyers
    @friedrichmyers Před měsícem +54

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

    • @zanagi
      @zanagi Před měsícem +8

      Im even a c++ user, and damn this Java is not an honest language lol

    • @friedrichmyers
      @friedrichmyers Před měsícem +14

      @@zanagi Yeah. I liked C++, coming from C but the problem with it is that the learning curve is a lot high. I've met people who write blackmagic C++ and it fucks up the code. But who gives a fuck when you can have Aesthetic "std::views::iota new" instead of just writing shit in the way that makes sense.

    • @HansBezemer
      @HansBezemer Před měsícem

      @@friedrichmyers C++ was created to avoid common bugs and make software more solid by adding lots of features to 'automate' things. They made programmingso easy that nobody has a clue what they're doing. C++ (and Java) are the perfect examples of missing your original design objectives by a mile.

    • @blackscrow
      @blackscrow Před měsícem

      ​@@friedrichmyers well to use C++, you don't need to use that "black magic" stuff though... just use the features you want to use, and you can still write in C for the rest

    • @AKA-077
      @AKA-077 Před 29 dny +1

      Try c#

  • @Kagmajn
    @Kagmajn Před měsícem +6

    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!

  • @Misteribel
    @Misteribel Před 29 dny +5

    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).

    • @nutbastard
      @nutbastard Před 18 dny

      Seems oddly confusing to have the same operator for different use cases. I'd rather they add a === or some other unique operator and catch an invalid syntax error than get a false result.

  • @no_nuts0614
    @no_nuts0614 Před měsícem +20

    Why is there an object for an int

    • @you_are_the_best2233
      @you_are_the_best2233 Před měsícem +10

      Primitives can't be used as generic types so these wrapper classes were created.

    • @Abc-jq4oz
      @Abc-jq4oz Před 29 dny +9

      Why primitives can’t be used as generic types?

    • @you_are_the_best2233
      @you_are_the_best2233 Před 29 dny

      @@Abc-jq4oz If I remember correctly during compilation generic type provided is swapped to Object. Object is base class for everything in Java. Everything except primitives such as int or byte. Because of this caveat you have to use objects like Integer. If you want more in depth explanation search this topic on the net. There are more wierd things with generics in Java. For example you can't create generic arrays.

    • @x1nto
      @x1nto Před 28 dny +4

      @@Abc-jq4oz Because Java

    • @DaSquyd
      @DaSquyd Před 28 dny +10

      Because this is Java and everything needs to be unnecessarily complicated

  • @SoumendraHarichandan
    @SoumendraHarichandan Před měsícem +7

    Nice animations and explanation. Keep it up!

  • @StrangerNoises
    @StrangerNoises Před 22 dny +1

    i know why. (without watching.) Autoboxing and Integer.valueOf(n) will return a cached Integer instance for values -1 to 100 (IIRC), so those Integer objects are the same instance. For 900, it's a fresh Integer instance each time. You're using reference equality rather than Integer::equals so it's returning whether it's the same instance, regardless of value.

  • @andiback
    @andiback Před 24 dny +1

    😀 at 0:36 this is German TV actor Walther Hoffmann performing in popular wedding soap

  • @The16MHz
    @The16MHz Před 24 dny +1

    Integer in Java is a Class. Next Vídeo.

  • @prayer4675
    @prayer4675 Před 28 dny +3

    Actually it works differently. The following code may print true and true, but only if you run it by a command like java -XX:AutoBoxCacheMax=9000 Test.java
    public class Test {
    public static void main(String[] args) {
    Integer a = 9;
    Integer b = 9;
    Integer c = 9000;
    Integer d = 9000;
    System.out.println(a == b);
    System.out.println(d == d);
    }
    }

  • @ncmathsadist
    @ncmathsadist Před 15 dny +1

    Object types store memory addresses, not values.

  • @HagenvonEitzen
    @HagenvonEitzen Před 29 dny +1

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

    • @nmmeswey3584
      @nmmeswey3584 Před 25 dny +2

      it'd still be the same problem tho. The issue is with object references, not with the comparison

  • @drdca8263
    @drdca8263 Před 22 dny +1

    [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…)

  • @ObadiahHoss
    @ObadiahHoss Před 19 dny +1

    Great video

  • @WackoMcGoose
    @WackoMcGoose Před 22 dny +1

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

  • @AlvinYap510
    @AlvinYap510 Před 29 dny +29

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

    • @youtubepooppismo5284
      @youtubepooppismo5284 Před 28 dny +6

      There's nothing really wrong here

    • @fusedqyou
      @fusedqyou Před 20 dny

      @@youtubepooppismo5284 It's incredibly pointless to leave in such an obscure issue, and it is definitely wrong as equality is expected to pass in this case, even without the use of `isEquals()`.

    • @youtubepooppismo5284
      @youtubepooppismo5284 Před 20 dny

      @@fusedqyou Clearly you don't understand Java. Every language has its "weird" things which arise from the design. If you understand the basics of Java it totally makes sense. Why would an integer value be equal to the pointer value of an object pointing to an integer? It shouldn't. It's not obscure, you just don't understand memory in Java. The only weird thing here is that he first example (with the 9) does work. That's obscure. But it's just because there is a cache for low value integers, so the pointers are effectively the same. But you shouldn't really to it in the first place if you understand Java

    • @fusedqyou
      @fusedqyou Před 20 dny

      @@youtubepooppismo5284 No, I don't understand Java. I understand programming in general and when it comes to equality I would expect equality to work the way where it would make sense. When it comes to value types I would expect them to have equality in a way where it compares for uniqueness, or where it is relevant. I would NOT expect it to have some obscure check like this. You just don't see the issue here because you're used to this bad system and you know what to look out for. Imagine the average programmer that tries using Java and has to deal with the weird bugs it brings because of this.

    • @youtubepooppismo5284
      @youtubepooppismo5284 Před 20 dny

      @@fusedqyou Maybe what you don't understand is that using "Integer" is rather advanced in java. If you want to deal with integers, you use "int". Equality works fine with "int". That's what everybody uses. "Integer", is a class wrapper around an int. You don't usually use it. It's "advanced" stuff. So, no. the average programmer doesn't really have to deal with it, even though he should understand that's it's not a good idea to do == between two "Integer". If you really must know, I abandoned java years ago and have fully switched to rust. I'm not "accustomed to a bad design", I just understand that it's not bad. I mean you don't even code in Java and have the audacity to say "it's a bad design" - yet you don't understand the basics. It makes perfect sense if you understand basic java memory management and should not jump to conclusions after watching a 1 minute video which doesn't even really explain what's going on

  • @ninocraft1
    @ninocraft1 Před 28 dny +2

    i love the accent and the info, thx bro

  • @juliantheivysaur3137
    @juliantheivysaur3137 Před měsícem +3

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

    • @bytepint
      @bytepint  Před měsícem +5

      Integer (or other wrapper classes) are very much required for certain reasons, for example if I fetch data from DB and certain (integer) column is blank then I don't think representing it as '0' or '-1' is a good idea, in that case Integer is required so that we can represent it as null (primitive can't have null values).
      2nd. In Java generics (which is powerful feature of Java), primitive can't be used.

  • @dominiorrr6510
    @dominiorrr6510 Před 29 dny +7

    That's why all my homies hate Java

  • @ross9263
    @ross9263 Před 24 dny +1

    Hey this is a great video, make more!

  • @MuhammadAli-ve7mt
    @MuhammadAli-ve7mt Před 28 dny +2

    Hell yea, I knew this one. Just watched the video to confirm and I was absolutely correct. Last time I used Java was almost 3 years ago

  • @dhineshd94
    @dhineshd94 Před měsícem +2

    nice bro i always use equals method for comparing 2 strings... now got some info

    • @bytepint
      @bytepint  Před měsícem

      Thanks bro.
      Yes, Make a thumb rule.
      If primitive then ==
      Anything else .equals no matter what.

    • @williamdrum9899
      @williamdrum9899 Před měsícem

      @@bytepint "Primitive" being anything like your typical C numeric types, char, short, int, long, float, double. Just checking to see if I understand

    • @bytepint
      @bytepint  Před měsícem

      Yes Primitives are : all you mentioned + byte.

  • @21k60
    @21k60 Před měsícem +3

    Nice🎉🎉🎉🎉

  • @DHARMA252
    @DHARMA252 Před měsícem +6

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

  • @dewmi4403
    @dewmi4403 Před měsícem +1

    Deep but not much to dive in 😺👍🏻

  • @bhimesh99
    @bhimesh99 Před měsícem +2

    Nice

  • @EatSleepCodeRepeat_
    @EatSleepCodeRepeat_ Před 24 dny +1

    That's why C++ is still one of the most reliable languages out there.
    C++ offers full control over a lot of things including cache.
    I chose C++ because it's the only language I know. 😅 There are surely more than one language offering this much freedom.

  • @frommarkham424
    @frommarkham424 Před 18 dny +1

    TECHNOLOGY IS AWESOME🗣🗣🗣🗣💯💯💯💯🔥🔥🔥🔥

  • @nagesh007
    @nagesh007 Před měsícem +1

    Super

  • @yootoobvyooer
    @yootoobvyooer Před 24 dny +1

    Only reason I use java is android studio and majority of help on internet are in java.

  • @zenniththefolf4888
    @zenniththefolf4888 Před 29 dny +1

    That's probably why you should just use primitive int.
    The wrapper class should only be used for it's methods.

    • @a_cats
      @a_cats Před 28 dny

      or for generics

    • @henrycgs
      @henrycgs Před 28 dny +2

      that's why you should just use c#, lmao.

  • @AswinS-l6u
    @AswinS-l6u Před měsícem +5

    good explanation

  • @asandax6
    @asandax6 Před 27 dny

    Javascript: Perhaps we are related (==,===)

  • @rodrigoqteixeira
    @rodrigoqteixeira Před měsícem +3

    Nah bro, just use int when you can. There is almost no case where Integer is good, except for LinkedLists and stuff

  • @SugarBeetMC
    @SugarBeetMC Před 22 dny +1

    Why doesn't Integer implement __eq__() to override ==?
    ... oh.

  • @baxstart9008
    @baxstart9008 Před 24 dny +1

    so it almost acts like a 7 bit integer 💀

  • @Pawlo370
    @Pawlo370 Před 27 dny

    Thx

  • @psyk0l0ge
    @psyk0l0ge Před 28 dny

    So they have an IT somewhere that deceides If sth IS in this specific range IT will use Cache...
    But why?

  • @navneethgopal1211
    @navneethgopal1211 Před 26 dny

    Does this occur in C# as well? Cause' they are very similar languages.

  • @efekos
    @efekos Před 28 dny +1

    why would you use Integer instead of int in the first place

  • @Ribula1
    @Ribula1 Před 24 dny +1

    Ok so, int does not stand for interger... What is int then?

  • @2wheels2
    @2wheels2 Před měsícem +3

    this is y i dont like an abstracted lang like java. then again i dont like the 0xffffffff times my program segfaults cuz a pointer was freed with new address 0x01 instead of NULL in c

    • @HansBezemer
      @HansBezemer Před měsícem +2

      Actually - according to the standard _free()_ does *NOT* update your pointer. You have to reassign your pointer yourself when you want to check its value later on. And that's a good practice.

    • @2wheels2
      @2wheels2 Před měsícem

      @@HansBezemer i was making a screen saver for fun and i had to deal with the pointer pointing to 0x01 instead of 0x00 or null and it is weird cuz i set the pointer to be equal to NULL after free too.
      the "fix" was to not have the condition be pointer != NULL but pointer >= (illigalPageSize) 0x1000

    • @henrycgs
      @henrycgs Před 28 dny +1

      you could always use rust and have neither problems.

  • @gorlix
    @gorlix Před měsícem +5

    what is the point of having Integer class? this seems like such a bloat

    • @bytepint
      @bytepint  Před měsícem +3

      cuz Object-Oriented Programming ;)
      Well, Integer (or other wrapper classes) are very much required for certain reasons, for example if I fetch data from DB and certain (integer) column is blank then I don't think representing it as '0' or '-1' is a good idea, in that case Integer is required so that we can represent it as null (primitive can't have null values).
      2nd. In Java generics (which is powerful feature of Java), primitive can't be used.

    • @Z3rgatul
      @Z3rgatul Před měsícem +5

      C# (which was based on Java) implement this differently, and there is no Integer class, only primitive type. And everything works fine.

    • @marcoaureliofernandezreyes1413
      @marcoaureliofernandezreyes1413 Před měsícem +5

      C# is better. What a headache is java, tbh.

    • @baconboyxy
      @baconboyxy Před 29 dny +1

      Yea c# you can just add a ? to the data type to make it nullable and generics don’t have this constraint

    • @fusedqyou
      @fusedqyou Před 20 dny

      @@bytepint What a horrible system, why would they make it so complicated? Take a look at C#

  • @CorneliusCornbread
    @CorneliusCornbread Před 25 dny +1

    Me when I use C# and don't need to use a wrapper class for primitives because generics support the usage of primitives.

    • @plaidchuck
      @plaidchuck Před 24 dny

      Yet it doesn’t automatically cast floats 😂

    • @CorneliusCornbread
      @CorneliusCornbread Před 24 dny

      @@plaidchuck floats will be implicitly casted to doubles but not vice versa, as such a cast is lossy. Why you would want implicit, destructive casting is beyond me
      Also neither does Java, frankly I'm not sure what you're talking about here

  • @UnwittingSweater
    @UnwittingSweater Před 27 dny

    That's interesting but if i got that as an interview question i would leave the interview.
    A good reason why to use .equals through. But then just have that as a rule in your code pipeline so your team all have the same level of best practises.

  • @FaizanFurqanAhmad
    @FaizanFurqanAhmad Před měsícem +1

    👍

  • @eobardthawne6903
    @eobardthawne6903 Před 28 dny

    So even Oracle hard codes the values.
    Interesting 😂

  • @larvajoke4255
    @larvajoke4255 Před měsícem +4

    It is possible in c++

    • @bytepint
      @bytepint  Před měsícem

      In c++ afaik there is not concept of Integer caching and autoboxing hence behaviour is different.

    • @UltraAryan10
      @UltraAryan10 Před měsícem +5

      ​​@@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

    • @DaSquyd
      @DaSquyd Před 28 dny +2

      In C++, the == operator is a lot more honest. You know when you’re comparing pointers and you know when you’re comparing primitives.

  • @Z3rgatul
    @Z3rgatul Před měsícem +5

    Lmao, and ppl are bitching JavaScript 🤣

    • @TechnoSan09
      @TechnoSan09 Před 25 dny +2

      typeof(idk)
      >> Object

    • @fusedqyou
      @fusedqyou Před 20 dny

      At least Javascript breaks no matter what. I would hate having to fix a bug that happens because of some obscure cache

  • @AK-vx4dy
    @AK-vx4dy Před 26 dny

    And people go crazy about JS ;)

  • @farhan-momin
    @farhan-momin Před 28 dny +2

    Solution- Use int instead of Integer

  • @K4rmy
    @K4rmy Před 28 dny +14

    Not a problem in C#

  • @sami-nn9fg
    @sami-nn9fg Před 28 dny

    In Rust we trust 🦀

    • @turolretar
      @turolretar Před 27 dny +1

      until it doesn’t compile that is

  • @warmachineuk
    @warmachineuk Před 6 dny

    Don’t treat Integer objects like the primitive type. Use the primitive type in preference. Check for null if needed and get the primitive value for comparison.

  • @DeclanMBrennan
    @DeclanMBrennan Před 29 dny +3

    What a horrible mess! What genius thought caching some values was a good idea so the == operator doesn't have consistent semantics?

  • @rakshitharangarajan2445
    @rakshitharangarajan2445 Před 29 dny +1

    Does ur channel teaches java

    • @bytepint
      @bytepint  Před 28 dny

      Right now I’m new to CZcams so you might not find enough videos, but yeah i am planning to create some tutorials in near future.

  • @szymoniak75
    @szymoniak75 Před 28 dny +3

    What a horrible question to ask on an interview. I couldn't care less if someone knows caveats like this

  • @test-rj2vl
    @test-rj2vl Před 29 dny +1

    Putting 9==9 and 900 == 900 both in interview questions is evil. People are used to equals method so some might assume 9==9 is false while rhea reality is that such developer would simply use equals and thus the code would not fail.

    • @bytepint
      @bytepint  Před 28 dny +1

      Well I was reviewing some PRs and found people(mostly junior devs) were still using == for some reason for Integer class.

    • @csuporj
      @csuporj Před 28 dny +2

      You do a test with some small numbers, see that == works as you intended, then in prod you get strange bugs due to larger numbers. You may even have 10 unit tests that proves that your == code works, all with small numbers.

  • @stulora3172
    @stulora3172 Před 23 dny +2

    tldr: Java is broken

  • @billclinton4913
    @billclinton4913 Před 28 dny

    that's mad silly.

  • @sebastiantomczyk4577
    @sebastiantomczyk4577 Před 21 dnem +2

    Integer myGirlFriends = -1;
    xD ...;(

  • @williamdrum9899
    @williamdrum9899 Před měsícem

    Doesn't Python do the same thing?

    • @bytepint
      @bytepint  Před měsícem

      Yes, see reply comment of @cmyk8964

    • @largewallofbeans9812
      @largewallofbeans9812 Před měsícem +1

      No. Python supports operator overloading, so A == B is defined by a method of type(A). You may be thinking of the “is” operator, which always and only compares references.

  • @henrycgs
    @henrycgs Před 28 dny +1

    this is indefensible. having an Integer class to begin with is insane!

  • @jarekmyszko3332
    @jarekmyszko3332 Před 22 dny +2

    Always, always, always... use programming language that doesn't introduce such illogical behavior.

  • @DreanPetruza
    @DreanPetruza Před 25 dny

    Having implementation details affect the result is horrible design.

  • @farpurple
    @farpurple Před 27 dny

    The fffff okay i saw such in python but there is with "is" statement, Java absolutely weird on this, tf i should care, am i not comparing values??
    Who tf even made Integer class and why??
    Use freaking int, wtf is Integer ahha??

  • @russianyoutube
    @russianyoutube Před měsícem +1

    Maybe don't use Integer and just use int?

    • @DaSquyd
      @DaSquyd Před 28 dny

      In Java, generics require object types and don't allow primitive types, so they’re unavoidable unfortunately.

    • @russianyoutube
      @russianyoutube Před 28 dny

      @@DaSquyd yeah but that still doesn't explain why anybody would use Integer instead of int. When working with generics you will write Integer, but your variables will usually still be of type int

    • @DaSquyd
      @DaSquyd Před 28 dny

      @@russianyoutube correct. I don't use Java much, but I've never used Integer in the way shown in the video. I'm not sure who does or why they would.

  • @RadenVijaya
    @RadenVijaya Před 28 dny +1

    This is the stupid of Java side 😂 and it's inherited on Kotlin too 😂

  • @Gigasharik5
    @Gigasharik5 Před měsícem +14

    because java sucks

    • @bytepint
      @bytepint  Před měsícem

      😅

    • @russianyoutube
      @russianyoutube Před měsícem

      Pretty sure python does a similar thing too lol

    • @baconboyxy
      @baconboyxy Před 29 dny +1

      In python == checks for equality (including for strings) and “is” checks for reference (pointer location). So, this isn’t usually a problem in python unless you are writing custom objects you need to compare, then you need to build your own comparison (ex based on their attributes) anyway.

    • @elementaltamago1297
      @elementaltamago1297 Před 28 dny

      Python also sucks

    • @baconboyxy
      @baconboyxy Před 28 dny

      @@elementaltamago1297 Python has its uses, primarily anything that doesn’t need low level access and you want to write quickly. As long as you don’t need to squeeze out tons of speed, it’s not a problem.

  • @leshommesdupilly
    @leshommesdupilly Před 27 dny

    Because Java isn´t a serious programming language

  • @Fasteroid
    @Fasteroid Před měsícem +4

    Disgusting. Java is gross.

  • @warlockpaladin2261
    @warlockpaladin2261 Před 28 dny

    Just no.

  • @casperhansen826
    @casperhansen826 Před 22 dny +3

    Avoiding Java at all cost

  • @kyleeames8229
    @kyleeames8229 Před měsícem +9

    WTF!? Is this really a problem in Java?? That’s a failure in basic functionality. Why do people use this language?

    • @bytepint
      @bytepint  Před měsícem +6

      Well every language has some quirky behavior, otherwise Java is really powerful language.

    • @earth2k66
      @earth2k66 Před měsícem +3

      Still better than the mess they call JavaScript, especially on the backend nowadays.

    • @itsahandle
      @itsahandle Před měsícem +2

      I bet you're a python programmer

  • @T33K3SS3LCH3N
    @T33K3SS3LCH3N Před měsícem +5

    I was so confused by the thumbnail until the first seconds reminded me of the existence of this idiotic wrapper class concept.
    Java is truly the worst major programming language.

  • @CKidder80
    @CKidder80 Před 25 dny +2

    Java seems cursed. I already didn't like it and refuse to use it because it has no concept of unsigned integers (ICK!) but this is even worse. James Gosling should have been slapped with a trout when he was designing this cancer of a language.

  • @enitalp
    @enitalp Před 26 dny

    The same as LUA, you need to know how the VM works to program it. That's stupid.

  • @tetrabromobisphenol
    @tetrabromobisphenol Před 28 dny

    I knew there was a reason I never bothered to learn Java. YUCK. I hope Scala isn't repeating this same type of nonsense.