Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is the difference between an int and an Integer in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 161
    Comment on it

    Well, in Java technology an int is a primitive type while an Integer is an Object or wrapper class. Meaning, if you made a new Integer:

    Integer integerObj = new Integer(6);
    

    You could call some method on integerObj:

    String str = integerObj.toString();
    //sets str the string representation of  integerObj
    

    Whereas with an int:

    int i = 6;
    

    You can not call any methods on it, because it is simply a primitive. So:

    String s = i.toString();//will not work!!!
    

    would produce an error, because int is not an object.

    int is one of the primitive type in Java technology. I'm not 100% sure, but I'm thinking that the Integer object more or less just has an int property and a whole bunch of methods to interact with that property (like the toString() method for example). So Integer is a fancy way to work with an int.

    Below Example help you to better understand :

    package bhagwan;
    public class Findnerd{ 
         public static void main(String [] args)  
          {         
           Integer integerObj = new Integer(6);
           System.out.println(integerObj value+integerObj.toString());    
            int i = 6;
            System.out.println(int i value+i);  
          }
     }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: