Java Integer longValue() method
The longValue() method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return Value This method returns the numeric value of this object after conversion to type long. Example 1
public class JavaIntegerLongValueExample1 { public static void main(String[] args) { Integer b1=98; Integer b2=102; //returns the numeric value of this object after conversion to type long. Long val=b1.longValue(); System.out.println("1. Long value : "+val); System.out.println("2. Long value : "+b2.intValue()); } }Output
1. Long value : 98 2. Long value : 102Example 2
public class JavaIntegerLongValueExample2 { public static void main(String[] args) { Integer val1=98; Integer val2=98; //returns the numeric value of this object after conversion to type long. Long l1=val1.longValue(); Long l2=val2.longValue(); //calling long class methods boolean val=l1.equals(l2); if(val){ System.out.println("Both values are equal"); } else{ System.out.println("Both values are different"); } } }Output
Both values are equalExample 3
public class JavaIntegerLongValueExample3 { public static void main(String[] args) { Integer val1=20; //converting the Integer value to long Long l1=val1.longValue(); Integer val2=-9; Long l2=val2.longValue(); //calling long class methods System.out.println("Sum : "); System.out.print(val1+" + ("+val2+") : "); System.out.println(Long.sum(l1,l2)); } }Output
Sum : 20 + (-9) : 11