- Integer class is present in java.lang package.
- java.lang.Integer is used to represent integer primitive value in form of object.
Class Definition
- public final class Integer
- extends Number
- 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
Output:
2.public Integer(String s) throws NumberFormatException:- package com.instanceofjavatutorial;
- public class IntegerDemo {
- public static void main(String[] args) {
- Integer obj=new Integer (37);
- System.out.println(obj);
- }
- }
Output:
- 37
- It takes String value as an argument.
Java code to convert String to Integer object
- package com.instanceofjavatutorial;
- public class IntegerDemo {
- public static void main(String[] args) {
- Integer obj=new Integer ("123");
- System.out.println(obj);
- }
- }
Output:
- 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
- package com.instanceofjavatutorial;
- public class IntegerParseIntDemo {
- public static void main(String[] args) {
- int x= Integer.parseInt("34");
- System.out.println(x);
- int y= Integer.parseInt("56");
- System.out.println(y);
- int z= Integer.parseInt("98");
- System.out.println(z);
- }
- }
Output:
- 34
- 56
- 98
- 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.
- package com.instanceofjavatutorial;
- public class IntegerIntValueDemo {
- public static void main(String[] args) {
- Integer obj= new Integer(25);
- int y= obj.intValue();
- System.out.println(y);
- }
- }
Output:
- 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.
- package com.instanceofjavatutorial;
- public class IntegercompareToDemo {
- public static void main(String[] args) {
- Integer obj1 = new Integer("34");
- Integer obj2 = new Integer("08");
- int val = obj1.compareTo(obj2);
- if(val > 0) {
- System.out.println("obj1 is greater than obj2");
- }
- else if(val < 0) {
- System.out.println("obj1 is less than obj2");
- }
- else {
- System.out.println("obj1 is equal to obj2");
- }
- }
- }
Output:
- 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
- package com.instanceofjavatutorial;
- public class IntegerfloatValueDemo {
- public static void main(String[] args) {
- Integer obj1 = new Integer("34");
- Integer obj2 = new Integer("23");
- float f 1= obj1.floatValue();
- float f 2= obj2.floatValue();
- System.out.println(f1);
- System.out.println(f2);
- }
- }
Output:
- 34.0
- 23.0
5.public String toString()
- This method used to convert Integer to string object.
Java Program to convert Integer to String Object.
- package com.instanceofjavatutorial;
- public class IntegerfloatValueDemo {
- public static void main(String[] args) {
- Integer obj1 = new Integer("12");
- Integer obj2 = new Integer("34");
- String str1= obj1.toString();
- String f 2= obj2.toString();
- System.out.println(str1);
- System.out.println(str2);
- }
- }
Output:
- 12
- 34
No comments