Java Variables

JK1982 
Created at
Updated at  

  7,239   0   0  

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

  • String - stores text, such as "Hello". String values are surrounded by double quotes
  • int - stores integers (whole numbers), without decimals, such as 123 or -123
  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • boolean - stores values with two states: true or false

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:

type variableName = value;

Where type is one of Java's types (such as int or String), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

To create a variable that should store text, look at the following example:

String name = "John";
System.out.println(name);

 

To create a variable that should store a number, look at the following example:

int myNum = 15;
System.out.println(myNum);

 

You can also declare a variable without assigning the value, and assign the value later:

int myNum;
myNum = 15;
System.out.println(myNum);

 

Note that if you assign a new value to an existing variable, it will overwrite the previous value:

int myNum = 15;
myNum = 20;  // myNum is now 20
System.out.println(myNum);

 


Display Variables

The println() method is often used to display variables.

To combine both text and a variable, use the + character:

String name = "John";
System.out.println("Hello " + name);

 

You can also use the + character to add a variable to another variable:

String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);

 

For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):

int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y

From the example above, you can expect:

  • x stores the value 5
  • y stores the value 6
  • Then we use the println() method to display the value of x + y, which is 11

Declare Many Variables

To declare more than one variable of the same type, you can use a comma-separated list:

int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);

You can simply write:

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);

One Value to Multiple Variables

You can also assign the same value to multiple variables in one line:

int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);

 



Tags: Java Java Variables String boolean char float int Share on Facebook Share on X

◀ PREVIOUS
Java Comments

▶ NEXT
Java Math

  Comments 0
SIMILAR POSTS

Java Math

(updated at )

Java Comments

(updated at )

The Print() Method

(updated at )

Java If ... Else

(updated at )

Java Syntax

(updated at )

Java Short Hand If...Else (Ternary Operator)

(updated at )

Java Switch Statements

(updated at )

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue

(updated at )

Java Getting Started

(updated at )

Java Methods

(updated at )

Java Scope

(updated at )

Java Recursion

(updated at )

Java Classes and Objects

(updated at )

Java Packages

(updated at )

Java Inheritance (Subclass and Superclass)

(updated at )

Java Polymorphism

(created at )

What is Java?

(created at )

Java Inner Classes

(updated at )

Machine Learning Types and Programming Languages

(updated at )

Java Tutorials associated with AP Computer Science A

(updated at )

Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows

(created at )

Java Servlet Example

(created at )

Difference between Java and Javascript

(updated at )

Challenge: One Code Problem Per Day

(created at )

OTHER POSTS IN THE SAME CATEGORY

Java Inner Classes

(updated at )

Java Polymorphism

(created at )

Java Inheritance (Subclass and Superclass)

(updated at )

Java Packages

(updated at )

Java Abstract Classes and Methods

(updated at )

Java Classes and Objects

(updated at )

Java Recursion

(updated at )

Java Scope

(updated at )

Java Methods

(updated at )

Java Arrays

(updated at )

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue

(updated at )

Java Switch Statements

(updated at )

Java Short Hand If...Else (Ternary Operator)

(updated at )

Java If ... Else

(updated at )

Java Math

(updated at )

Java Comments

(updated at )

The Print() Method

(updated at )

Java Syntax

(updated at )

Java Getting Started

(updated at )

What is Java?

(created at )

UPDATES

Harness vs. OpenClaw: Two Very Different "Agents"

(updated at )

Clean Python Environments: The Power of venv vs. Docker

(updated at )

What is Docker? Why is Docker also useful in a development environment?

(created at )

UIUC 2026-2027 Academic Calendar

(updated at )

How to Build Llama 3 AI Apps with Python: Setup & User Prompts

(updated at )

Open-Source LLMs: The AI Revolution

(updated at )

Resume 2.0: Leveling Up for My First Software Gig

(created at )

Not everyone will understand what this man just did

(created at )

UIUC Dorm Guide: Find Your Perfect Fit !!

(updated at )

Unpacking IU's Shopper

(created at )

Jackie Chan's Police Story: The Action Masterpiece

(updated at )

The IVE Story: Identity, 'I AM' Charts, and Influence

(updated at )

Tech Visionaries who graduated at UIUC - You are the Next Turn

(updated at )

Open Databases for Sex Crime Occurrences in the U.S.

(updated at )

Automatically copy text to the clipboard when dragging the mouse in the Cursor

(updated at )

My First Day at University of Illinois-Urvana Champaign

(updated at )

Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure

(updated at )

Sun, Rocks, and Adventure: A Day at Joshua Tree National Park

(updated at )

Sipping the Stars: My Starbucks Adventure

(updated at )

Exciting explore at Sequoia National Park

(updated at )

My Life Shot at Death Valley

(updated at )

Ip Man fights with Muay Thai Master

(created at )

Mad Clown - Don't Die

(created at )

How to get Student Enrollment and Degree Verification at UIUC

(updated at )

LAX Thanksgiving Rush: A Joyful Reunion

(updated at )

ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)

(updated at )

FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'

(updated at )

10CM - To Reach You (너에게 닿기를)

(updated at )

Feeling weak? Transform yourself at the UIUC ARC!

(updated at )

BOYNEXTDOOR - If I Say I Love You

(updated at )

The Future of Software Engineer - AI Engineering

(updated at )

G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025

(updated at )

Lie - Legend song by BIGBANG

(updated at )

Why ROLLBACK is useful when you work with Google Gemini CLI?

(created at )

Reimbursement after Vaccination at McKinley Health Center

(created at )

Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!

(created at )

Common Questions from UIUC school life in terms of CS Program

(created at )

UIUC Immunization Compliance

(created at )

LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...

(created at )

LEE CHANHYUK - Endangered Love (멸종위기사랑)

(created at )

Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)

(created at )

Common methods to improve coding skills

(created at )

US National Holiday in 2026

(created at )

BABYMONSTER “WE GO UP” Band LIVE [it's Live] K-POP live music show

(created at )

BLACKPINK - ‘Shut Down’ Live at Coachella 2023

(created at )

JENNIE - like JENNIE - One of Hot K-POP in 2025

(created at )

BABYMONSTER(베이비몬스터) - DRIP + HOT SOURCE + SHEESH

(created at )

Common Naming Format in Software Development

(created at )

In a life where I don't want to spill even a single sip of champagne - LEE CHANHYUK - Panorama(파노라마)

(created at )

Countries with more males and females - what about UIUC?

(created at )