Categories: Java

Byte Class in Java

The Byte class creates an object out of a primitive type byte value. An object of type Byte has a single field of type byte. SignedBytes and UnsignedBytes are the methods that specifically treat bytes as signed or unsigned.

Byte wrapper class Constructor:
Byte(byte c) //Constructor of Byte wrapper class takes a primitive byte value.

Byte(String str) //Constructor of Byte wrapper class also takes a String equivalent of a byte value.
Methods of Byte class:
MethodDescription
static Byte valueOf()This method returns a Byte instance that represents the specified byte or string value.
String toString()It returns the Byte object’s string representation.
static int toUnsignedInt()It is used to convert the specified byte to an int by an unsigned conversion.
static long toUnsignedLong()It is used to convert the specified byte to a long by an unsigned conversion.
byte byteValue()returns the value of this Byte as a byte.
int compareTo(Byte anotherByte)This method numerically compares two Byte objects.
static Byte decode(String nm)Decodes a String into a Byte.
double doubleValue() returns the Byte’s value as a double.
boolean equals(Object obj)This method compares this object to the object specified.
float floatValue()It returns the Byte’s value as a Float.
int hashCode()This method returns a hash code for this Byte.
int intValue()It returns the Byte’s value as int.
long longValue()It returns the value of this Byte as a long.
static byte parseByte(String s)The string argument is parsed as a signed decimal byte by this method.
Example: class to demonstrate Byte wrapper class methods
public class ByteClassExample {
 public static void main(String[] args) {
  byte b = 55;
        String str = "45";
          
        // Construct two Byte objects
        Byte m = new Byte(b);
        Byte n= new Byte(str);
  
        // toString()
        System.out.println("toString(b) = " + Byte.toString(b));
  
        // valueOf()
        // return Byte object
        Byte z = Byte.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Byte.valueOf(str);
        System.out.println("ValueOf(bb) = " + z);
        z = Byte.valueOf(str, 6);
        System.out.println("ValueOf(bb,6) = " + z);
  
        // parseByte()
        // return primitive byte value
        byte zz = Byte.parseByte(str);
        System.out.println("parseByte(bb) = " + zz);
        zz = Byte.parseByte(str, 6);
        System.out.println("parseByte(bb,6) = " + zz);
          
        //decode()
        String decimal = "45";
        String octal = "005";
        String hex = "0x0f";
          
        Byte dec=Byte.decode(decimal);
        System.out.println("decode(45) = " + dec);
        dec=Byte.decode(octal);
        System.out.println("decode(005) = " + dec);
        dec=Byte.decode(hex);
        System.out.println("decode(0x0f) = " + dec);
  
        System.out.println("bytevalue(m) = " + m.byteValue());
        System.out.println("shortvalue(m) = " + m.shortValue());
        System.out.println("intvalue(m) = " + m.intValue());
        System.out.println("longvalue(m) = " + m.longValue());
        System.out.println("doublevalue(m) = " + m.doubleValue());
        System.out.println("floatvalue(m) = " + m.floatValue());
          
        int hash=m.hashCode();
        System.out.println("hashcode(m) = " + hash);
          
        boolean eq=m.equals(n);
        System.out.println("m.equals(n) = " + eq);
          
        int e=Byte.compare(m, n);
        System.out.println("compare(m,n) = " + e);
          
        int f=m.compareTo(n);
        System.out.println("m.compareTo(n) = " + f);
 }
}
Output:
toString(b) = 55
valueOf(b) = 55
ValueOf(bb) = 45
ValueOf(bb,6) = 29
parseByte(bb) = 45
parseByte(bb,6) = 29
decode(45) = 45
decode(005) = 5
decode(0x0f) = 15
bytevalue(m) = 55
shortvalue(m) = 55
intvalue(m) = 55
longvalue(m) = 55
doublevalue(m) = 55.0
floatvalue(m) = 55.0hashcode(m) = 55m.equals(n) = false
compare(m,n) = 10
m.compareTo(n) = 10

Note: also read about the Wrapper Class in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago