Long Wrapper Class:
- Long Class is presented in java.lang package.
- java.lang.Long class is used to represent primitive Long Value to Long object.
Long Class :
Float Class Definition
- public final class Long
- extends Number
- implements Comparable<Long>
Constructors:
1.Long(long value):- The Constructor Long(long value) represents the specified long argument.
2.Long(String s):
- The Constructor Long (String s) represents the long value indicated by the String parameter.
Methods:
1.public static int bitCount(long i); Program:- package com.instanceofjava;
- import java.lang.*;
- public class LongExample {
- public static void main(String[] args) {
- long l = 4561;
- System.out.println("Number = " + l);
- System.out.println("Binary = " + Long.toBinaryString(l));
- System.out.println("Number of one bits = " + Long.bitCount(l));
- }
- }
Output:
- Number = 4561
- Binary = 1000111010001
- Number of one bits = 6
2.public byte byteValue():
This method returns value of long as byte.Program:
- package com.instanceofjava;
- import java.lang.*;
- public class LongExample {
- public static void main(String[] args) {
- Long obj = new Long(30);
- byte b = obj.byteValue();
- System.out.println("Value of b = " + b);
- }
- }
Output:
- Value of b = 30
3.public int compareTo(Long anotherLong):
Program:
- package com.instanceofjava;
- import java.lang.*;
- public class LongExample {
- public static void main(String[] args) {
- Long a = new Long(63255);
- Long b = new Long(71678);
- int result= a.compareTo(b);
- ifresult> 0) {
- System.out.println("a is greater than b");
- }
- else ifresult< 0) {
- System.out.println("a is less than b");
- }
- else {
- System.out.println("a is equal to b");
- }
- }
- }
Output:
- a is less than b
4.public static Long decode(String nm) throws NumberFormatException:
Program:
- package com.instanceofjava;
- import java.lang.*;
- public class LongExample{
- public static void main(String[] args) {
- Long l = new Long(10);
- String str = "57820";
- System.out.println("Number = " + l.decode(str));
- }
- }
Output:
- 57820
No comments