KEMBAR78
Wrapper class | PPS
Chapter 10



Wrapper Class



                http://www.java2all.com
Introduction



               http://www.java2all.com
Java uses primitive types, such as int, char,
double to hold the basic data types supported by
the language.

   Sometimes it is required to create an object
representation of these primitive types.

    These are collection classes that deal only with
such objects. One needs to wrap the primitive type
in a class.


                                           http://www.java2all.com
To satisfy this need, java provides classes that
correspond to each of the primitive types. Basically,
these classes encapsulate, or wrap, the primitive types
within a class.
      Thus, they are commonly referred to as type
wrapper. Type wrapper are classes that encapsulate a
primitive type within an object.

    The wrapper types are Byte, Short, Integer,
Long, Character, Boolean, Double, Float.

     These classes offer a wide array of methods that
allow to fully integrate the primitive types into Java’s
object hierarchy.                              http://www.java2all.com
Wrapper classes for converting simple types


      Simple Type               Wrapper class

boolean             Boolean
char                Character
double              Double
float               Float
int                 Integer
long                Long



                                                http://www.java2all.com
Converting primitive numbers to Object
numbers using constructor methods
    Constructor
                                 Conversion Action
       calling
Integer IntVal =
                   Primitive integer to Integer object
new Integer(i);
Float FloatVal =
                   Primitive float to Float object
new Float(f);
Double DoubleVal
                   Primitive double to Double object
= new Double(d);
Long LongVal =
                   Primitive long to Long object
new Long(l);


                                                     http://www.java2all.com
Converting Numeric Strings to Primitive
numbers using Parsing method


 Method calling                    Conversion Action

int i =
Integer.parseInt(st Converts String str into primitive integer i
r);

long l =
Long.parseLong(st Converts String str into primitive long l
r);


                                                        http://www.java2all.com
NOTE :
           parseInt() and parseLong() methods throw
a NumberFormatException if the value of the str
does not represent an integer.




                                          http://www.java2all.com
Byte



       http://www.java2all.com
The Byte class encapsulates a byte value. It
defines the constants MAX_VALUE and
MIN_VALUE and provides these constructors:

   Byte(byte b)
   Byte(String str)

     Here, b is a byte value and str is the string
equivalent of a byte value.



                                               http://www.java2all.com
EX :
import java.util.*;
public class Byte_Demo
{
  public static void main(String args[])
  {
    Byte b1 = new Byte((byte)120);
    for(int i = 125; i<=135; i++)
    {
       Byte b2 = new Byte((byte)i);
       System.out.println("b2 = " + b2);
    }
    System.out.println("b1 Object = " + b1);
    System.out.println("Minimum Value of Byte = " + Byte.MIN_VALUE);
    System.out.println("Maximum Value of Byte = " + Byte.MAX_VALUE);
    System.out.println("b1* 2 = " + b1*2);
    System.out.println("b1* 2 = " + b1.byteValue()*2);
    Byte b3 = new Byte("120");
    System.out.println("b3 Object = " + b3);
    System.out.println("(b1==b3)? " + b1.equals(b3));
    System.out.println("(b1.compareTo(b3)? " + b1.compareTo(b3));
     /*Returns 0 if equal. Returns -1 if b1 is less than b3 and 1 if b1 is
    greater than 1*/
  }
}
                                                                             http://www.java2all.com
Output :
 b2 = 125
b2 = 126
b2 = 127
b2 = -128
b2 = -127
b2 = -126
b2 = -125
b2 = -124
b2 = -123
b2 = -122
b2 = -121
b1 Object = 120
Minimum Value of Byte = -128
Maximum Value of Byte = 127
b1* 2 = 240
b1* 2 = 240
b3 Object = 120
(b1==b3)? true
(b1.compareTo(b3)? 0
                               http://www.java2all.com
Short



        http://www.java2all.com
The Short class encapsulates a short value.
It defines the constants MAX_VALUE and MIN_VALUE
and provides the following constructors:


           Short(short s)
           Short(String str)



                                             http://www.java2all.com
EX :


import java.util.*;
public class Short_Demo
{
  public static void main(String args[])
  {
    Short s1 = new Short((short)2345);
    for(int i = 32765; i<=32775; i++)
    {
       Short s2 = new Short((short)i);
       System.out.println("s2 = " + s2);
    }
    System.out.println("s1 Object = " + s1);
    System.out.println("Minimum Value of Short = " + Short.MIN_VALUE);
    System.out.println("Maximum Value of Short = " + Short.MAX_VALUE);
    System.out.println("s1* 2 = " + s1.shortValue()*2);
    Short s3 = new Short("2345");
    System.out.println("s3 Object = " + s3);
    System.out.println("(s1==s3)? " + s1.equals(s3));
    Short s4 = Short.valueOf("10", 16);
    System.out.println("s4 Object = " + s4);
  }
}


                                                                         http://www.java2all.com
Output :
s2 = 32765
s2 = 32766
s2 = 32767
s2 = -32768
s2 = -32767
s2 = -32766
s2 = -32765
s2 = -32764
s2 = -32763
s2 = -32762
s2 = -32761
s1 Object = 2345
Minimum Value of Short = -32768
Maximum Value of Short = 32767
s1* 2 = 4690
s3 Object = 2345
(s1==s3)? true
s4 Object = 16                    http://www.java2all.com
Integer



          http://www.java2all.com
Integer class :

     The Integer class encapsulates an integer value.
This class provides following constructors:

     Integer(int i)
     Integer(String str)

     Here, i is a simple int value and str is a String
object.



                                               http://www.java2all.com
EX :
import java.util.*;
public class Int_Demo
{
  public static void main(String args[])
  {
    Integer i1 = new Integer(12);
    System.out.println("I1 = " + i1);
    System.out.println("Binary Equivalent = " + Integer.toBinaryString(i1));
    System.out.println("Hexadecimal Equivalent = " + Integer.toHexString(i1));
    System.out.println("Minimum Value of Integer = " + Integer.MIN_VALUE);
    System.out.println("Maximum Value of Integer = " + Integer.MAX_VALUE);
    System.out.println("Byte Value of Integer = " + i1.byteValue());
    System.out.println("Double Value of Integer = " + i1.doubleValue());
    Integer i2 = new Integer(12);
    System.out.println("i1==i2 " + i1.equals(i2));
    System.out.println("i1.compareTo(i2) = " + i2.compareTo(i1));
    // Compareto - if it is less than it returns -1 else 1, if equal it return 0.
    Integer i3 = Integer.valueOf("11", 16);
    System.out.println("i3 = " + i3);
  }
}




                                                                                    http://www.java2all.com
Output :

I1 = 12
Binary Equivalent = 1100
Hexadecimal Equivalent = c
Minimum Value of Integer = -2147483648
Maximum Value of Integer = 2147483647
Byte Value of Integer = 12
Double Value of Integer = 12.0
i1==i2 true
i1.compareTo(i2) = 0
i3 = 17



                                         http://www.java2all.com
Long



       http://www.java2all.com
Long :

     The Long class encapsulates a long value. It
defines the constants MAX_VALUE and
MIN_VALUE and provides the following
constructors:

     Long(long l)
     Long(String str)


                                            http://www.java2all.com
EX :
import java.util.*;
 public class Long_Demo
{
  public static void main(String args[])
  {
    Long L1 = new Long(68764);
    Long L2 = new Long("686748");
    System.out.println("Object L1 = " + L1);
    System.out.println("Object L2 = " + L2);
    System.out.println("Minimum Value of Long = " + Long.MIN_VALUE);
    System.out.println("Maximum Value of Long = " + Long.MAX_VALUE);
    System.out.println("L1 * 2 = " + L1 * 2);
    System.out.println("L1.longValue() * 2 = " + L1.longValue() * 2);
    System.out.println("L1.compareTo(l2) = " + L1.compareTo(L2));
    System.out.println("L1==L2 ? = " + L1.equals(L2));
    Long L3 = Long.valueOf("10", 16);
    System.out.println("Object L3 = " + L3);
    System.out.println("Byte value of Long = " + L1.byteValue());
    System.out.println("int value of Long = " + L1.intValue());
    System.out.println("Double value of Long = " + L1.doubleValue());
    int i = 12;
    System.out.println("Binary equivalent of decimal " + i + "=" + Long.toBinaryString(i));
    System.out.println("Hexadecimal equivalent of decimal " + i + "=" + Long.toHexString(i));

    }
}
                                                                                 http://www.java2all.com
Output :

Object L1 = 68764
Object L2 = 686748
Minimum Value of Long = -9223372036854775808
Maximum Value of Long = 9223372036854775807
L1 * 2 = 137528
L1.longValue() * 2 = 137528
L1.compareTo(l2) = -1
L1==L2 ? = false
Object L3 = 16
Byte value of Long = -100
int value of Long = 68764
Double value of Long = 68764.0
Binary equivalent of decimal 12=1100
Hexadecimal equivalent of decimal 12=c
                                               http://www.java2all.com
Double



         http://www.java2all.com
Double class :

     The Double class encapsulates a double value. It
defines several constants.

   The largest and smallest values are saved in
MAX_VALUE and MIN_VALUE.

      The constant NaN (Not a Number) indicates that
a value is not a number.

                                           http://www.java2all.com
If you divide a double number by zero, the result
is NaN. This class defines these constructors:

     Double(double d)
     Double(String str)

     Here, d is a double value to be encapsulated in a
Double object. In the last form, str is the string
representation of a double value.




                                            http://www.java2all.com
EX :

import java.util.*;
class Double_Demo
{
   public static void main(String args[])
   {
     Double d1 = new Double(687642365.4563);
     Double d2 = new Double("686748");
     System.out.println("Object d1 = " + d1);
     System.out.println("Object d2 = " + d2);
     System.out.println("Minimum Value of Double = " + Double.MIN_VALUE);
     System.out.println("Maximum Value of Double = " + Double.MAX_VALUE);
     System.out.println("Byte value of Double = " + d1.byteValue());
     System.out.println("int value of Double = " + d1.intValue());
     System.out.println("Float value of Double = " + d1.floatValue());
     Double d3 = Double.parseDouble("765.89");
     System.out.println("Double value from the string "765.89"="+d3);

    }
}




                                                                            http://www.java2all.com
Output :

Object d1 = 6.876423654563E8
Object d2 = 686748.0
Minimum Value of Double = 4.9E-324
Maximum Value of Double = 1.7976931348623157E308
Byte value of Double = -3
int value of Double = 687642365
Float value of Double = 6.8764237E8
Double value from the string "765.89"=765.89




                                               http://www.java2all.com
Float



        http://www.java2all.com
Float class :
      The float class encapsulates a float value.
It defines several constants the largest and smallest
values are stored in MAX_VALUE and
MIN_VALUE.

      The constant NaN indicates that a value is not a
number. If you divide a floating – point number by
zero, the result is NaN.


                                              http://www.java2all.com
This class defines these constructors:

    Float(float f)
    Float(double d)
    Float(String str)

     Here, f and d are float and double types to be
encapsulated in a Float object.

str is the string representation of a float value.



                                                http://www.java2all.com
EX :

import java.util.*;

public class Float_Demo
{
  public static void main(String args[])
  {
    Float f1 = new Float(123.5626);
    Float f2 = new Float(854.32f);
    Float i = new Float(10);
    System.out.println("Object f1 = " + f1);
    System.out.println("Object f2 = " + f2);
    System.out.println("Minimum Value of Float = " + Float.MIN_VALUE);
    System.out.println("Maximum Value of Float = " + Float.MAX_VALUE);
    System.out.println("Byte value of Float = " + f1.byteValue());
    System.out.println("Short value of Float = " + f1.shortValue());
    System.out.println("Integer value of Float = " + f1.intValue());
    System.out.println("Double value of Float = " + f1.doubleValue());
    System.out.println("(f1==f2) ?= " + f1.equals(f2));
    System.out.println("f1.compareTo(f2) = " + f1.compareTo(f2));
    System.out.println("f1 is not a number = " + i.isNaN());

    }
}

                                                                         http://www.java2all.com
Output :

Object f1 = 123.5626
Object f2 = 854.32
Minimum Value of Float = 1.4E-45
Maximum Value of Float = 3.4028235E38
Byte value of Float = 123
Short value of Float = 123
Integer value of Float = 123
Double value of Float = 123.5625991821289
(f1==f2) ?= false
f1.compareTo(f2) = -1
f1 is not a number = false

                                            http://www.java2all.com
Character



            http://www.java2all.com
Character class :
     The Character class encapsulates a char value.
This class provides the following constructor.

     Character(char ch)

      Here, c is a char value. charValue() method
returns the char value that is encapsulated by a
Character object and has the following form:

char charValue()
                                            http://www.java2all.com
EX :

import java.util.*;

public class Char_Demo
{
  public static void main(String args[])
  {
    Character c1 = new Character('m');
    char c2 = 'O';
    System.out.println("Object C1 = " + c1);
    System.out.println("char value of Character Object = " + c1.charValue());
    System.out.println(c2 + " is defined character set ? " + Character.isDefined(c2));
    System.out.println("c2 is digit = " + Character.isDigit(c2));
    System.out.println("c2 is lowercase character = " + Character.isLowerCase(c2));
    System.out.println("c2 is uppercase character = " + Character.isUpperCase(c2));


    }
}




                                                                                    http://www.java2all.com
Output :

Object C1 = m
char value of Character Object = m
O is defined character set ? true
c2 is digit = false
c2 is lowercase character = false
c2 is uppercase character = true




                                     http://www.java2all.com
Boolean



          http://www.java2all.com
Boolean class :

      The Boolean class encapsulates a Boolean value.
It defines FALSE and TRUE constants.

     This class provides following constructors:

     Boolean(Boolean b)
     Boolean(String str)

     Here, b is a Boolean value and str is the string
equivalent of a Boolean value.
                                              http://www.java2all.com
The methods associated with Boolean Class
are as follows:


    1. Boolean booleanValue()
    2. Boolean equals(Boolean b)
    3. String toString(Boolean b)




                                       http://www.java2all.com
EX :

import java.util.*;

public class Boolean_Demo
{
  public static void main(String args[])
  {
    Boolean b1 = new Boolean(true);
    Boolean b2 = new Boolean(false);
    System.out.println("Object B1 = " + b1);
    System.out.println("Object B2 = " + b2);
    Boolean b3 = new Boolean("true");
    Boolean b4 = new Boolean("false");
    System.out.println("Object B3 = " + b3);
    System.out.println("Object B4 = " + b4);
    System.out.println("Boolean Value = " + b1.booleanValue());
    System.out.println("(b1==b2)? " + b1.equals(b2));
    String S1 = b1.toString();
    System.out.println("String S1 " + S1);

    }
}



                                                                  http://www.java2all.com
Output :

Object B1 = true
Object B2 = false
Object B3 = true
Object B4 = false
Boolean Value = true
(b1==b2)? false
String S1 true




                       http://www.java2all.com

Wrapper class

  • 1.
    Chapter 10 Wrapper Class http://www.java2all.com
  • 2.
    Introduction http://www.java2all.com
  • 3.
    Java uses primitivetypes, such as int, char, double to hold the basic data types supported by the language. Sometimes it is required to create an object representation of these primitive types. These are collection classes that deal only with such objects. One needs to wrap the primitive type in a class. http://www.java2all.com
  • 4.
    To satisfy thisneed, java provides classes that correspond to each of the primitive types. Basically, these classes encapsulate, or wrap, the primitive types within a class. Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object. The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float. These classes offer a wide array of methods that allow to fully integrate the primitive types into Java’s object hierarchy. http://www.java2all.com
  • 5.
    Wrapper classes forconverting simple types Simple Type Wrapper class boolean Boolean char Character double Double float Float int Integer long Long http://www.java2all.com
  • 6.
    Converting primitive numbersto Object numbers using constructor methods Constructor Conversion Action calling Integer IntVal = Primitive integer to Integer object new Integer(i); Float FloatVal = Primitive float to Float object new Float(f); Double DoubleVal Primitive double to Double object = new Double(d); Long LongVal = Primitive long to Long object new Long(l); http://www.java2all.com
  • 7.
    Converting Numeric Stringsto Primitive numbers using Parsing method Method calling Conversion Action int i = Integer.parseInt(st Converts String str into primitive integer i r); long l = Long.parseLong(st Converts String str into primitive long l r); http://www.java2all.com
  • 8.
    NOTE : parseInt() and parseLong() methods throw a NumberFormatException if the value of the str does not represent an integer. http://www.java2all.com
  • 9.
    Byte http://www.java2all.com
  • 10.
    The Byte classencapsulates a byte value. It defines the constants MAX_VALUE and MIN_VALUE and provides these constructors: Byte(byte b) Byte(String str) Here, b is a byte value and str is the string equivalent of a byte value. http://www.java2all.com
  • 11.
    EX : import java.util.*; publicclass Byte_Demo { public static void main(String args[]) { Byte b1 = new Byte((byte)120); for(int i = 125; i<=135; i++) { Byte b2 = new Byte((byte)i); System.out.println("b2 = " + b2); } System.out.println("b1 Object = " + b1); System.out.println("Minimum Value of Byte = " + Byte.MIN_VALUE); System.out.println("Maximum Value of Byte = " + Byte.MAX_VALUE); System.out.println("b1* 2 = " + b1*2); System.out.println("b1* 2 = " + b1.byteValue()*2); Byte b3 = new Byte("120"); System.out.println("b3 Object = " + b3); System.out.println("(b1==b3)? " + b1.equals(b3)); System.out.println("(b1.compareTo(b3)? " + b1.compareTo(b3)); /*Returns 0 if equal. Returns -1 if b1 is less than b3 and 1 if b1 is greater than 1*/ } } http://www.java2all.com
  • 12.
    Output : b2= 125 b2 = 126 b2 = 127 b2 = -128 b2 = -127 b2 = -126 b2 = -125 b2 = -124 b2 = -123 b2 = -122 b2 = -121 b1 Object = 120 Minimum Value of Byte = -128 Maximum Value of Byte = 127 b1* 2 = 240 b1* 2 = 240 b3 Object = 120 (b1==b3)? true (b1.compareTo(b3)? 0 http://www.java2all.com
  • 13.
    Short http://www.java2all.com
  • 14.
    The Short classencapsulates a short value. It defines the constants MAX_VALUE and MIN_VALUE and provides the following constructors: Short(short s) Short(String str) http://www.java2all.com
  • 15.
    EX : import java.util.*; publicclass Short_Demo { public static void main(String args[]) { Short s1 = new Short((short)2345); for(int i = 32765; i<=32775; i++) { Short s2 = new Short((short)i); System.out.println("s2 = " + s2); } System.out.println("s1 Object = " + s1); System.out.println("Minimum Value of Short = " + Short.MIN_VALUE); System.out.println("Maximum Value of Short = " + Short.MAX_VALUE); System.out.println("s1* 2 = " + s1.shortValue()*2); Short s3 = new Short("2345"); System.out.println("s3 Object = " + s3); System.out.println("(s1==s3)? " + s1.equals(s3)); Short s4 = Short.valueOf("10", 16); System.out.println("s4 Object = " + s4); } } http://www.java2all.com
  • 16.
    Output : s2 =32765 s2 = 32766 s2 = 32767 s2 = -32768 s2 = -32767 s2 = -32766 s2 = -32765 s2 = -32764 s2 = -32763 s2 = -32762 s2 = -32761 s1 Object = 2345 Minimum Value of Short = -32768 Maximum Value of Short = 32767 s1* 2 = 4690 s3 Object = 2345 (s1==s3)? true s4 Object = 16 http://www.java2all.com
  • 17.
    Integer http://www.java2all.com
  • 18.
    Integer class : The Integer class encapsulates an integer value. This class provides following constructors: Integer(int i) Integer(String str) Here, i is a simple int value and str is a String object. http://www.java2all.com
  • 19.
    EX : import java.util.*; publicclass Int_Demo { public static void main(String args[]) { Integer i1 = new Integer(12); System.out.println("I1 = " + i1); System.out.println("Binary Equivalent = " + Integer.toBinaryString(i1)); System.out.println("Hexadecimal Equivalent = " + Integer.toHexString(i1)); System.out.println("Minimum Value of Integer = " + Integer.MIN_VALUE); System.out.println("Maximum Value of Integer = " + Integer.MAX_VALUE); System.out.println("Byte Value of Integer = " + i1.byteValue()); System.out.println("Double Value of Integer = " + i1.doubleValue()); Integer i2 = new Integer(12); System.out.println("i1==i2 " + i1.equals(i2)); System.out.println("i1.compareTo(i2) = " + i2.compareTo(i1)); // Compareto - if it is less than it returns -1 else 1, if equal it return 0. Integer i3 = Integer.valueOf("11", 16); System.out.println("i3 = " + i3); } } http://www.java2all.com
  • 20.
    Output : I1 =12 Binary Equivalent = 1100 Hexadecimal Equivalent = c Minimum Value of Integer = -2147483648 Maximum Value of Integer = 2147483647 Byte Value of Integer = 12 Double Value of Integer = 12.0 i1==i2 true i1.compareTo(i2) = 0 i3 = 17 http://www.java2all.com
  • 21.
    Long http://www.java2all.com
  • 22.
    Long : The Long class encapsulates a long value. It defines the constants MAX_VALUE and MIN_VALUE and provides the following constructors: Long(long l) Long(String str) http://www.java2all.com
  • 23.
    EX : import java.util.*; public class Long_Demo { public static void main(String args[]) { Long L1 = new Long(68764); Long L2 = new Long("686748"); System.out.println("Object L1 = " + L1); System.out.println("Object L2 = " + L2); System.out.println("Minimum Value of Long = " + Long.MIN_VALUE); System.out.println("Maximum Value of Long = " + Long.MAX_VALUE); System.out.println("L1 * 2 = " + L1 * 2); System.out.println("L1.longValue() * 2 = " + L1.longValue() * 2); System.out.println("L1.compareTo(l2) = " + L1.compareTo(L2)); System.out.println("L1==L2 ? = " + L1.equals(L2)); Long L3 = Long.valueOf("10", 16); System.out.println("Object L3 = " + L3); System.out.println("Byte value of Long = " + L1.byteValue()); System.out.println("int value of Long = " + L1.intValue()); System.out.println("Double value of Long = " + L1.doubleValue()); int i = 12; System.out.println("Binary equivalent of decimal " + i + "=" + Long.toBinaryString(i)); System.out.println("Hexadecimal equivalent of decimal " + i + "=" + Long.toHexString(i)); } } http://www.java2all.com
  • 24.
    Output : Object L1= 68764 Object L2 = 686748 Minimum Value of Long = -9223372036854775808 Maximum Value of Long = 9223372036854775807 L1 * 2 = 137528 L1.longValue() * 2 = 137528 L1.compareTo(l2) = -1 L1==L2 ? = false Object L3 = 16 Byte value of Long = -100 int value of Long = 68764 Double value of Long = 68764.0 Binary equivalent of decimal 12=1100 Hexadecimal equivalent of decimal 12=c http://www.java2all.com
  • 25.
    Double http://www.java2all.com
  • 26.
    Double class : The Double class encapsulates a double value. It defines several constants. The largest and smallest values are saved in MAX_VALUE and MIN_VALUE. The constant NaN (Not a Number) indicates that a value is not a number. http://www.java2all.com
  • 27.
    If you dividea double number by zero, the result is NaN. This class defines these constructors: Double(double d) Double(String str) Here, d is a double value to be encapsulated in a Double object. In the last form, str is the string representation of a double value. http://www.java2all.com
  • 28.
    EX : import java.util.*; classDouble_Demo { public static void main(String args[]) { Double d1 = new Double(687642365.4563); Double d2 = new Double("686748"); System.out.println("Object d1 = " + d1); System.out.println("Object d2 = " + d2); System.out.println("Minimum Value of Double = " + Double.MIN_VALUE); System.out.println("Maximum Value of Double = " + Double.MAX_VALUE); System.out.println("Byte value of Double = " + d1.byteValue()); System.out.println("int value of Double = " + d1.intValue()); System.out.println("Float value of Double = " + d1.floatValue()); Double d3 = Double.parseDouble("765.89"); System.out.println("Double value from the string "765.89"="+d3); } } http://www.java2all.com
  • 29.
    Output : Object d1= 6.876423654563E8 Object d2 = 686748.0 Minimum Value of Double = 4.9E-324 Maximum Value of Double = 1.7976931348623157E308 Byte value of Double = -3 int value of Double = 687642365 Float value of Double = 6.8764237E8 Double value from the string "765.89"=765.89 http://www.java2all.com
  • 30.
    Float http://www.java2all.com
  • 31.
    Float class : The float class encapsulates a float value. It defines several constants the largest and smallest values are stored in MAX_VALUE and MIN_VALUE. The constant NaN indicates that a value is not a number. If you divide a floating – point number by zero, the result is NaN. http://www.java2all.com
  • 32.
    This class definesthese constructors: Float(float f) Float(double d) Float(String str) Here, f and d are float and double types to be encapsulated in a Float object. str is the string representation of a float value. http://www.java2all.com
  • 33.
    EX : import java.util.*; publicclass Float_Demo { public static void main(String args[]) { Float f1 = new Float(123.5626); Float f2 = new Float(854.32f); Float i = new Float(10); System.out.println("Object f1 = " + f1); System.out.println("Object f2 = " + f2); System.out.println("Minimum Value of Float = " + Float.MIN_VALUE); System.out.println("Maximum Value of Float = " + Float.MAX_VALUE); System.out.println("Byte value of Float = " + f1.byteValue()); System.out.println("Short value of Float = " + f1.shortValue()); System.out.println("Integer value of Float = " + f1.intValue()); System.out.println("Double value of Float = " + f1.doubleValue()); System.out.println("(f1==f2) ?= " + f1.equals(f2)); System.out.println("f1.compareTo(f2) = " + f1.compareTo(f2)); System.out.println("f1 is not a number = " + i.isNaN()); } } http://www.java2all.com
  • 34.
    Output : Object f1= 123.5626 Object f2 = 854.32 Minimum Value of Float = 1.4E-45 Maximum Value of Float = 3.4028235E38 Byte value of Float = 123 Short value of Float = 123 Integer value of Float = 123 Double value of Float = 123.5625991821289 (f1==f2) ?= false f1.compareTo(f2) = -1 f1 is not a number = false http://www.java2all.com
  • 35.
    Character http://www.java2all.com
  • 36.
    Character class : The Character class encapsulates a char value. This class provides the following constructor. Character(char ch) Here, c is a char value. charValue() method returns the char value that is encapsulated by a Character object and has the following form: char charValue() http://www.java2all.com
  • 37.
    EX : import java.util.*; publicclass Char_Demo { public static void main(String args[]) { Character c1 = new Character('m'); char c2 = 'O'; System.out.println("Object C1 = " + c1); System.out.println("char value of Character Object = " + c1.charValue()); System.out.println(c2 + " is defined character set ? " + Character.isDefined(c2)); System.out.println("c2 is digit = " + Character.isDigit(c2)); System.out.println("c2 is lowercase character = " + Character.isLowerCase(c2)); System.out.println("c2 is uppercase character = " + Character.isUpperCase(c2)); } } http://www.java2all.com
  • 38.
    Output : Object C1= m char value of Character Object = m O is defined character set ? true c2 is digit = false c2 is lowercase character = false c2 is uppercase character = true http://www.java2all.com
  • 39.
    Boolean http://www.java2all.com
  • 40.
    Boolean class : The Boolean class encapsulates a Boolean value. It defines FALSE and TRUE constants. This class provides following constructors: Boolean(Boolean b) Boolean(String str) Here, b is a Boolean value and str is the string equivalent of a Boolean value. http://www.java2all.com
  • 41.
    The methods associatedwith Boolean Class are as follows: 1. Boolean booleanValue() 2. Boolean equals(Boolean b) 3. String toString(Boolean b) http://www.java2all.com
  • 42.
    EX : import java.util.*; publicclass Boolean_Demo { public static void main(String args[]) { Boolean b1 = new Boolean(true); Boolean b2 = new Boolean(false); System.out.println("Object B1 = " + b1); System.out.println("Object B2 = " + b2); Boolean b3 = new Boolean("true"); Boolean b4 = new Boolean("false"); System.out.println("Object B3 = " + b3); System.out.println("Object B4 = " + b4); System.out.println("Boolean Value = " + b1.booleanValue()); System.out.println("(b1==b2)? " + b1.equals(b2)); String S1 = b1.toString(); System.out.println("String S1 " + S1); } } http://www.java2all.com
  • 43.
    Output : Object B1= true Object B2 = false Object B3 = true Object B4 = false Boolean Value = true (b1==b2)? false String S1 true http://www.java2all.com

Editor's Notes

  • #14 White Space Characters