HomeJava

Java for Testers – byte data type

Java for Testers – byte data type

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained about String non-primitive data type.

In this article, I am going to explain and practically demonstrate byte data type in Java.

Java for Testers – byte data type

byte data type also stores the integer values similar to int data type.

But the size of byte data type is 4 times smaller than the int data type.

i.e. int data type is 32 bit and can store the values in the range of -2147483648 to 2147483647

Whereas byte is 8 bit and can store the values in the range of -128 to 127.

In general, we use int data type for storing the integer values into a variable, unless we get any problem where we have to save memory and go with byte data type.

Example of using byte data type:

byte f = 9;

In the above example, we have declared the variable f with the byte data type, and hence we are able to store the integer values in the range of -128 to 127 into the variable.

Follow the below steps for practically implementing byte data type in Java:

1) Let’s create a Java program and create a Demo class as shown below:

Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE

Print statements - Java project

2) Inside the main() method, let’s create a variable, declare the variable with byte data type and assign the variable with an integer value in the range of -128 to 127 say 9 as shown below:

byte data type Java - byte

3) Now print the value stored in the variable by printing the variable using the print statement as shown below:

byte data type Java - byte print

4) Save the Project and click on the ‘Run’ button.

Observe that the Program got executed and the integer value stored in the byte declared variable f got printed as shown below:

byte data type Java - byte printed

Copy the above-explained code from the below section:

public class Demo {

	public static void main(String[] args) {
		
		byte f = 9;
		
		System.out.println(f);
	
	}

}

Here concludes this article.

In the next article, I will explain and demonstrate the short data type in Java.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning ?

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp