Java: Variables: byte

In this tutorial I will explain the byte. To do that I will use this file.


package net.tutorial.src;

public class Tutorial 
{
public static void main(String[] args)
{
System.out.println(“Hello World!”);
}
}


Like all other variables a byte has to be created somewhere inside of a class.
A byte is a number used in Java. It is faster than a short or an int, but it can only hold numbers from -128 up to 127. Just like with an int you can only use whole numbers like 2, 8 or 16. Numbers like 4,5 or 6,027492 can not be saved in a byte.
The reason why people would want to use a byte is that the speed of using one is much higher than with any other number variables. This can be useful in Arrays which I will explain in a later tutorial. Because of the speed you can also use it to save small numbers without lowering the speed much.
If you want to use a byte in java you need to code it like this.


byte number = 6;


The first part in this line is the type of variable. In this case an byte.
The second part in this line of code is the name of the variable. In this case number.
The last part of this line of code is the thing number will be. In this case number will be 6.
If you want to print out a number in the console you have to use a line of code like this.


System.out.println(number);


Because this bit of the code is in the main method and the main method is static you have to add the word static in front of byte. Why that is and how you should use that is something for a later tutorial though.


package net.tutorial.src;

public class Tutorial 
{
static byte number = 6;

public static void main(String[] args)
{
System.out.println(number);
}
}


 

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">