java lang Byte Class - java.lang.Byte Class in Java
Last updated on
java.lang.Byte Class
Byte Class in Java
The Byte class wraps the value of primitive data type byte into Byte object. An object of type Byte contains a single field whose type is byte. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.
In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.
java.lang.Byte Class Example
/* Java Byte Class Example Save with file name ByteExample.java */ public class ByteExample { public static void main(String args[]) { // JAVA BYTE DECLARATION Byte b; // PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT byte a = 101; // MEMORY ALLOCATION FOR BYTE // DON'T USE BELOW CONSTRUCTOR LIKE THIS b = new Byte(101) // BECAUSE JAVA COMPILER UNDERSTANDS 101 AS AN int b = new Byte(a); // JAVA BYTE CLASS OUTPUT System.out.println("Byte Class Example"); // RETURNS byte PRIMITIVE DATA TYPE System.out.println("Value is : "+ b.byteValue()); // RETURNS double PRIMITIVE DATA TYPE System.out.println("double Value is : "+ b.doubleValue()); // RETURNS float PRIMITIVE DATA TYPE System.out.println("float Value is : "+ b.floatValue()); // RETURNS int PRIMITIVE DATA TYPE System.out.println("int Value is : "+ b.intValue()); // RETURNS long PRIMITIVE DATA TYPE System.out.println("long Value is : "+ b.longValue()); // RETURNS short PRIMITIVE DATA TYPE System.out.println("short Value is : "+ b.shortValue()); } }
java.lang.Byte Class Example 2
/* Java Byte Class Example Save with file name ByteExample2.java */ public class ByteExample2 { public static void main(String args[]) { // JAVA BYTE DECLARATION Byte b, b2; // PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT byte a = 101; // MEMORY ALLOCATION FOR JAVA BYTE b = new Byte(a); b2 = new Byte("102"); // JAVA BYTE CLASS OUTPUT System.out.println("Java Byte Class Example"); System.out.println("b Value is : "+ b.byteValue()); System.out.println("b2 Value is : "+ b2.byteValue()); System.out.println("b compareTo b2 : "+ b.compareTo(b2)); System.out.println("b equals b2 : "+ b.equals(b2)); } }
In the following example you can learn how to use static methods of Byte Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.
java.lang.Byte Class Example 3
/* Java Byte Class Example Save with file name ByteExample3.java */ public class ByteExample3 { public static void main(String args[]) { // JAVA BYTE CLASS STATIC METHODS USAGE System.out.println("Java Byte Class Example"); // PRIMITIVE byte DECLARATION AND VALUE ASSIGNMENT byte a = 101; // RETURN Byte OBJECT WITH THE SPECIFIED PRIMITIVE byte DATA TYPE System.out.println("valueOf : "+ Byte.valueOf(a)); // RETURN Byte OBJECT WITH THE SPECIFIED // PRIMITIVE byte DATA TYPE AS String System.out.println("valueOf : "+ Byte.valueOf("103")); // RETURN PRIMITIVE byte DATA TYPE WITH THE SPECIFIED // PRIMITIVE byte DATA TYPE AS String System.out.println("parseByte : "+ Byte.parseByte("105")); } }