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
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
read this:
http://mindprod.com/jgloss/intvsinteger.html

in short:
int is a primitive
Integer is a object
0
sciuriwareCommented:
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!
0
ysnkyCommented:
>>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
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

cmalakarCommented:
>>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
0
krakatoaCommented:
Java always passes arguments by value. cf:
http://java.sun.com/developer/JDCTechTips/2001/tt1009.html

0
krakatoaCommented:
0
ysnkyCommented:
>>I think its opposite.
have a look at the link i posted above.
0
krakatoaCommented:
dupe post, me and ysnky - link is pale. ;)
0
DJ_AM_JuiceboxAuthor Commented:
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
0
krakatoaCommented:
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.
0
Mick BarryJava DeveloperCommented:
> 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
0
krakatoaCommented:
This *may* help ....

 public class ReforVal{


 int a =1,b=2,c=3,d =4;
 static boolean yesno = false;

 public static void main(String [] args){

 new ReforVal();

}

 public ReforVal(){
      
      System.out.println("The constructor thinks int a value is "+a+" before any method calls");
      System.out.println("The constructor thinks int a value is "+changeint(a,yesno)+" after it returns from a call to changeint");
      a=changeint(a, yesno);
      System.out.println("The constructor thinks int a value is "+a+" after re-allocating a to the return value from changeint()");
      Integer B = new Integer(b);
      yesno=false;
      System.out.println("The constructor thinks Integer B value is "+changeInteger(B)+" after a call to changeInteger");
      B=changeInteger(B);
      System.out.println("The constructor thinks value of B is "+B.intValue());
 }


  public int changeint( int inte, boolean yesorno){

      if(!yesorno){
            System.out.println("The method changeint() thinks that a b c d values are "+a+" "+b+" "+c+" "+d);

            System.out.println("The method changeint() thinks the local inte is now "+inte);

            System.out.println("The method changeint() thinks the incremented inte value is now "+inte++);

            yesno =(!yesorno);

            return inte;
      }

      if (yesno){
            return inte++;
      }

      return inte;
}
 


  public Integer changeInteger(Integer INTe){

      if(!yesno){
            System.out.println("The method changeInteger() thinks Integer INTe value before any changes is = "+INTe.intValue());
      yesno= !yesno;
      }

      int incomingINT = INTe.intValue();

      incomingINT = incomingINT+3;

      INTe = new Integer(incomingINT);

                return INTe;

   }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
Thnx, k.
0
ysnkyCommented:
for me, at least the point must be splited.
:-(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.