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

Java Variables and Data Types With EXAMPLE

This document discusses Java variables and data types. It defines a variable as a container that stores data during program execution and notes that every variable has a data type. It explains that to use a variable, it must be declared and initialized. The document outlines the different types of variables in Java including local, instance, and static variables. It also defines Java's primitive and non-primitive data types and provides examples of variable declaration, initialization, and type conversion.

Uploaded by

yiheyis alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Java Variables and Data Types With EXAMPLE

This document discusses Java variables and data types. It defines a variable as a container that stores data during program execution and notes that every variable has a data type. It explains that to use a variable, it must be declared and initialized. The document outlines the different types of variables in Java including local, instance, and static variables. It also defines Java's primitive and non-primitive data types and provides examples of variable declaration, initialization, and type conversion.

Uploaded by

yiheyis alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.

html

Java Variables and Data Types with EXAMPLE


By James Hartman Updated August 27, 2021

What is a Variable in Java?


Variable in Java is a data container that
stores the data values during Java
program execution. Every variable is
assigned data type which designates
the type and quantity of value it can
hold. Variable is a memory location
name of the data. The Java variables
have mainly three types : Local,
Instance and Static.

In order to use a variable in a program you to need to perform 2 steps

1. Variable Declaration
2. Variable Initialization

In this tutorial, you will learn-

• Variable Declaration
• Variable Initialization
• Types of variables
• Data Types in Java
• Type Conversion & Type Casting

Variable Declaration:
To declare a variable, you must specify the data type & give the variable a unique
name.

1 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

int a,b,c;

float pi;

double d;

char a;

Variable Initialization:
To initialize a variable, you must assign it a valid value.

Example of other Valid Initializations are

pi =3.14f;

do =20.22d;

2 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

Example :

int a=2,b=4,c=6;

float pi=3.14f;

double do=20.22d;

char a=’v’;

Types of variables
In Java, there are three types of variables:

1. Local Variables
2. Instance Variables
3. Static Variables

1) Local Variables
Local Variables are a variable that are declared inside the body of a method.

2) Instance Variables
Instance variables are defined without the STATIC keyword .They are defined Outside a
method declaration. They are Object specific and are known as instance variables.

3) Static Variables
Static variables are initialized only once, at the start of the program execution. These

3 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

What is Data Types in Java?


Data Types in Java are defined as specifiers that allocate di�erent sizes and types of
values that can be stored in the variable or an identifier. Java has a rich set of data
types. Data types in Java can be divided into two parts :

1. Primitive Data Types :- which include integer, character, boolean, and float
2. Non-primitive Data Types :- which include classes, arrays and interfaces.

4 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

float (4 bytes)

double (8 bytes)

Textual Data Type

char (2 bytes)

Logical

boolean (1 byte) (true/false)

Java Data Types


Data Type Default Value Default size

byte 0 1 byte

5 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

Java Variable Type Conversion & Type Casting


A variable of one type can receive the value of another type. Here there are 2 cases –

Case 1) Variable of smaller capacity is be assigned to another variable of bigger


capacity.

This process is Automatic, and non-explicit is known as Conversion

Case 2) Variable of larger capacity is be assigned to another variable of smaller


capacity

6 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

public static void main(String args[]) {


byte x;
int a = 270;
double b = 128.128;
System.out.println("int converted to byte");
x = (byte) a;
System.out.println("a and x " + a + " " + x);
System.out.println("double converted to int");
a = (int) b;
System.out.println("b and a " + b + " " + a);
System.out.println("\ndouble converted to byte");
x = (byte)b;
System.out.println("b and x " + b + " " + x);

7 of 9 9/20/21, 10:12
Java Variables and Data Types with EXAMPLE https://www.guru99.com/java-variables.html

About
About Us
Advertise with Us
Write For Us

8 of 9 9/20/21, 10:12

You might also like