Java2all.com  

Byte





Share This Topic -

Byte class example in java:

         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. 

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*/
    }
}

 
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