0% found this document useful (0 votes)
4 views

Variables and Datatypes

The document explains variables and datatypes in Java, categorizing them into primitive and non-primitive types. It details the characteristics of primitive variables, their default values, and the scope of variables, including local, static, and non-static variables. Additionally, it highlights the importance of initialization for local variables and their limited scope within their declaring block.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Variables and Datatypes

The document explains variables and datatypes in Java, categorizing them into primitive and non-primitive types. It details the characteristics of primitive variables, their default values, and the scope of variables, including local, static, and non-static variables. Additionally, it highlights the importance of initialization for local variables and their limited scope within their declaring block.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Variables and Datatypes

• Variable: It is a container which is used to store a


single value.
• We have two types of variables they are:
1. Primitive Variables.
2. Non Primitive Variables.
• Datatypes: Datatypes used to create variables of
specific type.
• In java data types are classified into 2 types.
1. Primitive Datatype.
2. Non Primitive Datatype.
• Primitive data type:
• The data type which is used to create a variable to
store primitive value such as numbers, characters,
Boolean is known as primitive data type.
• Note: All primitive data types are keywords in java.
• Non-Primitive data type:
• The data type which is used to create a non primitive
variable to store the reference is known as Non
Primitive data type.
• Note: Every class name in java is non-primitive data
type.
PRIMITIVE DATA TYPES DEFAULT VALUES

SIZE
PRIMITIVE VALUES

Integer byte 0 1 byte


(Whole number)
+ve to 0 to -ve
short 0 2 byte

int 0 4 byte

Numbers
long 0l/L 8 byte

Floating value float 0.0 f / F 4 byte

double 0.0 d / D 8 byte

Character char /u0000 2 byte

Boolean boolean false 1 bit


Note: The number data type in increasing order of capacity.
byte<short<int<long<float<double
Primitive Variable
• The variable which is used store a primitive value such as numbers,
characters, boolean.
• We can create primitive variable with the help of primitive data
type.
• Syntax:
datatype identifier1,identifier2…
primitive datatype identifier1…..
Example: int a; boolean b;

Non-Primitive Variable:
• The variable which is used to store a reference.
• It is also known as reference variable
• Syntax: Non primitive datatype identifier1, identifier2
• Example: String s=new String();
Scope of Variable
• The visibility of a variable is known as scope of a
variable.
• Based on scope of variables we can categorize variable
in three types.
1. Local Variables
2. Static Variables
3. Non-Static Variables.(Instance)
Local Variables
 Local Variables developed inside a method block or any
other block except class block is known as local
variable.
 Characteristics of Local Variable:
• We can’t use local variables without initialization if we
try to use variable without initialization then we get
compile time error.
• Local variable will not be initialized with default values.
• The scope of the local variable is nested inside the
block whenever it is declared, hence it can’t be used
outside the block.

You might also like