In this, java variables and data types tutorial, we learn how to declare a variable in java and what is data types in java. Let’s start with an example to declare variable and data type.
Example: Addition of two numbers.
class VarDemo //creating a class
{
public static void main(string arg[]) //define main method
{
//declare variables
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println(c);
}
}
In the above example you need to look at the first line in main() method that is int a,b,c;
- int – This is called a data type.
- a,b,c – These are known as variables.
Now, let’s learn about variable and data type.
What is a Variable?
The benefit of variables is to store values than further, we can refer multiple times anywhere in the program, whenever required.
- The variable is a memory location.
- It can store values like numbers and text.
- The value of a variable can be changed during programming.
- All variables must be declared before use.
- The variables always assigned values with the equal(=) sign.
Naming Rules for Variable
- Name of a variable can be a combination of letters, numbers and underscore.
- The first character of a variable name should not be a number, it must start with a character or underscore.
- A variable name can have both uppercase and lowercase or a combination of both cases.
- Variable name should not be a reserved keyword (predefined word in Java, like void and break).
- Name of the variable should be meaningful.
Data Types in Java.
Whenever we declare a variable, we must specify the data type of that variable in Java. So, the question is what is a data type, type of data to be store in a variable is known as the data type. We must specify that the variable will store number, text or boolean values as a data type. In Java, there are two types of the data type.
- Primitive Data Type
- Abstract Data Type (Non-Primitive)
Primitive Data Type
There is a total of eight primitive data types in Java.
Data Type | Size in Bytes | Description and Range |
---|---|---|
boolean | 1 | Store true or false |
byte | 1 | Whole Numbers between -128 to 127 |
short | 2 | Whole Numbers between -32,768 to 32,767 |
int | 4 | Whole Numbers between -2,147,483,648 to 2,147,483,647 |
long | 8 | Whole numbers between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 | Fractional numbers, store upto 7 decimanl digit |
double | 8 | Fractional numbers, store upto 15 decimal digit |
char | 2 | Single characters |
Abstract Data Type (Non-Primitive)
Abstract data types are based on primitive data types.
String, Array, classes.
Example: Example of data type and variables.
class Example {
public static void main(String arg[]) {
boolean a=true;
byte b=10;
short c=25000;
int d=50000;
long e=100000L; //long value should end with L.
float f=1.44f; //float value should end with f.
double g=45000.55d; //double value shold end with d;
char h='A'; //store only single character, should be in sigle inverted comma.
String name="Coder Mantra"; //Store text value, should be in double inverted comma.
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(name);
}
}