Learn Variables In Java

Here we are going to learn variables in java.

So, what are variables in java??

Variables are nothing but names which we can declare to store some value. So, a variable is a name which we can use to store some value. In java there is a specified format to declare a variable.

For example,

public class Sample_Example {

public static void main(String[] args) {
 
int age = 10;
 
System.out.println(age);

}

}

In java we declare a variable like this. So first of all goes the data type. Data type can be for example the type of variable we are going to declare for example we want to store integer in some variable then we can declare this data type int which stands for integer and then you can give any name to the variable. For example age and then you can assign some value to this variable like 10.

This simply means that age is a variable of type integer which has a value of 10. So age in this case is our variable.

Now in java there are different type of data types like we have declared a variable of type integer but there are different type of data type which we can declare. And some of the most basic data type are mentioned below,

Learn Variables In Java: Primitive data types

Also Read – How To Set Environment Variables For Java Installation

Learn Variables In Java

So these are some of the basic data types. Let’s see one by one,

Byte, short, int and long data type can store a number but only difference between this byte, short, int and long is their size. So byte can store 1 byte. A short can store 2 bytes. Integer can store 4 bytes and a long can store 8 bytes. So the size differs in this case. A range of byte can be -128 to +127. For short the range can be  minus -32768 to +32767. So the range of these increases in the ascending order. So smallest is the byte then the short then the int and then the long.

Now to store some decimal point value we use float. For example, we want to store some decimal value we will write a data type called float average,

public class Sample_Example {

public static void main(String[] args) {
 
float average = 4.5;
 
System.out.println(average);

}

}

And this will store decimal value. But the problem with declaration of float in this case is you need to declare float something like this,

public class Sample_Example {

public static void main(String[] args) {
 
float average = (float)4.5;
 
System.out.println(average);

}

}

Because in java this 4.5 is natively considered as a double and double can also store a decimal point but it’s range is 8 byte. So that’s why we need to convert this double value which is 4.5 to the float value. And the difference between this float and double value is float is 4 bytes and this double is 8 bytes.

And in the same way you can declare a double value. Double can also store some decimal points. For example,

public class Sample_Example {

public static void main(String[] args) {
 
double my_Average = 11.56;
 
System.out.println(my_Average);

}

}

The other data type is a char. A char value can store character. For example,

public class Sample_Example {

public static void main(String[] args) {
 
char my_Grades = 'A';
 
System.out.println(my_Grades);

}

}

Here i’m storing capital ‘A’ or any single character that you store in char value.

And the last basic data type is a boolean. Boolean can store true or false. For example,

public class Sample_Example {

public static void main(String[] args) {
 
boolean is_True = false;
 
System.out.println(is_True);

}

}

So, these are some of the basic data types in java and above are the examples and how you can declare a variable in java.

About the author

admin

View all posts

Leave a Reply

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