Java Variables | |||
| |||
Variables are containers for storing data values. In Java, there are different types of variables, for example:
Declaring (Creating) Variables To create a variable, you must specify the type and assign it a value:
Where type is one of Java's types (such as To create a variable that should store text, look at the following example:
To create a variable that should store a number, look at the following example:
You can also declare a variable without assigning the value, and assign the value later:
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
Display Variables The To combine both text and a variable, use the
You can also use the
For numeric values, the
From the example above, you can expect:
Declare Many Variables To declare more than one variable of the same type, you can use a comma-separated list:
You can simply write:
One Value to Multiple VariablesYou can also assign the same value to multiple variables in one line:
Tags: Java Java Variables String boolean char float int | |||
| |||
| |||
|
OTHER POSTS IN THE SAME CATEGORYJava Inner ClassesJava PolymorphismJava Inheritance (Subclass and Superclass)Java PackagesJava Abstract Classes and MethodsJava Classes and ObjectsJava RecursionJava ScopeJava MethodsJava ArraysJava While Loop/Do While Loop/For Loop/For-Each Loop/Break/ContinueJava Switch StatementsJava Short Hand If...Else (Ternary Operator)Java If ... ElseJava MathJava CommentsThe Print() MethodJava SyntaxJava Getting StartedWhat is Java? |