Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Java Integer Fun

Java Integer Fun

Scheduled Pinned Locked Moved Clever Code
csharpjavaquestionlearning
17 Posts 11 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dominik Reichl

    Integer i = 1;
    Integer j = 1;
    Integer n = 1000;
    Integer m = 1000;

    boolean b1 = (i == j);
    boolean b2 = (n == m);

    Now, what are b1 and b2? Surprisingly b1 is true, b2 is false. Of course, if you know why it works this way, it's totally obvious. But if you're a Java novice, the code can be pretty surprising :) Let's hope Microsoft will never introduce ugly hacks like this into .NET...


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #7

    OK, I assume that, as i, j, n and m are actually objects, and not primitive types, the correct way to compare them would be using the equals method thus:

    boolean b1 = i.equals(j);
    boolean b2 = n.equals(m);

    But I still don't see why i == j! Please put me out of my misery. :)


    When I am king, you will be first against the wall.

    X 1 Reply Last reply
    0
    • L Lost User

      OK, I assume that, as i, j, n and m are actually objects, and not primitive types, the correct way to compare them would be using the equals method thus:

      boolean b1 = i.equals(j);
      boolean b2 = n.equals(m);

      But I still don't see why i == j! Please put me out of my misery. :)


      When I am king, you will be first against the wall.

      X Offline
      X Offline
      xfournet78
      wrote on last edited by
      #8

      See java.lang.Integer.valueOf(int i) method code which is used for auboxing. It return the same object for values in range -128 .. 127

      1 Reply Last reply
      0
      • D Dominik Reichl

        Integer i = 1;
        Integer j = 1;
        Integer n = 1000;
        Integer m = 1000;

        boolean b1 = (i == j);
        boolean b2 = (n == m);

        Now, what are b1 and b2? Surprisingly b1 is true, b2 is false. Of course, if you know why it works this way, it's totally obvious. But if you're a Java novice, the code can be pretty surprising :) Let's hope Microsoft will never introduce ugly hacks like this into .NET...


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #9

        Ok, so here's the solution why it works this way :) First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer). So, we got 4 different Integer instances, right? No! :) The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references). Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        M L C S 4 Replies Last reply
        0
        • D Dominik Reichl

          Ok, so here's the solution why it works this way :) First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer). So, we got 4 different Integer instances, right? No! :) The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references). Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #10

          :wtf: :omg: :~ :wtf: :omg: :~ :wtf: :omg: :~ That is absolutely insane.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

          R 1 Reply Last reply
          0
          • D Dominik Reichl

            Ok, so here's the solution why it works this way :) First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer). So, we got 4 different Integer instances, right? No! :) The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references). Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.


            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            Brilliant explanation thanks. I just mailed this "teaser" to my Java tutor... :)


            When I am king, you will be first against the wall.

            1 Reply Last reply
            0
            • D Dominik Reichl

              Integer i = 1;
              Integer j = 1;
              Integer n = 1000;
              Integer m = 1000;

              boolean b1 = (i == j);
              boolean b2 = (n == m);

              Now, what are b1 and b2? Surprisingly b1 is true, b2 is false. Of course, if you know why it works this way, it's totally obvious. But if you're a Java novice, the code can be pretty surprising :) Let's hope Microsoft will never introduce ugly hacks like this into .NET...


              _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

              T Offline
              T Offline
              ToddHileHoffer
              wrote on last edited by
              #12

              I'm glad I don't code java.


              how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06

              1 Reply Last reply
              0
              • D Dominik Reichl

                Ok, so here's the solution why it works this way :) First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer). So, we got 4 different Integer instances, right? No! :) The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references). Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.


                _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #13

                I just gave my C# compiler a big long hug.

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                1 Reply Last reply
                0
                • D Dominik Reichl

                  Integer i = 1;
                  Integer j = 1;
                  Integer n = 1000;
                  Integer m = 1000;

                  boolean b1 = (i == j);
                  boolean b2 = (n == m);

                  Now, what are b1 and b2? Surprisingly b1 is true, b2 is false. Of course, if you know why it works this way, it's totally obvious. But if you're a Java novice, the code can be pretty surprising :) Let's hope Microsoft will never introduce ugly hacks like this into .NET...


                  _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                  C Offline
                  C Offline
                  CastorTiu
                  wrote on last edited by
                  #14

                  WOW :omg: Do you have some place in the documentation where it is explained? All my long Java programmers cooworkers felt for it :)...

                  1 Reply Last reply
                  0
                  • M Michael Dunn

                    :wtf: :omg: :~ :wtf: :omg: :~ :wtf: :omg: :~ That is absolutely insane.

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #15

                    Michael Dunn wrote:

                    That is absolutely insane.

                    We are talking about Java, remember... ;)

                    Ryan

                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                    1 Reply Last reply
                    0
                    • D Dominik Reichl

                      Ok, so here's the solution why it works this way :) First of all, we are using Integer wrapper classes instead of the primitive int type. When compiling, the integral values 1 and 1000 are implicitely converted to Integer objects (called auto-boxing, from int to Integer). So, we got 4 different Integer instances, right? No! :) The Java VM automatically packs "small" primitive values into identical objects (1 is small, 1000 isn't small). You can easily verify this by pasting the code above into a small testing project and fire up the debugger: i and j reference the same object, while n and m reference different objects. The == operator is comparing references, therefore b1 is true (same references) and b2 is false (different references). Currently "small" means any integer between -128 and 127. But according to Sun this can change to [-32768, +32767] in the future. This packing trick has been introduced to solve performance problems when doing arithmetic with wrapper classes.


                      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #16

                      But what happens when the boxed value is modified? Does some kind of "Write on copy" thing happen in the background?

                      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                      1 Reply Last reply
                      0
                      • D Dominik Reichl

                        Integer i = 1;
                        Integer j = 1;
                        Integer n = 1000;
                        Integer m = 1000;

                        boolean b1 = (i == j);
                        boolean b2 = (n == m);

                        Now, what are b1 and b2? Surprisingly b1 is true, b2 is false. Of course, if you know why it works this way, it's totally obvious. But if you're a Java novice, the code can be pretty surprising :) Let's hope Microsoft will never introduce ugly hacks like this into .NET...


                        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

                        G Offline
                        G Offline
                        Gary R Wheeler
                        wrote on last edited by
                        #17

                        The people who decided that should be euthanized as a service to humanity.


                        Software Zen: delete this;

                        Fold With Us![^]

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups