Core Java 1) Basics of Java 5) Data Types _ Variables
Core Java 1) Basics of Java 5) Data Types _ Variables
Ltd
Affordable & Live Training
Address: P2 304, Pentagon Tower 2, Magarpatta City IT Park, Hadapsar, Pune 28, India. Contact:
8766016640 Email: [email protected] Website: www.puneripattern.com
Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available
in Java language.
Byte 0 1 byte
Short 0 2 byte
Int 0 4 byte
Long 0L 8 byte
Java Variables
A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: local, instance and static.
There are two types of data types in Java: primitive and non-primitive.
Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of "vary + able"
which means its value can be changed.
Types of Variables
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other
methods in the class aren't even aware that the variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it
among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory.
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12. }//end of class
Identifiers :
In Java programming language, an identifier is a name given to a variable, class or method. Identifiers start with a letter, underscore(_) or
dollar sign ($). The following characters can be digits. Identifiers are case sensitive and have no maximum length.
identifier
userName
user_name
_sys_var1
$change
Keywords :
Keywords have special meaning to the Java compiler. They help in indentifying a data type name or program construct name.