0% found this document useful (0 votes)
10 views5 pages

Java Variables

The document provides an overview of Java variables, explaining their components: data type, variable name, and value. It includes examples of variable declaration and initialization, along with naming rules for variables in Java. The document emphasizes the importance of meaningful names and adherence to Java naming conventions.
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)
10 views5 pages

Java Variables

The document provides an overview of Java variables, explaining their components: data type, variable name, and value. It includes examples of variable declaration and initialization, along with naming rules for variables in Java. The document emphasizes the importance of meaningful names and adherence to Java naming conventions.
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

5/12/26, 1:05 PM Java Variables - GeeksforGeeks

Courses
Search... Tutorials Sign In
Interview Prep
Java Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Collection

Java Variables
Last Updated : 20 Apr, 2026

In Java, variables are containers used to store data in memory. Variables define how data is stored,
accessed, and manipulated. A variable in Java has three components,

Data Type: Defines the kind of data stored (e.g., int, String, float).
Variable Name: A unique identifier following Java naming rules.
Value: The actual data assigned to the variable.

class Geeks {
public static void main(String[] args) {

// Declaring and initializing variables

// Integer variable
int age = 25;

// String variable
String name = "GeeksforGeeks";

// Double variable
double salary = 50000.50;

// Displaying the values of variables


[Link]("Age: " + age);
[Link]("Name: " + name);
[Link]("Salary: " + salary);
}
}

Output

Age: 25
Name: GeeksforGeeks
Salary: 50000.5

Declaration
The image below demonstrates how we can declare a variable in Java:

[Link] w w .[Link]/java/variables-in-java/ 1/5


5/12/26, 1:05 PM Java Variables - GeeksforGeeks

Variable Declaration

From the image, it can be easily perceived that while declaring a variable, we need to take care of two
things that are data type of the variable and name.

Initialization
It can be perceived with the help of 3 components explained above:

Variable Initialization

Example: Here, we are initializing variables of different types like float, int and char.

class Geeks{
public static void main(String[] args) {
// Declaring and initializing variables

// Initializing float variable


float si = 5.5f;

// Initializing integer variables


int t = 10;
int s = 20;

[Link] w w .[Link]/java/variables-in-java/ 2/5


5/12/26, 1:05 PM Java Variables - GeeksforGeeks

// Initializing character variable


char var = 'h';

// Displaying the values of the variables


[Link]("Simple Interest: " + si);
[Link]("Speed: " + s);
[Link]("Time: " + t);
[Link]("Character: " + var);
}
}

Output

Simple Interest: 5.5


Speed: 20
Time: 10
Character: h

Naming Rule 's of Java Variables


Start with a Letter, $, or _ – Variable names must begin with a letter (a–z, A–Z), dollar sign $, or
underscore _.
No Keywords: Reserved Java keywords (e.g., int, class, if) cannot be used as variable names.
Case Sensitive: age and Age are treated as different variables.
Use Letters, Digits, $, or _ : After the first character, you can use letters, digits (0–9), $, or _.
Meaningful Names: Choose descriptive names that reflect the purpose of the variable (e.g.,
studentName instead of s).
No Spaces: Variable names cannot contain spaces.
Follow Naming Conventions: Typically, use camelCase for variable names in Java (e.g., totalMarks).

Related articles:

Scope of Variables in Java


Types of Variables

[Link] w w .[Link]/java/variables-in-java/ 3/5


5/12/26, 1:05 PM Java Variables - GeeksforGeeks

Variables and Primitive DataTypes Visit Course

Suggested Quiz 5 Questions

Which of the following is true about variable names in Java?

A Variable names can start with a number

B Variable names can contain special characters like @ or #

C Variable names are case-sensitive

D Variable names can be a Java keyword

Login to View Explanation 1/5 < Previous Next >

Comment K kartik 659

Article Tags: Java java-basics

Explore
Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java
Java Courses

[Link] w w .[Link]/java/variables-in-java/ 4/5


5/12/26, 1:05 PM Java Variables - GeeksforGeeks

Company Explore Tutorials Courses Videos Preparation


About Us POTD Programming ML and Data DSA Corner
Corporate & Communications Address: Legal Job-A- Languages Science Python Interview
Privacy Thon DSA DSA and Java Corner
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Policy Blogs Web Placements C++ Aptitude
Pradesh (201305) Contact Us Nation Technology Web Web Puzzles
Advertise Skill Up AI, ML & Data Development Development GfG 160
Registered Address:
with us Science Programming Data Science System
K 061, Tower K, Gulshan Vivante GFG DevOps Languages CS Subjects Design
Apartment, Sector 137, Noida,
Corporate CS Core DevOps &
Gautam Buddh Nagar, Uttar Pradesh,
201305 Solution Subjects Cloud
Campus Interview GATE
Training Preparation Trending
Program Software and Technologies
Tools

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] w w .[Link]/java/variables-in-java/ 5/5

You might also like