Java Tutorials
Java Methods
Java Classes
Java File Handling
Variables are data storage containers.
In Java, there are different types of variables, for example:
string
- saves text, such as "Hello". Character unit values are surrounded by two quotesint
- keeps numbers (whole numbers), excluding decimals, such as 123 or -123float
- keeps floating point numbers, with decimals, such as 19.99 or 19.99char
- saves single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean
- keeps values in two forms: true or falseTo create a variable, you must specify a type and assign a value:
type variableName = value;
Where type is one of the Java variants (such as int
or string
), and variableName is a variable name (such as x or name). The equals sign is used for variable values.
To create an alternative that should keep the text, see the following example:
Create a variable called name of type String
and assign it the value "John":
String name = "John";
System.out.println(name);
To create an alternative that should keep the number, see the following example:
Create a variable called myNum of type int
and assign it the value 15:
int myNum = 15;
System.out.println(myNum);
You can also declare the variable without giving value, and provide value later:
int myNum;
myNum = 15;
System.out.println(myNum);
Note that if you assign a new value to an existing variant, it will delete the previous value:
Change the value of myNum
from 15
to 20
:
int myNum = 15;
myNum = 20; // myNum is now 20
System.out.println(myNum);
However, you can add a final
keyword if you do not want others (or yourself) to write over existing values (this will declare variables as "last" or "continuous", meaning they are invariant and read only):
Change the value of myNum from 15 to 20:
final int myNum = 15;
myNum = 20; // will generate an error: cannot assign a value to a final variable
Demonstration of a flexible advertising method for other types:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
The println()
method is often used for dynamic displays.
To combine both text and flexibility, use the + character:
String name = "John";
System.out.println("Hello " + name);
You can also use the character +
to add variables to other variables:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);
In numerical values, the character +
acts as a mathematical operator (note that we use the int
(number) variable here):
int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y
In the example above, you can expect:
println()
method to display the number x + y, which is 11To declare more than one variant of the same type, use a comma-separated list:
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
All Java variables must be identified by a different name.
These unique names are called identifiers.
Identifiers can be short words (like x and y) or multiple descriptive words (age, total, volume).
Note: It is recommended to use descriptive words to create understandable and sustainable code:
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
The general rules for flexible design are:
int
or boolean
) cannot be used as words