Long Wrapper Class

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

  1. public final class Long
  2. extends Number 
  3. 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:

  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class LongExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8.   long l = 4561; 

  9.  System.out.println("Number = " + l);
  10.  System.out.println("Binary = " + Long.toBinaryString(l));
  11.  System.out.println("Number of one bits = " + Long.bitCount(l));
  12.  
  13. }
  14. }

Output:
  1. Number = 4561 
  2. Binary = 1000111010001 
  3. Number of one bits = 6

2.public byte byteValue():

This method returns value of long as byte.

Program:

 

  1. package com.instanceofjava;
  2.  import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {

  5.  Long obj = new Long(30);

  6.  byte b = obj.byteValue();
  7.  System.out.println("Value of b = " + b);
  8.  
  9.  }
  10. }


Output:
  1. Value of b = 30

3.public int compareTo(Long anotherLong):

Program:

  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {
  5.  
  6.    Long a = new Long(63255);
  7.   Long b = new Long(71678);
  8.  
  9.    int result=  a.compareTo(b);
  10.    ifresult> 0) {
  11.    System.out.println("a is greater than b");
  12.    }
  13.    else ifresult< 0) {
  14.    System.out.println("a is less than b");
  15.    }
  16.    else {
  17.    System.out.println("a is equal to b");
  18.    }
  19.  
  20.    }
  21. }




Output:
  1. a is less than b

4.public static Long decode(String nm) throws NumberFormatException:

Program:


  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample{

  4.  public static void main(String[] args) {

  5.    Long l = new Long(10);
  6.    String str = "57820";
  7.    System.out.println("Number = " + l.decode(str));
  8.  
  9.   }
  10. }

Output:
  1. 57820




Float Wrapper Class

  • Float class is presented in java.lang package
  • java.lang.Float class is used to represent primitive float value to Float object.

Float Class Definition

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

Float Class Constructors

1.public Float(double value)


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

  7.  Float f = new Float(22.56d);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


2.public Float(float value)


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

  7.  Float f = new Float(22.56f);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


3.public Float(String s) throws NumberFormatException


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

  7.  Float f = new Float("22.56f");
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56

Float Class Methods:

1.public static Float valueOf(String s) throws NumberFormatException 

  • This method used to convert string value to float value. If the string contains non parsable value then it throws NumberFormatException 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="56.32";

  8.  Float f = Float.valueOf(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 56.32

2.public String toString()
  • This method returns String value from Float Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.   
  7.  Float f = new Float("12.3")
  8.  String str=f.toString();

  9.   System.out.println(str);
  10.  
  11.  
  12. }
  13. }



Output:
  1. 12.3

3.public static float parseFloat(String s) throws NumberFormatException

  • This method returns float value from string object. If string doesnt contains parsable float value then throws NumberFormatException 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="20";

  8.  Float f = Float.parseFloat(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 20.0



Character Class

  • Character class is presented in java.lang package
  • java.lang.Character class is used to represent primitive character value to character object.

Character Class Definition:

  1. public final class Character
  2.  extends Object 
  3. implements Serializable, Comparable<Character>

Character Class Constructors:

1. public Character(char value)
  • Character class having only one constructor which accepts char primitive value.

Java Program to convert char primitive value to Character Object.

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

  7.  Character ch = new Character ('a');
  8.   System.out.println(ch);
  9.  
  10.  
  11. }
  12. }

Output:
  1. a

 Character Class Methods:

1.public char charValue()

  • This method returns the primitive char value from the Character object.

Java code to get char primitive value from Character Object.

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

  7.  Character ch = new Character ('a');
  8.   char c=ch.charValue();

  9.  System.out.println(c);
  10.  
  11. }
  12. }

Output:
  1. a

2.public static String toString(char c) :

  • toString(char c) method present in Character class accepts "char" primitive values and returns String objects representing that character.
Java code to convert char primitive value to String object

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

  6.   char ch ='I';
  7.  
  8.   String str="";
  9.  
  10.   str=Character.toString(ch);
  11.  
  12.   System.out.println(str);
  13. }
  14. }

Output:
  1. I

3.public static Character valueOf(char c)

  • valueOf(char c)  is static method present in Character class used to convert char value to Chraracter object.
  • It accepts char primitive value and returns Character object

Java code to convert char primitive value to String object

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

  6.   char ch ='S';
  7.  
  8.   Character chobj='';
  9.  
  10.   chobj=Character.valueOf(ch);
  11.  
  12.   System.out.println(chobj);
  13. }
  14. }

Output:
  1. S
4.public static char toUpperCase(char ch)

  • toUpperCase(char ch) static method present in Character class used to convert a character to uppercase.
  • It accepts a character and returns same character in uppercase.


Java code to convert char primitive value to uppercase

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

  6.   char ch ='a';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }



Output:
  1. A

5.public static char toLowerCase(char ch) 

  •  toLowerCase(char ch) static method present in Character class used to convert a character to lowercase.
  • It accepts a character and returns same character in lowercase.


Java code to convert char primitive value to lowercase

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

  6.   char ch ='L';
  7.  
  8.   char c=Character.valueOf(ch);
  9.  
  10.   System.out.println(c);
  11. }
  12. }

Output:
  1. l



Integer Wrapper class

  • 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

Byte Wrapper Class

Byte Wrapper class Programs:


  • The java.lang.Byte class is used represent byte primitive value in form of Byte Object

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

Byte Class Constructors:


1.Byte(byte value)
2.Byte(String s)

Byte Class Methods:

  • public byte byteValue() returns the value of this Byte as a byte.

Program:

  1. package com.instanceofjavatutorial;

  2. import java.lang.*; 

  3. public class ByteClass {

  4. public static void main(String[] args) {

  5.       Byte a;
  6.       a = new Byte("100");

  7.       byte t;
  8.       t = a.byteValue();

  9.       String s= "byte value of Byte object " + a + " is " + t;

  10.       System.out.println( s);

  11.    }
  12. }


Output:
  1. byte value of Byte object 100 is 100












Boolean wrapper class example programs

  • java.lang.Boolean Class is used to represent primitive boolean value in the form of object.

  1.  public final class Boolean
  2.    extends Object
  3.       implements Serializable, Comparable<Boolean>
     

Boolean Class Constructors:

1. public Boolean(boolean value)

  1. Boolean obj=new Boolean(true);

Java code to convert boolean primitive to Boolean object


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


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

Output:
  1. true

2. public Boolean(String s)

  1. Boolean b=new Boolean("true");

Java Program to Convert String Object to Boolean Object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="true";

  8.  Boolean obj=new Boolean(str);
  9.  
  10.  System.out.println(obj);
  11.  
  12.  
  13. }
  14. }

Output:
  1. true

Boolean class Methods:

1.public boolean booleanValue()

  • This method gives the boolean primitive value from Boolean object
  • See below java code which explains how to get boolean primitive value and assig to primitive variable from Boolean Object.

Java Program To Convert boolean primitive value to Boolean Object and To get boolean value from Boolean Object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. // converting boolean variable in to Boolean Object
  8.  Boolean bobj=new Boolean(true);
  9.  
  10.  System.out.println(bobj);
  11.  
  12. //converting Bolean object into boolean primitive value
  13.  boolean bval=bobj.booleanValue();
  14.  
  15.  System.out.println(bval);
  16.  
  17. }
  18. }

Output:
  1. true
  2. true
Select Menu