Java Variables

JK JK 2038 
Created at
Updated at  
7,588 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 Facebook X
Comments 0
Similar posts
  1. Java Math
    7,538
  2. Java Comments
    8,212
  3. The Print() Method
    7,238
  4. Java If ... Else
    7,509
  5. Java Syntax
    7,560
  6. Java Short Hand If...Else (Ternary Operator)
    7,688
  7. Java Switch Statements
    7,515
  8. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    8,050
  9. Java Getting Started
    7,507
  10. Java Methods
    7,179
  11. Java Scope
    7,205
  12. Java Recursion
    7,272
  13. Java Classes and Objects
    7,206
  14. Java Packages
    7,263
  15. Java Inheritance (Subclass and Superclass)
    7,259
  16. Java Polymorphism
    7,142
  17. What is Java?
    7,167
  18. Java Inner Classes
    7,163
  19. Machine Learning Types and Programming Languages
    7,345
  20. Java Tutorials associated with AP Computer Science A
    7,777
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,274
  22. Java Servlet Example
    7,200
  23. Difference between Java and Javascript
    7,751
  24. Challenge: One Code Problem Per Day
    1,279
  1. How do I replace content that based on the HTML UI Template
    7,236
  2. Dataset of California Foodbanks
    7,250
  3. Java Abstract Classes and Methods
    7,905
  4. Java Arrays
    9,061
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    908
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    806
  3. Michael Jackson's Billie Jean
    395
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    359
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    325
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    356
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    369
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    387
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,291
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    545
  11. The Evolution and Production Reality of Agentic AI
    422
  12. How to Activate or Waive Your UIUC Student Health Insurance
    531
  13. Complete Guide to Building a Machine Learning Model
    553
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,604
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    579
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,348
  17. Clean Python Environments: The Power of venv vs. Docker
    1,096
  18. What is Docker? Why is Docker also useful in a development environment?
    812
  19. UIUC 2026-2027 Academic Calendar
    2,018
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,068
  21. Open-Source LLMs: The AI Revolution
    977
  22. Resume 2.0: Leveling Up for My First Software Gig
    2,982
  23. Not everyone will understand what this man just did
    2,444
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,855
  25. Unpacking IU's Shopper
    975
  26. Jackie Chan's Police Story: The Action Masterpiece
    859
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,196
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,382
  29. Open Databases for Sex Crime Occurrences in the U.S.
    969
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,407
  31. My First Day at University of Illinois-Urvana Champaign
    1,373
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,415
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,484
  34. Sipping the Stars: My Starbucks Adventure
    10,147
  35. Exciting explore at Sequoia National Park
    7,858
  36. My Life Shot at Death Valley
    2,056
  37. Ip Man fights with Muay Thai Master
    1,130
  38. Mad Clown - Don't Die
    1,250
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,863
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,082
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,351
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,147
  43. 10CM - To Reach You (너에게 닿기를)
    1,328
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,738
  45. BOYNEXTDOOR - If I Say I Love You
    1,375
  46. The Future of Software Engineer - AI Engineering
    1,098
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,108
  48. Lie - Legend song by BIGBANG
    7,998
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,099
  50. Reimbursement after Vaccination at McKinley Health Center
    1,216