Data Types in Java
Data Types in Java
• After the successful compilation of a Java source code, byte code will be generated which
contains by.class files.
• That byte code will be processed further while running the Java program.
How many .class files can we get after successful compilation of a source code?
Ans. On demand we can declare more than one class in same Source Code. . Number of .class
files depends on the number of classes declared in the source code.
Ans. At the time of the execution we can use only those .class files which have main(),
otherwise we get an error: MainMethodNotFound
Ans. No, if we change then we will get error, such as: java.lang.UnsupportedClassVersionError
They define what kind of value a variable can hold, such as numbers, text, or collections of
items.
Data types can describe the various sizes and values that can be stored in that particular
variable.
1. Primitive data type: This particular data type includes float, short, boolean, byte, char,
long, int, and double.
2. Non-primitive data type: This particular data type includes arrays, interfaces, strings,
and classes.
Primitive Data Types Table – Default Value, Size, and Range
Data Default
Default size Range
Type Value
1 byte or 8
byte 0 -128 to 127
bits
2 bytes or 16
short 0 -32,768 to 32,767
bits
4 bytes or 32
int 0 2,147,483,648 to 2,147,483,647
bits
8 bytes or 64 9,223,372,036,854,775,808 to
long 0
bits 9,223,372,036,854,775,807
4 bytes or 32
float 0.0f 1.4e-045 to 3.4e+038
bits
8 bytes or 64
double 0.0d 4.9e-324 to 1.8e+308
bits
2 bytes or 16
char ‘u0000’ 0 to 65536
bits
1 byte or 2
boolean FALSE 0 or 1
bytes
Variable
It is a memory allocation, which we use, to store the data as per their data type.
Variables can be of some specific data type which contains some value according to the
compatibility of their corresponding data type.
final keyword : When we declare a variable along with final keyword/modifier we can't change
their value.
Syntax:
<data_type> <variable_name>;
Variable Declaration
Int- it stores any integer, specifically any whole number without decimal.
Char- it stores characters such as 'x', and 'y'. It is denoted by single quotes.
Initialization of variable
After the declaration of a variable, you can initialize the variable. When we assign the value to a
variable it means we initialize the variable. To initialize a variable, follow this syntax:
variable_Name = value
a = 5;
Examples:
class Jtc{
System.out.println(a);
// a = 4000
System.out.println(a);
// a = -56
System.out.println(a);
Types of variables
Instance variables
Static/class variables
Instance variable
Static variable
vl.variable_m2();
class Variable {
byte b;
short s;
int i;
long l;
float f;
double d;
boolean bl;
char c;
void variable_m2() {
System.out.println("byte : " + b);
Local variable:
When we declare a variable inside the body of a method or block or constructor then it’s
known as a local variable. You can’t access these variables outsides of the method. These
variables are created in memory when the function/block/constructor is called. After
completion of the execution of function/block/constructor, these variables get destroyed
by JVM. We can access these variables only within that block because the scope of these
variables exists only within the block. The initialization of the local variables is mandatory. If you
don’t initialize the variable compiler will throw a runtime exception. Let’s see how to define the
local variable.
• Any variable that is defined inside any block, any method, any constructor or any local context
is called local variable.
• we must have to initialize the local variable before their use
• Local variable memory will be allocated only when that corresponding context is being
processed and immediately after processing the context that memory will be destroyed.
• You can keep same name of localvariable and class name variable but when you are accessing
that local variable in that context itself then that local variable only will be accessed.
int rollNo = 5;
student.studentRollNo();
int rollNo = 5;
System.out.println("Student Roll no is : " + rollNo);
class LocalVariable {
void localVariable_m1() {
System.out.println(b);
void localVariable_m2() {
System.out.println(c);
void localVariable_m3() {
int d = 40; // local variable
System.out.println(d);
System.out.println(new LocalVariable().a);
new LocalVariable().localVariable_m1();
new LocalVariable().localVariable_m2();
new LocalVariable().localVariable_m3();
In Java, a local variable is one that is declared within a method, constructor, or block of code
and is only accessible within that specific scope. Local variables are created when the method,
constructor, or block is entered and are destroyed when it completes. On the other hand, a
global variable, also known as an instance variable or class variable, is declared within a class
but outside of any method or constructor. Global variables are accessible throughout the entire
class and can be accessed by all methods within the class. They exist as long as the class is
loaded in memory. It is important to note that using global variables can lead to potential issues
such as data inconsistency and lack of encapsulation, so it is generally recommended to use
local variables whenever possible.
1 - Local variables:
Local variables are defined within a specific block of code, such as a method or a loop, and are
only accessible within that block.
2) Global variables:
Global variables are declared at the class level, making them accessible throughout the entire
class.
3) Scope:
Local variables have a limited scope and are only available within the block where they are
declared, while global variables have a wider scope and can be accessed from any part of the
class.
4) Memory allocation:
Local variables are stored on the stack and are automatically deleted once the block in which
they are declared finishes executing. On the other hand, global variables are stored in the heap
memory and remain in memory until the program terminates.
5) Accessibility:
Local variables cannot be accessed or modified outside of the block in which they are declared,
providing data encapsulation and improving code readability. Global variables, being accessible
throughout the class, can be modified by any method within the class leading to potential
conflicts and bugs.
6) Lifetime:
Local variables have a shorter lifetime compared to global variables, as they are created and
destroyed each time the block in which they are declared is executed. Global variables persist
throughout the execution of the program.
7) Flexibility:
Using local variables is recommended as they promote good programming practices such as
encapsulation and reduce the chances of unintended side effects. Global variables should be
used judiciously and only when necessary, as they can introduce complexity and make the code
harder to maintain.
2. Global variable in java
The global variable always declares outside the method/block/constructor. It is always get
declared within the body of the class. Unlike local variables, the scope of these variables is not
limited. We can access the global variable within the class or outside the class. Global variables
are categorized in two ways: Instance variable in java and Static/Class variable in java
A variable declared outside the method/block/constructor but inside the class is called
an instance variable in java. We don’t use the static keyword with the instance
variable because if we use static with it, It makes it a class variable and it will be common for all
instances/Objects.
Whenever we create an object of the class, the JVM creates one copy of the instance
variable and associates it with the object. The copy gets destroyed when the object gets
destroyed.
We can specify the access modifier/access specifier for instance variables. By default,
JVM provides the default access specifier.
Initialization of Instance Variable is not Mandatory. Its default value depends on the data
type of variable.
Every object has its own separate copy of the instance variable. If we created two objects of a
class, then each object contains its own instance variables. Let’s understand with an example:
// instance variable
int rollNo;
firstStudent.rollNo = 5;
firstStudent.printStudentRollno();
secondStudent.rollNo = 8;
secondStudent.printStudentRollno();
Output:
Student Roll No: 5
Student Roll No: 8