In Java, variables are the foundation of dynamic, flexible programs. They allow you to store and manipulate data — from a user’s name to the result of a math calculation.

At GoNimbus, we make learning variables super simple so you can write real-world Java programs with confidence.


📦 What Is a Variable?

Think of a variable as a labeled box in memory that stores a value. You give it a name, choose what type of data it holds, and then you can use or change that value as your program runs.


🔠 Java Variable Types (Primitives + Reference)

Java supports many data types, but here are the core ones you’ll use often:

TypeDescriptionExample
intWhole numbers (no decimals)123, -99
floatDecimal numbers (use f at the end)3.14f
charA single character (in single quotes)'A', '9'
booleanTrue or false valuestrue, false
StringA sequence of text (in double quotes)"Hello"

💡 Note: String is not a primitive type — it’s a reference type (a class), but it behaves like a basic type in most cases.


🛠️ Declaring and Assigning Variables

Here’s how you create (declare) a variable:

type variableName = value;

✅ Example 1: Declare and assign a String

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

✅ Example 2: Declare and assign an int

int age = 25;
System.out.println(age);

🕐 Assign Values Later

You don’t have to assign a value right away:

int marks;
marks = 90;
System.out.println(marks);

🔁 Overwriting Values

Java variables are mutable by default — you can change their value after they’re declared.

int count = 10;
count = 20;
System.out.println(count); // Output: 20

🔒 Make It Constant with final

Want to make a variable unchangeable? Use the final keyword:

final int maxScore = 100;
maxScore = 110; // ❌ ERROR: Cannot assign a new value to a final variable

Use final for things like tax rates, constants, or settings you don’t want to accidentally change.


🧪 Practice: Declare Various Types

int myNum = 10;
float pi = 3.14f;
char grade = 'A';
boolean isJavaFun = true;
String greeting = "Welcome to GoNimbus!";

Try printing each of these using System.out.println() in the GoNimbus Java Playground.


🎯 GoNimbus Tips for Writing Better Java Code

✅ Use meaningful variable names:

int a = 25;       // ❌ unclear
int studentAge = 25; // ✅ clear

✅ Stick to camelCase for variable names:

String firstName = "John";

✅ Never start a variable name with a number:

int 1score; // ❌ invalid
int score1; // ✅ valid

✅ Avoid using Java keywords as variable names:

int class; // ❌ invalid, because "class" is a Java keyword

🔜 Coming Next: Java Data Types Explained

Now that you know how to create and use variables, let’s dive deeper into understanding Java’s powerful data types — what they are, when to use them, and how they behave.


Scroll to Top