Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.Lang.Short class in Java
Next article icon

Java.Lang.Byte class in Java

Last Updated : 22 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, Byte class is a wrapper class for the primitive type byte which contains several methods to effectively deal with a byte value like converting it to a string representation, and vice-versa. An object of the Byte class can hold a single byte value.

Constructors of Byte Class

There are mainly two constructors to initialize a Byte object-

1. Byte(byte b)

Creates a Byte object initialized with the value provided.

Syntax:   public Byte(byte b)  Parameters : b : value with which to initialize

2. Byte(String s)

Creates a Byte object initialized with the byte value provided by string representation. The default radix is taken to be 10.

Syntax :  public Byte(String s) throws NumberFormatException  Parameters : s : string representation of the byte value   Throws : NumberFormatException : If the string provided does not represent any byte value.

Fields in Java Byte class

  1. static int BYTES: The number of bytes used to represent a byte value in two’s complement binary form.
  2. static byte MAX_VALUE: A constant holding the maximum value a byte can have, 27-1.
  3. static byte MIN_VALUE: A constant holding the minimum value a byte can have, -27.
  4. static int SIZE: The number of bits used to represent a byte value in two’s complement binary form.
  5. static Class<Byte> TYPE: The Class instance representing the primitive type byte.

Methods in Java Byte Class

1. toString()

The toString() function returns the string corresponding to the byte value.

Syntax :  public String toString(byte b)  Parameters : b : byte value for which string representation required.

2. valueOf() :

This function returns the Byte object initialized with the value provided.

Syntax : public static Byte valueOf(byte b)  Parameters : b : a byte value

3. valueOf(String val,int radix) 

Another overloaded function valueOf(String val,int radix) which provides function similar to new Byte(Byte.parseByte(val,radix))

Syntax :  public static Byte valueOf(String val, int radix) throws NumberFormatException  Parameters : val : String to be parsed into byte value radix : radix to be used while parsing  Throws : NumberFormatException : if String cannot be parsed to a byte value in given radix.

4. valueOf(String val)

Another overloaded function valueOf(String val) which provides function similar to new Byte(Byte.parseByte(val,10))

Syntax :   public static Byte valueOf(String s)  throws NumberFormatException             Parameters : s : a String object to be parsed as byte  Throws : NumberFormatException : if String cannot be parsed to a byte value in given radix.

5. parseByte()

Returns byte value by parsing the string in radix provided. Differs from valueOf() as it returns a primitive byte value and valueOf() return Byte object.

Syntax :      public static byte parseByte(String val, int radix)     throws NumberFormatException  Parameters : val : String representation of byte  radix : radix to be used while parsing  Throws : NumberFormatException : if String cannot be parsed to a byte value in given radix.

6. Another overloaded method containing only String as a parameter, radix is by default set to 10.

Syntax :   public static byte parseByte(String val)  throws NumberFormatException  Parameters : val : String representation of byte   Throws : NumberFormatException : if String cannot be parsed to a byte value in given radix.

7. decode(): 

returns a Byte object holding the decoded value of the string provided. String provided must be of the following form else NumberFormatException will be thrown- Decimal- (Sign)Decimal_Number Hex- (Sign)”0x”Hex_Digits Hex- (Sign)”0X”Hex_Digits Octal- (Sign)”0″Octal_Digits

Syntax :   public static Byte decode(String s)  throws NumberFormatException  Parameters : s : encoded string to be parsed into byte val  Throws : NumberFormatException : If the string cannot be decoded into a byte value

8. byteValue()

The byteValue() function returns a byte value corresponding to this Byte Object.

Syntax : public byte byteValue()

9. shortValue()

returns a short value corresponding to this Byte Object.

Syntax : public short shortValue()

10. intValue()

The intValue() returns a int value corresponding to this Byte Object.

Syntax : public int intValue()

11. longValue()

The longValue() function returns a long value corresponding to this Byte Object.

Syntax : public long longValue()

12. doubleValue()

returns a double value corresponding to this Byte Object.

Syntax : public double doubleValue()

13. floatValue() 

returns a float value corresponding to this Byte Object.

Syntax : public float floatValue()

14. hashCode()

The hashCode() method returns the hashcode corresponding to this Byte Object.

Syntax : public int hashCode()

15. equals()

It is used to compare the equality of two Byte objects. The equals() methods returns true if both the objects contains same byte value. Should be used only if checking for equality. In all other cases compareTo method should be preferred.

Syntax :      public boolean equals(Object obj)  Parameters : obj : object to compare with

16. compareTo()

The compareTo() is used to compare two Byte objects for numerical equality. This should be used when comparing two Byte values for numerical equality as it would differentiate between less and greater values. Returns a value less than 0,0,value greater than 0 for less than,equal to and greater than.

Syntax :   public int compareTo(Byte b)  Parameters : b : Byte object to compare with

17. compare()

The compare() method compares two primitive byte values for numerical equality. As it is a static method therefore it can be used without creating any object of Byte.

Syntax :  public static int compare(byte x,byte y)  Parameters : x : byte value y : another byte value

Java




// Java program to illustrate
// various methods of Byte class
public class Byte_test {
    public static void main(String[] args)
    {
 
        byte b = 55;
        String bb = "45";
 
        // Construct two Byte objects
        Byte x = new Byte(b);
        Byte y = new Byte(bb);
 
        // 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(bb);
        System.out.println("ValueOf(bb) = " + z);
        z = Byte.valueOf(bb, 6);
        System.out.println("ValueOf(bb,6) = " + z);
 
        // parseByte()
        // return primitive byte value
        byte zz = Byte.parseByte(bb);
        System.out.println("parseByte(bb) = " + zz);
        zz = Byte.parseByte(bb, 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(x) = "
                           + x.byteValue());
        System.out.println("shortvalue(x) = "
                           + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = "
                           + x.longValue());
        System.out.println("doublevalue(x) = "
                           + x.doubleValue());
        System.out.println("floatvalue(x) = "
                           + x.floatValue());
 
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
 
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
 
        int e = Byte.compare(x, y);
        System.out.println("compare(x,y) = " + e);
 
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + 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(x) = 55 shortvalue(x) = 55 intvalue(x) = 55 longvalue(x) = 55 doublevalue(x) = 55.0 floatvalue(x) = 55.0 hashcode(x) = 55 x.equals(y) = false compare(x,y) = 10 x.compareTo(y) = 10 

References : Official Java Documentation 



Next Article
Java.Lang.Short class in Java

R

Rishabh Mahrsee
Improve
Article Tags :
  • Java
  • Java-Byte
  • Java-lang package
  • java-lang-reflect-package
  • java-wrapper-class
Practice Tags :
  • Java

Similar Reads

  • Wrapper Classes in Java
    A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
    6 min read
  • Primitive Wrapper Classes are Immutable in Java
    In Java, an immutable class is a class (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) which once created then its body can not be changed and the same applies to immutable objects which once created cannot be changed. Now the question arises that we do need the help of wrapper c
    3 min read
  • Java.lang.Number Class in Java
    Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method
    9 min read
  • Java.lang.Integer class in Java
    Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa. An object of the Integer class can hold a single int value. Constructors: Integer(int b): Creates an Integer
    15 min read
  • Java.Lang.Byte class in Java
    In Java, Byte class is a wrapper class for the primitive type byte which contains several methods to effectively deal with a byte value like converting it to a string representation, and vice-versa. An object of the Byte class can hold a single byte value. Constructors of Byte Class There are mainly
    6 min read
  • Java.Lang.Short class in Java
    Short class is a wrapper class for the primitive type short which contains several methods to effectively deal with a short value like converting it to a string representation, and vice-versa. An object of Short class can hold a single short value. There are mainly two constructors to initialize a S
    6 min read
  • Java.Lang.Long class in Java
    Long class is a wrapper class for the primitive type long which contains several methods to effectively deal with a long value like converting it to a string representation, and vice-versa. An object of Long class can hold a single long value. There are mainly two constructors to initialize a Long o
    12 min read
  • Java.Lang.Float class in Java
    Float class is a wrapper class for the primitive type float which contains several methods to effectively deal with a float value like converting it to a string representation, and vice-versa. An object of the Float class can hold a single float value. There are mainly two constructors to initialize
    6 min read
  • Java.Lang.Double Class in Java
    Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr
    4 min read
  • Java.lang.Boolean Class in Java
    Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences