codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

6 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Variables
☕Java Programming

Java Variables

Updated 2026-05-12
15 min read

Java Variables

In this tutorial, you'll learn how to declare and use variables in Java. Variables are fundamental building blocks in programming that allow you to store and manipulate data. Understanding variables is crucial for writing effective and efficient Java programs.

Introduction

Variables act like containers that hold values. Each variable has a name (identifier) and a type, which determines the kind of data it can store. In Java, you must declare the type of each variable before using it. Variables are essential for performing operations, making decisions, and storing results in your programs.

Declaring Variables

To declare a variable in Java, you specify its type followed by the variable name. Here's the basic syntax:

Java
1

For example, to declare an integer variable named age, you would write:

Java
1int age;

After declaring a variable, you can assign it a value using the assignment operator (=):

Java
1age = 25;

Or, you can combine declaration and initialization in one statement:

Java
1int age = 25;

Common Data Types

Java supports several built-in data types. Here are some of the most commonly used ones:

int (Integer)

The int type is used to store whole numbers without a fractional component.

IntExample.java
1public class IntExample {
2 public static void main(String[] args) {
3 int age = 25;
4 System.out.println("Age: " + age);
5 }
6}
Output
Age: 25

float (Floating Point)

The float type is used to store numbers with a fractional component.

FloatExample.java
1public class FloatExample {
2 public static void main(String[] args) {
3 float height = 5.9f;
4 System.out.println("Height: " + height);
5 }
6}
Output
Height: 5.9

String (Text)

The String type is used to store sequences of characters.

StringExample.java
1public class StringExample {
2 public static void main(String[] args) {
3 String name = "Alice";
4 System.out.println("Name: " + name);
5 }
6}
Output
Name: Alice

char (Character)

The char type is used to store single characters.

CharExample.java
1public class CharExample {
2 public static void main(String[] args) {
3 char initial = 'J';
4 System.out.println("Initial: " + initial);
5 }
6}
Output
Initial: J

boolean (Boolean)

The boolean type is used to store true or false values.

BooleanExample.java
1public class BooleanExample {
2 public static void main(String[] args) {
3 boolean isStudent = true;
4 System.out.println("Is Student: " + isStudent);
5 }
6}
Output
Is Student: true

Final Variables

A final variable is a constant whose value cannot be changed once it's assigned. You declare a final variable using the final keyword.

FinalExample.java
1public class FinalExample {
2 public static void main(String[] args) {
3 final int MAX_VALUE = 100;
4 System.out.println("Max Value: " + MAX_VALUE);
5 }
6}
Output
Max Value: 100

Multiple Variables

You can declare multiple variables of the same type in a single statement, separated by commas.

MultipleVariablesExample.java
1public class MultipleVariablesExample {
2 public static void main(String[] args) {
3 int x = 10, y = 20, z = 30;
4 System.out.println("x: " + x);
5 System.out.println("y: " + y);
6 System.out.println("z: " + z);
7 }
8}
Output
x: 10
y: 20
z: 30

Practical Example

Let's create a simple program that calculates the area of a rectangle. The program will use variables to store the length and width, compute the area, and then display the result.

RectangleArea.java
1public class RectangleArea {
2 public static void main(String[] args) {
3 int length = 10;
4 int width = 5;
5 int area = length * width;
6 System.out.println("The area of the rectangle is: " + area);
7 }
8}
Output
The area of the rectangle is: 50

Summary

ConceptDescription
Variable DeclarationSpecifies the type and name of a variable.
Common Data Typesint, float, String, char, boolean.
Final VariablesConstants whose value cannot be changed after initialization.
Multiple VariablesDeclaring multiple variables of the same type in a single statement.

What's Next?

In the next tutorial, we'll explore Java data types in more detail, including primitive and reference types, their ranges, and how they are used in programming. Understanding these concepts will help you write more robust and efficient Java applications.

Stay tuned for more insights into Java programming!


PreviousJava CommentsNext Java Data Types

Recommended Gear

Java CommentsJava Data Types