What are Java Variables and Data Types?
1. What is a Java Variable?
A variable can be called a container that holds some value in a Java program. The variable has assigned a data type that defines what type of value the variable can hold. The variable has a name which locates in the memory.If you want to use a variable in a program, there are two steps associated with it you have to perform:
The above two steps (declaration and initialization) can also be joined together in one single step.
- Variable Declaration
- Variable Initialization
While declaring a variable you have to assign a data type with the variable name. The variable name has to be unique.
Example: String employee;
ii. Variable Initialization
It means assigning a value to the declared variable.
Example: employee = "John"
The above two steps (declaration and initialization) can also be joined together in one single step.
Example: String employee = "John";
Let's explain the above example,
- 'String' is a data type
- 'employee' is the name of the variable
- "John" is the value assigned to a variable named employee.
2. Types of Java Variables
Java has three types of variables:
- Local Variables
- Instance Variables
- Static Variables
2.1. Local Variables
A variable that is declared inside a method is a local variable. These variables can only be used within that method as their scope lies only inside that method.
2.2. Instance Variables
A variable that is declared outside of the method but inside the class is called the instance variable. It is not declared static.
2.3. Static Variables
A variable that is declared with the keyword 'static' and outside the method is called a static variable. Static variables are allocated memory when a class is loaded in memory. Static variables are initialized before instance variables.
Code example for Java Variable Types:
package com.techlistic.java;
public class JavaVaribales {
// Instance Variables
int x = 15;
// Static Variables
static String message = "Hello World!";
// Main Method
public static void main(String[] args) {
// Local variables
int a = 10;
int b = 20;
// Some code here
// ---------
// ---------
}
// Method
void myMethod() {
// Local Variables
char ch = 'i';
float num = (float) 1.5;
}
}
3. Java Data Types
As we already discussed in the previous tutorial every Java variable has a data type. Data type tells us the different sizes and values that can be stored in a variable.Java has two Data Types:
- Primitive Data Types
- Non-Primitive Data Types
3.1. Primitive Data Types
Primitive data types are built-in within Java language. No additional methods are required for it. There are 8 primitive data types:
- byte
- short
- int
- long
- float
- double
- boolean
- char
Data Type | Default Value | Default size |
---|---|---|
byte | 0 | 1 byte |
short | 0 | 2 bytes |
int | 0 | 4 bytes |
long | 0L | 8 bytes |
float | 0.0f | 4 bytes |
double | 0.0d | 8 bytes |
boolean | false | 1 bit |
char | '\u0000' | 2 bytes |
1. byte :
Its value range lies between -128 to 127. Byte is used in place of int to save memory as a byte is 4 times smaller than an integer. Its default value is 0.
Example:
byte x = 10;
byte y = 20;
2. short :
The range of short data type lies between -32,768 to 32,767. The short data type can again be used to save memory instead of int. Short is 2 times smaller than int. Its default value is 0.
Example:
short s = 12000;
short t = -10000;
3. int :
int is also known as an integer. Its range value lies between -2,147,483,648 to 2,147,483,647. Its default value is 0. This is the most commonly used data type for integral values.
Example:
int x = 10;
int y = 2000;
4. long :
The long data type is 8 bytes. Its value range lies between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. long data type is used when a large range of values is required which are not provided by int. Its default value is 0.
Example:
long l = 100000L;
long m = -300000L;
5. float :
The float data type is used for decimal values. Its range is unlimited. float is recommended over double data type to save memory. Its default value is 0.0F.
Example:
float f = 20.5f;
6. double :
double data type is generally used for decimal values just like float. Its range is unlimited. Its default value is 0.0d.
Example:
double d = 10.5;
7. boolean :
The boolean data type can only store one of the two values: true or false.
Example:
boolean flag = false;
boolean isJavaInteresting = true;
8. char :
The char data type is used to store characters. The character must be within single quotes ('a').
Example:
char grade = 'A';
3.2. Non-Primitive Data Types
Non-Primitive data types are not defined by Java and are created by programmers except Strings. Unlike primitive, non-primitive data types can be null. Non-Primitive data types are:
- Strings
- Arrays
- Classes
- Interface etc.
i. Strings :
The String data type is used to store the sequence of characters or text. Strings must be declared within double quotes like "Hello".
Example:
String myName = "John";
Also, read - Java Tutorials Series
Next >> Java Modifiers
Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.
Comments
Post a Comment