After getting introduced to Java structure, Let's know about Data types and variables in Java. By the end of the article, you will come to know about today's topic in detail. Today I will start the article with an example that will give a brief idea about a given topic. So let's start with it.
Consider there are many containers in which colours are stored. Each container has stored a unique colour. For eg: One container has red colour in it. What if I mix yellow colour in red, obviously it will mix and gives another colour. To avoid this, it is necessary to store the yellow colour in the container that stores the yellow colour in it. Now let us assume these containers as variables, these variables contain unique values which have some types i.e Number, characters etc. You can't stores mixed types in one variable. In short, a variable is a container that stores a data value. Variables are declared in the following way:
data_type variable_name= value;
Now a question arises, What are the data types??
Data types describe what type of data value is going to be store in the variable i.e numbers, character, decimal numbers, etc. Data types are divided into two groups:
Primitive data types : These data types are defined with specific size and their type. They are already defined by Java. Following are the primitive data types:
byte : 1 byte // Stores whole numbers from -128 to 127
short : 2 bytes // Stores whole numbers from -32,768 to 32,767
int : 4 bytes // Stores whole numbers from -2,147,483,648 to 2,147,483,647
long : 8 bytes // Stores whole number which is very long you can check it on internet.
float : 4 bytes // Stores fractional numbers. Upto 6 to 7 decimal digits.
double : 8 bytes // Stores fractional numbers. Upto 15 decimal digits.
boolean : 1 bit // Stores true or false.
char : 2 bytes // Stores a single character / letter or ASCII.
Non-primitive data types: These data types are defined by the users except the String
data type. Following are the types:
String
: This stores a sequence of characters, like "Java" enclosed in double inverted commas.- Arrays
- Class
Arrays and Class will be discussed in upcoming blogs. For now, just focus on String
.
Let's see how these data types are assigned to variables by considering the below code.
byte n = 100;
short num = 5000;
int number = 100000;
long long_number = 15000000000L;
float r= 5.75f;
double d = 19.99d;
boolean t = true;
boolean f = false;
char character = 'B';
String greet = "Hello World";
Note:
long
,float
,double
are ended with letters 'L', 'f', 'd'. It is mandatory to end with a letter for given data types.
The above variables can be displayed by the println()
method
System.out.println(variable_name);
Tip: 1. Using the word
final
before declaring the variable makes it constant throughout the program. No one can overwrite or change it during the program execution.
final int s=14; //This value will not change till the user doesn't change it.
Tip: 2. You can declare the variable first and initialize it later.
int r;
r = 10;
System.out.println(r);
For a summary, you can see the below image:
I hope you have understood today's topic which I have covered. Thanks for your time. Happy Programming!