• Integer  class is present in java.lang package.
  • java.lang.Integer  is used to represent integer primitive value in form of object.

Class Definition

  1. public final class Integer
  2. extends Number 
  3. implements Comparable<Integer>

Integer Class Constructors

1.public Integer(int value):
  • It takes int primitive value as an argument.
Java code to convert int primitive to Integer object


  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerDemo {
  4.  
  5. public static void main(String[] args) {
  6.  


  7.  Integer obj=new Integer (37);
  8.  
  9.  System.out.println(obj);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 37

2.public Integer(String s)  throws NumberFormatException:

  • It takes String value as an argument. 

Java code to convert String to Integer object


  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerDemo {
  4.  
  5. public static void main(String[] args) {
  6.  


  7.  Integer obj=new Integer ("123");
  8.  
  9.  System.out.println(obj);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 123

Integer Class Methods:

3.public static int parseInt(String s) throws NumberFormatException :
  • This method takes String as an argument and converts to the int value
  • Static method present in Integer Class used to parse the string to the corresponding int value.in
  • We can use this method by class name Integer.parseInt("number").
  • If the String contains non numeric value then this method throws NumberFormatException 

 Java Program to parse a string or convert String to Integer value

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerParseIntDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  int x= Integer.parseInt("34");
  8.   System.out.println(x);
  9.  
  10.  int y=  Integer.parseInt("56");
  11.    System.out.println(y);
  12.  
  13.  int z=  Integer.parseInt("98");
  14.  System.out.println(z);
  15.  
  16. }
  17. }

Output:
  1. 34
  2. 56
  3. 98
4.public int intValue()

  • This method returns the int value from the Integer Object.
  • obj.intValue()- returns a int value of Object obj.



Java Program to get int value from a Integer Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerIntValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Integer obj= new Integer(25);

  8.  
  9.  int y= obj.intValue();
  10.  
  11.  System.out.println(y);
  12.  
  13. }
  14. }

Output:
  1. 25

3.public int compareTo(Integer anotherInteger) 

  •  This method is used to compare two Integer Objects and returns int value
  •  0- Integer is equal to the argument Integer
  • < 0 if this Integer is numerically less than the argument Integer
  • > 0 if this Integer is numerically greater than the argument

Java Program to compare two Integer Objects.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegercompareToDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("34");
  8.    Integer obj2 = new Integer("08");
  9.  
  10.   int val =  obj1.compareTo(obj2);
  11.  
  12.    if(val > 0) {
  13.  
  14.    System.out.println("obj1 is greater than obj2");
  15.  
  16.    }
  17.    else if(val < 0) {
  18.  
  19.    System.out.println("obj1 is less than obj2");
  20.  
  21.    }
  22.    else {
  23.  
  24.    System.out.println("obj1 is equal to obj2");
  25.  
  26.    }
  27. }
  28. }

Output:
  1. obj1 is greater than obj2


4.public float floatValue()

  • This method used to convert integer value to float value.

Java Program to convert Integer to float

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerfloatValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("34");
  8.    Integer obj2 = new Integer("23");
  9.    
  10.     float f 1= obj1.floatValue();
  11.     float f 2= obj2.floatValue();
  12.  
  13.    System.out.println(f1);
  14.    System.out.println(f2); 

  15. }
  16. }

Output:
  1. 34.0
  2. 23.0

5.public String toString()

  • This method used to convert Integer to string object.

Java Program to convert Integer to String Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class IntegerfloatValueDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.    Integer obj1 = new Integer("12");
  8.    Integer obj2 = new Integer("34");
  9.    
  10.     String  str1= obj1.toString();
  11.     String  f 2= obj2.toString();
  12.  
  13.    System.out.println(str1);
  14.    System.out.println(str2); 

  15. }
  16. }

Output:
  1. 12
  2. 34

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu