Puneri Pattern Pvt.
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 in Java
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.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available
in Java language.
There are 8 types of primitive data types:
o boolean data type
o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
Data Type Default Value Default size
Boolean False 1 bit
Char '\u0000' 2 byte
Byte 0 1 byte
Short 0 2 byte
Int 0 4 byte
Long 0L 8 byte
Float 0.0f 4 byte
Double 0.0d 8 byte
For Puneri Pattern Pvt. Ltd. Students Only.
Puneri Pattern Pvt.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
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.
1. int data=50;//Here data is variable
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
For Puneri Pattern Pvt. Ltd. Students Only.
Puneri Pattern Pvt.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
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.
A local variable cannot be defined with "static" keyword.
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.
Example to understand the types of variables in java
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
Java Variable Example: Add Two Numbers
1. public class Simple{
2. public static void main(String[] args){
3. int a=10;
4. int b=10;
5. int c=a+b;
6. System.out.println(c);
7. }
8. }
For Puneri Pattern Pvt. Ltd. Students Only.
Puneri Pattern Pvt.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
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.
The following are valid Identifiers :
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.
Java Programming Language Keywords :
Abstract continue for new switch
Assert default goto package Synchronized
Boolean do if private This
Break double implements protected Throw
Byte else import public Throws
Case enum instanceof return Transient
Catch Extends int short Try
Char final interface static Void
Class finally long strictfp Volatile
Const float native super While
For Puneri Pattern Pvt. Ltd. Students Only
For Puneri Pattern Pvt. Ltd. Students Only.