Pin It

Java Long Class : Fields and Methods Usage

Program to demonstrate Java Long Class : fields and methods usage in Java

package com.hubberspot.example;

public class LongDemo {

   public static void main(String[] args) {

 System.out.println("------------------------------------------");
 System.out.println("Long's Max Value : " + Long.MAX_VALUE);
 System.out.println("Long's Min Value : " + Long.MIN_VALUE);
 System.out.println("Long's Size : " + Long.SIZE);
 System.out.println("Long's Type : " + Long.TYPE);
 System.out.println("------------------------------------------");

 Long long1 = new Long(10);
 Long long2 = new Long("9");

 System.out.println(long1);
 System.out.println(long2);
 System.out.println("------------------------------------------");
 System.out.println("Method *primitiveValue() Testing : ");

 Long long4 = new Long(145367);
 byte b   = long4.byteValue();
 short s  = long4.shortValue();
 int i    = long4.intValue();
 long l   = long4.longValue();
 float f  = long4.floatValue();
 double d = long4.doubleValue();

 System.out.println("byte : " + b );
 System.out.println("short : " + s);
 System.out.println("int : " + i);
 System.out.println("long : " + l);
 System.out.println("float : " + f);
 System.out.println("double : " + d);

 System.out.println("------------------------------------------");
 System.out.println("Method compareTo Testing : ");

 long1 = new Long(1);
 long2 = new Long(2);
 Long long3 = new Long(1);

 System.out.println(long1.compareTo(long3));
 System.out.println(long1.compareTo(long2));
 System.out.println(long2.compareTo(long3));

 System.out.println("------------------------------------------");
 System.out.println("Method equals() Testing : ");

 System.out.println(long1.equals(long3));
 System.out.println(long1.equals(long2));

 System.out.println("------------------------------------------");

 System.out.println("Method decode() Testing : ");
 String decoder = "0x11";
 System.out.println(Long.decode(decoder));

 decoder = "011";
 System.out.println(Long.decode(decoder));

 System.out.println("------------------------------------------");

 System.out.println("Method parseLong() Testing : ");
 System.out.println(Long.parseLong(decoder));
 System.out.println(Long.parseLong(decoder, 8));

 System.out.println("------------------------------------------");
 System.out.println("Method valueOf() Testing : ");

 System.out.println(Long.valueOf(10));
 System.out.println(Long.valueOf("3"));
 System.out.println(Long.valueOf("100", 2));

 System.out.println("------------------------------------------");
 System.out.println("Method toString() Testing : ");

 System.out.println(long4.toString());
 System.out.println(Long.toString(10));
 System.out.println(Long.toString(10 , 16));

 System.out.println("------------------------------------------");
 System.out.println("Method reverse() and rotate() Testing : ");

 System.out.println(Long.reverse(10));
 System.out.println(Long.reverseBytes(10));
 System.out.println(Long.rotateLeft(100, 2));
 System.out.println(Long.rotateRight(100, 2));

 System.out.println("------------------------------------------");
 System.out.println("Method bitCount() , highestOneBit() and " +
   "lowestOneBit() Testing : ");

 System.out.println(Long.bitCount(3));
 System.out.println(Long.highestOneBit(3));
 System.out.println(Long.lowestOneBit(3));

 System.out.println("------------------------------------------");
 System.out.println("Method getLong() Testing : ");

 System.setProperty("Key","10");
 System.out.println(Long.getLong("Key"));
 System.setProperty("Key","20");
 System.out.println(Long.getLong("Key",10));

 System.out.println("------------------------------------------");
 System.out.println("Method numberOfLeadingZeros(),numberOfTrailingZeros() Testing : ");

 System.out.println(Long.numberOfLeadingZeros(5));
 System.out.println(Long.numberOfTrailingZeros(30));

 System.out.println("------------------------------------------");
 System.out.println("Method signum() Testing : ");

 System.out.println(Long.signum(9));
 System.out.println(Long.signum(0));
 System.out.println(Long.signum(-9));

 System.out.println("------------------------------------------");
 System.out.println("Method toBinaryString() , toHexString() and toOctalString() Testing : ");

 System.out.println(Long.toBinaryString(15));
 System.out.println(Long.toHexString(15));
 System.out.println(Long.toOctalString(15));

 System.out.println("------------------------------------------");
   }

}




Output of the program :





Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot