Advertisement
Constructor | Description |
---|---|
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. |
class A
{
public static void main(String... ar)
{
Byte b1 = new Byte((byte)10); //casting the default int value to byte.
Byte b2 = new Byte("10"); //Passing primitive byte as a String.
}
}
class A
{
public static void main(String... ar)
{
//In Java, by default any numeric non-floating value is of primitive int type, so its casting to byte will be required
//it is sent to the constructor of the Byte wrapper class.
Byte b1 = new Byte((byte)10); //Boxing primitive byte in a wrapper Byte object.
System.out.println(b1); //Unboxing the Byte object.
//Reading primitive byte out of Byte object using byteValue() method
Byte b2 = new Byte("10"); //Passing primitive byte as a String
System.out.println("b2.byteValue());
}
}
A
A
Advertisement
Methods | Description | int compareTo(Byte b) | - Returns a zero if the invoked Byte object contains the same byte value as b. - Returns a positive value if the invoked Byte object contains greater value than b. - Returns a negative value if the invoked Byte object contains smaller value than b. |
---|---|
boolean equals(Object ob) | Returns a true if the invoked Byte object has same value as referred by ob, else false. |
static byte parseByte(String s) | Returns a primitive byte value if String s could be converted to a valid byte value. |
static Byte valueOf(byte b) | Returns a Byte object after converting it from primitive byte value, b. |
short shortValue() | Returns an primitive short value after converting it from an invoked Byte object. |
byte byteValue() | Returns an byte value after converting it from an invoked Byte object. |
int intValue() | Returns an int value after converting it from an invoked Byte object. |
long longValue() | Returns an long value after converting it from an invoked Byte object. |
float floatValue() | Returns an float value after converting it from an invoked Byte object. |
double doubleValue() | Returns an double value after converting it from an invoked Byte object. |
class A
{
public static void main(String... ar)
{
Byte b1 = new Byte("10"); //Constructor accepting String value
Byte b2 = new Byte((byte)10); //casting an int to byte to pass to Constructor of Byte class.
System.out.println("Value in b1 = "+b1);
System.out.println("Value in b2 = "+b2);
System.out.println("Invoking b1 to compare with b2 : "+ b1.compareTo(b2));
Byte b3 = new Byte("20");
Byte b4 = new Byte((byte)25);
System.out.println("Value in b3 = "+b3);
System.out.println("Value in b4 = "+b4);
System.out.println("Invoking b3 to compare with b4 : "+ b3.compareTo(b4));
System.out.println("Invoking b4 to compare with b3 : "+ b4.compareTo(b3));
}
}
Value in b1 = 10
Value in b2 = 10
Invoking b1 to compare with b2 : 0
Value in b3 = 20
Value in b4 = 25
Invoking b3 to compare with b4 : -5
Invoking b4 to compare with b3 : 5
//Converting String to primitive byte value.
class A
{
public static void main(String... ar)
{
byte b1 = Byte.parseByte("10");
byte b2 = Byte.parseByte("100");
byte b3 = Byte.parseByte("50");
System.out.println("Primitive byte value in b1 : "+ b1);
System.out.println("Primitive byte value in b2 : "+ b2);
System.out.println("Primitive byte value in b3 : "+ b3);
}
}
Primitive byte value in b1 : 10
Primitive byte value in b2 : 100
Primitive byte value in b3 : 50
//Converting String to object of Byte class by passing it to constructor of Byte class.
class A
{
public static void main(String... ar)
{
Byte b1 = new Byte("hh"); //Passing a String value to Byte class which can't be parsed to byte.
System.out.println("Primitive byte value in b1 : "+ b1);
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "hh"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Byte.parseByte(Unknown Source)
at java.lang.Byte.(Unknown Source)
at A.main(Byte8.java:7)
//Converting String to primitive byte value by passing it to parseShort() method.
class A
{
public static void main(String... ar)
{
byte b1 = Byte.parseByte("-128"); //last negative range of a byte valuev
byte b2 = Byte.parseByte("-129"); //Passing a String value beyond the range of byte
System.out.println("Primitive byte value in b1 : "+ b1);
System.out.println("Primitive byte value in b2 : "+ b2);
}
}
Exception in thread "main" java.lang.NumberFormatException: Value out of range.
Value:"-129" Radix:10
at java.lang.Byte.parseByte(Unknown Source)
at java.lang.Byte.parseByte(Unknown Source)
at A.main(Short6.java:8)
//Converting Byte to short, int, long, float, double
class A
{
public static void main(String... ar)
{
Byte y = new Byte("10"); // Converting a String argument to wrapped Integer object
System.out.println("Value in wrapped object,y "+ y);
byte b = y.byteValue(); //Returns a primitive byte value out of a wrapped Byte object
short s= y.shortValue(); //Returns a primitive short value out of a wrapped Byte object
int i = y.intValue(); //Returns a primitive int value out of a wrapped Byte object
long l = y.longValue(); //Returns a primitive long value out of a wrapped Byte object
float f = y.floatValue(); //Returns a primitive float value out of a wrapped Byte object
double d = y.doubleValue(); //Returns a primitive double value out of a wrapped Byte object
System.out.println("byte value of Byte object, y :" + b);
System.out.println("short value of Byte object, y:" + s);
System.out.println("int value of Byte object, y :" + i);
System.out.println("long value of Byte object, y :" + l);
System.out.println("float value of Byte object, y :" + f);
System.out.println("double value of Byte object, y :"+ d);
}
}
Value in wrapped object,y 10
byte value of Byte object, y :10
short value of Byte object, y:10
int value of Byte object, y :10
long value of Byte object, y :10
float value of Byte object, y :10.0
double value of Byte object, y :10.0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement