Byte Class Fields in Java with example

JavaObject Oriented ProgrammingProgramming

The Byte class wraps a value of primitive type byte in an object.

Following are the fields for Byte class−

  • static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.
  • static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.
  • static int SIZE − This is the number of bits used to represent a byte value in two's complement binary form.
  • static Class<Byte> TYPE − This is the Class instance representing the primitive type byte.

Example

Let us now see an example −

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      int i1, i2;
      b1 = new Byte("1");
      b2 = Byte.MIN_VALUE;
      i1 = b1.intValue();
      i2 = b2.intValue();
      String str1 = "int value of Byte " + b1 + " is " + i1;
      String str2 = "int value of Byte " + b2 + " is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

This will produce the following output -

int value of Byte 1 is 1
int value of Byte -128 is -128

Example

Let us now see another example −

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      String s1, s2;
      b1 = new Byte("-10");
      b2 = Byte.MIN_VALUE;
      System.out.println("Number of bits in b1 = "+b1.SIZE );
      System.out.println("Number of bits in b2 = "+b2.SIZE );
      s1 = b1.toString();
      s2 = b2.toString();
      String str1 = "String value of Byte " + b1 + " is " + s1;
      String str2 = "String value of Byte " + b2 + " is " + s2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

This will produce the following output -

Number of bits in b1 = 8
Number of bits in b2 = 8
String value of Byte -10 is -10
String value of Byte -128 is -128
raja
Published on 02-Jan-2020 15:29:17
Advertisements