Java Byte Keyword
Byte:
- The Keyword Byte in Java programming language is a primitive data type.
- Digital content that is most used for eight bits is called as a byte.
- The Java Byte Keyword is used to implement the variables in the program.
- In the program the byte keyword can also be implemented with the methods to execute the byte values.
- The Byte keyword can support the value of an 8-bit signed two's integer variables.
Important Points of Byte Keyword
- The Byte Keyword will hold or support the values that lie between –128 to 127(inclusive).
- The Default value of the Byte keyword represented by 0.
- The Byte keyword can be replaced with the int or instead of using int we can also use byte keyword which resembles the same storing range values that is from –128 to 127.
- The main intension of using byte keyword instead of int data type is that the byte keyword has the ability in reducing memory allocation or it is useful in saving the memory in large forms of arrays.
- The byte keyword is mainly useful for networks or files that handle stream of data that is extracted from the networks or files.
Initializing and Declaring the Byte Keyword
- Here we implement the declaration and implementation of the byte keyword in java program with some values.
- Syntax of java byte declaration is represented below.
byte variablename
- The name of the variable is mending with the semi-colon.
- Syntax for initializing the byte keyword with the variable is given below.
byte variablename = raju
Byte Syntax in Java
byte Name = raju.
For example:
byte p = 20.
Here variable name is p and value is given by 20, given to a variable integer p with data type byte.
Java byte keyword Examples
Example 1: variable with byte keyword
public class ByEx1 {
public static void main (String [] s) {
byte d1= 100.
byte d2=-200.
System.out.println("d1: "+d1).
System.out.println("d2: "+d2).
}
}
Output:
d1=100
d2=200
public class Byte {
public static void main(String [] s) {
byte g= 10; //provide a byte variable and given the value i.e 7
System.out.println(g); // print the declared value of byte variable g
}
}
Output:
5
Example 2: byte lies out of range
public class ByteEx2 {
public static void main (String [] s) {
byte f1=128.
byte f2=-129.
System.out.println("f1: "+f1).
System.out.println("f2: "+f2).
}
}
Output:
error
public class Main {
public static void main (String [] s) {
byte n1 = 5.
byte n2 = 140.
// printing
System.out.println("The sum of variable is:"+ (n1+n2)).
}
}
Output:
error
Example 3: Using byte with a method
public class ByEx3 {
byte year=18.
public byte display ()
{
return year.
}
public static void main (String [] s) {
ByEx2 f=new ByEx2().
System.out.println("The year must be: "+f. display ()).
}
}
Output:
The year must be: 18
public class happy
{
public static void main (String [] s)
{
byte f = 5; //implement a byte variable and giving the value i.e 5
Byte z1 = new Byte(f).
Byte z2 = new Byte ("6").
System.out.println(z1); // print the value of byte variable z1
System.out.println(z2) ;//print the value of byte variable z2
}
}
Output:
5
6
Addition of numbers using byte:
public class AddByte {
public static void main (String [] args) {
byte x= 400.
byte y = 210.
byte z = (byte) (x + y).
System.out.println("The z variable Value after Addition is: " + z).
}
}
Example of Java byte array:
import java. util. Arrays.
// main class in java
public class Main {
// method of java
public static void main (String [] args) {
// supporting byte array type
byte [] MyArray = new byte []{1, 2, 3, 4, 5,6};
// printing the java byte array
System.out.println(Arrays.toString(MyArray)).
}
}
Output:
[1, 2, 3, 4, 5, 6]
Converting String to Java byte array:
// importing Arrays
import java. util. Arrays.
// java main class
public class Main {
// java main method
public static void main (String [] args) {
// developing java string
String HY = "hello world”.
// converting a java string to byte array
byte [] MyArray = HY. getBytes ();
// printing
System.out.println(Arrays.toString(HY));
}
}