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

Data Types in Java

The document explains the process of generating byte code from Java source code, the role of the Java compiler, and the types of data types in Java, including primitive and non-primitive types. It details variable declaration, initialization, and the differences between local and global variables, as well as instance and static variables. Additionally, it provides examples and emphasizes the importance of scope and memory allocation for different variable types.

Uploaded by

atul
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Types in Java

The document explains the process of generating byte code from Java source code, the role of the Java compiler, and the types of data types in Java, including primitive and non-primitive types. It details variable declaration, initialization, and the differences between local and global variables, as well as instance and static variables. Additionally, it provides examples and emphasizes the importance of scope and memory allocation for different variable types.

Uploaded by

atul
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Byte Code

• 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.

Who is responsible to generate the Byte Code?

Ans. Java Compiler

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.

Q.4 Which .class file can we use at the time of execution?

Ans. At the time of the execution we can use only those .class files which have main(),
otherwise we get an error: MainMethodNotFound

Q.5 Is it possible to change the .class file content manually?

Ans. No, if we change then we will get error, such as: java.lang.UnsupportedClassVersionError

Data Types in Java

They define what kind of value a variable can hold, such as numbers, text, or collections of
items.

What are Data Types in Java?

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

Various types of declaration happen by using Java variables such as

 Strings- which stores text, Such as "Hello world".

 Int- it stores any integer, specifically any whole number without decimal.

 Float- stores "floating point" numbers that include decimal values.

 Char- it stores characters such as 'x', and 'y'. It is denoted by single quotes.

 Boolean- store values if true or false state.

To declare the variable programmers need to focus on two things:

1. Datatype: It denotes the types of Data that are going to be stored.

2. data_name: A particular name of that variable

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{

public static void main(String arg[]){

final int a = 10;

System.out.println(a);

// a = 4000

System.out.println(a);

// a = -56

System.out.println(a);

Types of variables

1. Local variable in java

 A Local variable in the method

 A local variable in a static block

 A local variable in instance block

 A local variable in Constructor

2. Global variable in java

 Instance variables

 Static/class variables

Class Level Variable


• When we declare a variable into a class directly, it is called class level variable.
• We can access a Class level variable from anywhere inside the Program.
• A class level variable gets its memory inside Heap.
• When we do not a initialize a class level variable, then JVM initialize class level variable by
default value.

Class level variable is also of two types:

Instance variable
Static variable

public class JtcClassLevelVariableExample {

public static void main(String[] args) {

Variable vl = new Variable();

vl.variable_m2();

class Variable {

// class level variables

byte b;

short s;

int i;

long l;

float f;

double d;

boolean bl;

char c;

void variable_m2() {
System.out.println("byte : " + b);

System.out.println("short : " + s);

System.out.println("int : " + i);

System.out.println("long : " + l);

System.out.println("float : " + f);

System.out.println("double : " + d);

System.out.println("boolean : " + bl);

System.out.println("char : " + c);

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.

A Local variable in the method


We can create a local variable within the method and use it. But the scope of variables lies
within the method. Because these variables get created when the method gets invoked and
destroyed after the execution of the method. You can’t leave a local variable without a
declaration. It’s mandatory to initialize the local variable.

public class Student

public void studentRollNo()

// local variable rollNo, Its declared in studentRollNo ()

int rollNo = 5;

System.out.println("Student Roll no is : " + rollNo);

public static void main(String args[])

Student student = new Student();

student.studentRollNo();

Output: Student Roll no is : 5

public class Student

public void studentRollNo()

// local variable rollNo, Its declared in studentRollNo ()

int rollNo = 5;
System.out.println("Student Roll no is : " + rollNo);

public static void main(String args[])

// using local variable rollNo outside it's scope

System.out.println("Student Roll no is : " + rollNo);

class LocalVariable {

int a = 10; // class level variable of A class

void localVariable_m1() {

int b = 20; // local variable

System.out.println("--localVariable_m1() in LocalVariable class--");

System.out.println(b);

void localVariable_m2() {

int c = 30; // local variable

System.out.println("--localVariable_m2() in LocalVariable class--");

System.out.println(c);

void localVariable_m3() {
int d = 40; // local variable

System.out.println("--localVariable_m3() in LocalVariable class--");

System.out.println(d);

public class Jtc {

public static void main(String[] args) {

System.out.println(new LocalVariable().a);

new LocalVariable().localVariable_m1();

new LocalVariable().localVariable_m2();

new LocalVariable().localVariable_m3();

Difference Between Local And Global Variable In Java

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

Instance 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.

 To access the instance variable we always need to create the object.

An important point about instance variables

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:

public class Student

// instance variable
int rollNo;

public void printStudentRollno()

System.out.println("Student Roll No: "+rollNo);

public static void main(String args[])

// Creating first object of Student class.

//This object creates separate copy of "rollNo".

Student firstStudent = new Student();

// Assign the roll no to first Student

firstStudent.rollNo = 5;

// print the first student roll no

firstStudent.printStudentRollno();

// Creating second object of Student class.

//This object also creates separate copy of "rollNo".

Student secondStudent = new Student();

// Assign the roll no to second Student

secondStudent.rollNo = 8;

// print the second student roll no

secondStudent.printStudentRollno();

Output:
Student Roll No: 5
Student Roll No: 8

You might also like