What is a variable in the Java?

Java

1 answer

Answer

1025628

2026-07-21 06:45

+ Follow

A variable is something that can store data, such as numbers and Words. One of the common types of variables is called "int", which can store numbers.

Creating a variable is simple:

int myVar;

"int" is the data type of the variable, and "myVar" is the name of the variable, you can choose almost any name you want for your variables.

Then you can assign a number to this variable, you can even use negative numbers:

myVar = 5;

Notice how you do not need to type "int" again, you only need to do it once when you create the variable.

Here's how to add numbers to variables:

myVar = myVar + 4;

OR

myVar += 4;

When you do either of these it will add 4 to the value of myVar, which means myVar now equals 9. (5 + 4 = 9)

You can also use subtraction: -

Multiplication: *

Division: /

and Modulus: %

Another imortant data type is the String, a String can store Words and letters, and behaves much like an int.

String myVar;

myVar = "Hello";

myVar += " there, Sir.";

Now, like we did with the int earlier, myVar equals "Hello there, Sir."

One last thing, you can add different variables together, but they must be the same data type:

myVar += anotherVar;

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.