Byte object constructor
In this chapter you will learn:
Byte object's constructor
These are the two constructors from Byte
class. We can use them to create Byte
object.
Byte(byte value)
creates a Byte
object for byte
value.
Byte(String s)
creates a Byte
object for the byte value indicated by
the String
parameter.
The following code creates two Byte
type object. One is from a string value and the other is from a
byte
value.
public class Main {
public static void main(String[] args) {
/* j a v a 2 s . com*/
Byte byteObject = new Byte("100");
System.out.println(byteObject);
byte b = 101;
Byte byteObject2 = new Byte(b);
System.out.println(byteObject2);
}
}
The output:
You cannot pass integer
literal to the constructor directly as Java think it
is an int
not a byte
.
public class Main {
public static void main(String[] args) {
// j ava 2 s.co m
Byte byteObject2 = new Byte(101);
System.out.println(byteObject2);
}
}
When you try to compile the code above, the compiler will output the following error message:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types