---
description: What is a Variable in Java? A variable can be thought of as a container which holds value for you during the life of your program. Learn Java Variables and Data Types with examples.In Java, there are three types of variables: Local Variables, Instance Variables, Static Variables.
title: Java Variables and Data Types
image: https://www.guru99.com/images/java-variables-and-data-types.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Java Variables and Data Types define how a program stores and labels values in memory. This resource explains how to declare and initialize variables, the three variable types, the eight primitive data types, and type conversion versus type casting.

* 📦 **Core Definition:** A variable is a named memory location that holds a value of a specific data type during program execution.
* 📝 **Declare and Initialize:** Declaration sets the data type and name, while initialization assigns a valid value to the variable.
* 🗂️ **Variable Types:** Java has local, instance, and static variables, each with a different scope and lifetime.
* 🔢 **Primitive Data Types:** Eight primitives—byte, short, int, long, float, double, char, and boolean—have fixed sizes across all platforms.
* 🔄 **Conversion and Casting:** Widening conversion is automatic, while narrowing a larger type to a smaller one requires explicit type casting.

[ Read More ](javascript:void%280%29;) 

![Java Variables and Data Types](https://www.guru99.com/images/java-variables-and-data-types.png)

## What is a Variable in Java?

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

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

1. Variable Declaration
2. Variable Initialization

## How to Declare Variables in Java?

To declare a variable, you must specify the data type and give the variable a unique name.

[](https://www.guru99.com/images/uploads/2012/07/VriableTypeNameDeclaration.jpg)

Examples of other valid declarations are:

int a, b, c;

float pi;

double d;

char a;

## How to Initialize Variables in Java?

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

[](https://www.guru99.com/images/uploads/2012/07/java-varibale-initialization.jpg)

Examples of other valid initializations are:

pi = 3.14f;

do = 20.22d;

a = 'v';

You can combine variable declaration and initialization.

[](https://www.guru99.com/images/uploads/2012/07/intialization%5F%5Fdeclaration.jpg)

Example:

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

float pi = 3.14f;

double do = 20.22d;

char a = 'v';

### RELATED ARTICLES

* [Arrays in Java ](https://www.guru99.com/java-arrays.html "Arrays in Java")
* [Java Tutorial for Beginners: Complete Guide ](https://www.guru99.com/java-tutorial.html "Java Tutorial for Beginners: Complete Guide")
* [Insertion Sort Algorithm in Java with Program Example ](https://www.guru99.com/insertion-sort-java.html "Insertion Sort Algorithm in Java with Program Example")
* [TOP 50 WebLogic Interview Questions and Answers (2026) ](https://www.guru99.com/weblogic-interview-questions.html "TOP 50 WebLogic Interview Questions and Answers (2026)")

## Types of Variables in Java

In Java, there are three types of variables:

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

### 1) Local Variables

Local variables are variables that are declared inside the body of a method.

### 2) Instance Variables

Instance variables are defined without the static keyword. They are declared outside any method, constructor, or block. These variables are specific to each instance of a class and are known as instance variables.

### 3) Static Variables

Static variables are initialized only once, at the time of class loading, before the execution of the program starts. These variables are shared among all instances of a class and are initialized before any instance variables.

## Types of Variables in Java with Examples

class Guru99 {
    static int a = 1; //static variable
    int data = 99; //instance variable
    void method() {
        int b = 90; //local variable
    }
}

## What is Data Types in Java?

**Data Types in Java** are defined as specifiers that allocate different sizes and types of values that can be stored in a 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.

[](https://www.guru99.com/images/java/DataTypes.png)

### Primitive Data Types

Primitive data types are predefined and available within the Java language. Primitive values do not share state with other primitive values.

There are 8 primitive types: byte, short, int, long, char, float, double, and boolean.

**Integer data types**

byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)

[](https://www.guru99.com/images/uploads/2012/07/java-varaibles.jpg)

**Floating Data Type**

float (4 bytes)

double (8 bytes)

**Textual Data Type**

char (2 bytes)

**Logical**

boolean (1 byte) (true/false)

| Data Type | Default Value | Default size |
| --------- | ------------- | ------------ |
| byte      | 0             | 1 byte       |
| short     | 0             | 2 bytes      |
| int       | 0             | 4 bytes      |
| long      | 0L            | 8 bytes      |
| float     | 0.0f          | 4 bytes      |
| double    | 0.0d          | 8 bytes      |
| boolean   | false         | 1 bit        |
| char      | ‘(null)’      | 2 bytes      |

**Points to Remember:**

* All numeric data types are signed (+/-).
* The size of data types remains the same on all platforms (standardized).
* The char data type in Java is 2 bytes because it uses the **UNICODE** character set. By virtue of it, Java supports internationalization. UNICODE is a character set that covers all known scripts and languages in the world.

## 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)** A variable of smaller capacity is assigned to another variable of bigger capacity.

[](https://www.guru99.com/images/uploads/2012/07/java-type-conversion.jpg)

This process is automatic and non-explicit, and is known as **_Conversion_**.

**Case 2)** A variable of larger capacity is assigned to another variable of smaller capacity.

[](https://www.guru99.com/images/uploads/2012/07/java-type-cast-operator.jpg)

In such cases, you have to explicitly specify the **type cast operator. This process is known as _Type Casting_.**

In case you do not specify a type cast operator, the compiler gives an error. Since this rule is enforced by the compiler, it makes the programmer aware that the conversion they are about to do may cause some loss in data, and it prevents **accidental losses.**

**Example: To Understand Type Casting**

**Step 1)** Copy the following code into an editor.

class Demo {
 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);
 }
}

**Step 2)** Save, Compile & Run the code.

**Expected Output:**

int converted to byte
a and x 270 14
double converted to int
b and a 128.128 128

double converted to byte
b and x 128.128 -128

This code is editable. Click Run to Compile + Execute   

![]()

## FAQs

🤖 Can AI tools help write and debug Java variable declarations?

Yes. AI coding assistants suggest correct data types, flag uninitialized variables, and explain type-casting errors. They speed up learning, but understanding scope, data types, and casting yourself remains essential for writing correct Java code.

🧠 Does AI change how beginners should learn Java variables?

AI provides instant examples and feedback, which accelerates practice. However, beginners still need to grasp variable scope, primitive sizes, and conversion rules so they can read, debug, and trust the code AI produces.

🗂️ What is the difference between instance and static variables?

Instance variables belong to each object and hold separate values per instance. Static variables are shared across all instances of a class and are created once when the class is loaded into memory.

🔢 What is the default value of a Java variable?

Local variables have no default and must be initialized before use. Instance and static variables default automatically to 0, 0.0, false, or null, depending on their declared data type.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/java-variables-and-data-types.png","url":"https://www.guru99.com/images/java-variables-and-data-types.png","width":"700","height":"250","caption":"Java Variables and Data Types","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/java-variables.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/java-variables.html","name":"Java Variables and Data Types"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/java-variables.html#webpage","url":"https://www.guru99.com/java-variables.html","name":"Java Variables and Data Types","dateModified":"2026-06-30T13:00:05+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/java-variables-and-data-types.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/java-variables.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"Java Variables and Data Types","description":"What is a Variable in Java? A variable can be thought of as a container which holds value for you during the life of your program. Learn Java Variables and Data Types with examples.In Java, there are three types of variables: Local Variables, Instance Variables, Static Variables.","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"dateModified":"2026-06-30T13:00:05+05:30","image":{"@id":"https://www.guru99.com/images/java-variables-and-data-types.png"},"copyrightYear":"2026","name":"Java Variables and Data Types","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Can AI tools help write and debug Java variable declarations?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI coding assistants suggest correct data types, flag uninitialized variables, and explain type-casting errors. They speed up learning, but understanding scope, data types, and casting yourself remains essential for writing correct Java code."}},{"@type":"Question","name":"Does AI change how beginners should learn Java variables?","acceptedAnswer":{"@type":"Answer","text":"AI provides instant examples and feedback, which accelerates practice. However, beginners still need to grasp variable scope, primitive sizes, and conversion rules so they can read, debug, and trust the code AI produces."}},{"@type":"Question","name":"What is the difference between instance and static variables?","acceptedAnswer":{"@type":"Answer","text":"Instance variables belong to each object and hold separate values per instance. Static variables are shared across all instances of a class and are created once when the class is loaded into memory."}},{"@type":"Question","name":"What is the default value of a Java variable?","acceptedAnswer":{"@type":"Answer","text":"Local variables have no default and must be initialized before use. Instance and static variables default automatically to 0, 0.0, false, or null, depending on their declared data type."}}]}],"@id":"https://www.guru99.com/java-variables.html#schema-1126318","isPartOf":{"@id":"https://www.guru99.com/java-variables.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/java-variables.html#webpage"}}]}
```
