Byte Class in Java

JavaObject Oriented ProgrammingProgramming

The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

Following are some of the methods of the Byte class −

Sr.No.Method & Description
1byte byteValue()
This method returns the value of this Byte as a byte.
2int compareTo(Byte anotherByte)
This method compares two Byte objects numerically.
3static Byte decode(String nm)
This method decodes a String into a Byte.
4double doubleValue()
This method returns the value of this Byte as a double.
5boolean equals(Object obj)
This method compares this object to the specified object.
6float floatValue()
This method returns the value of this Byte as a float.
7int hashCode()
This method returns a hash code for this Byte.
8int intValue()
This method returns the value of this Byte as an int.
9long longValue()
This method returns the value of this Byte as a long.
10static byte parseByte(String s)
This method parses the string argument as a signed decimal byte.

Let us now see an example−

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 = new Byte("-1");
      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

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

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("-123");
      b2 = new Byte("0");
      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

String value of Byte -123 is -123
String value of Byte 0 is 0
raja
Updated on 02-Jan-2020 10:57:36

Advertisements