Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Difference between Integer and int?

Hi,

What's the difference between Integer and int? Performance or anything?

Also, I'd like to be able to pass an integer value by reference, something like:

void Trial()
{
    Integer n = 0;
    ChangeInt(n);
}

void ChangeInt(Integer test)
{
    test = 5;
}

Is there someway to do that, will Integer do it?

Thanks
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

read this:
http://mindprod.com/jgloss/intvsinteger.html

in short:
int is a primitive
Integer is a object
Avatar of sciuriware
sciuriware

And you can work with collections and databases based on Integer objects, never with int.
Thus, you can put numbers into lists, trees and maps by encapsulating them as Integer.

;JOOP!
>>What's the difference between Integer and int? Performance or anything?
int --> primitive, mutable, high performance
Integer --> object, immutable,


>>Also, I'd like to be able to pass an integer value by reference, something like:
Java doesn't pass method arguments by reference; it passes them by value.

so to change the value of integer object in a methot you need to create a new one and return it;

      void Trial() {
          Integer n = new Integer(0);
          System.out.println(n.intValue());
          n = ChangeInt();
          System.out.println(n.intValue());
      }

      Integer ChangeInt()
      {
          return new Integer(5);
      }


for more info;
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
>>Java doesn't pass method arguments by reference; it passes them by value.

I think its opposite..

Integer object is immutable, meaning by you cannot change the value, once integer object is constructed.

Similar to String object
Java always passes arguments by value. cf:
http://java.sun.com/developer/JDCTechTips/2001/tt1009.html

>>I think its opposite.
have a look at the link i posted above.
dupe post, me and ysnky - link is pale. ;)
Avatar of DJ_AM_Juicebox

ASKER

I don't understand though  - why did they design the language so that you can't pass an integer by reference. I mean what if you have a large number of variables to modify in a single method call:

void whatever(x, y, z)
{
}

and you want 'whatever()' to modify x,y,z - then you're in trouble, I guess you have to make some subclass to just encapsulate those three? Doesn't make sense to me, coming from C++ anyway. What is the logic behind the decision?

Thanks
Because int is one of 8 primitive data types, built into the language as reserved keywords. All languages must have primitives otherwise they would never be able to compile values and pass them through as recognisable values to the operating system and CPU. Objects are just fancy wrappers for primitives.
> why did they design the language so that you can't pass an integer by reference.

to reduce the possibility of side effects.
You end up with cleaner and more maintainable code without it (pass by ref) as there is no chance of your vars values getting changed without u knowing about it.

If you relly need it you could use a mutable integer class
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thnx, k.
for me, at least the point must be splited.
:-(