A variable is a location in memory (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Learn more about Java identifiers.
Here's an example to declare a variable in Java.
int speedLimit = 80;
Here, speedLimit is a variable of int data type, and is assigned value 80. Meaning, the speedLimit variable can store integer values. You will learn about Java data types in detail later in the article.
In the example, we have assigned value to the variable during declaration. However, it's not mandatory. You can declare variables without assigning the value, and later you can store the value as you wish. For example,
int speedLimit; speedLimit = 80;
The value of a variable can be changed in the program, hence the name 'variable'. For example,
int speedLimit = 80; ... .. ... speedLimit = 90;
Java is a statically-typed language. It means that all variables must be declared before they can be used.
Also, you cannot change the data type of a variable in Java within the same scope. What is variable scope? Don't worry about it for now. For now, just remember you cannot do something like this.
int speedLimit = 80; ... .. ... float speedLimit;
To learn more, visit: Can I change declaration type for a variable in Java?
Java programming language has its own set of rules and conventions for naming variables. Here's what you need to know:
There are 4 types of variables in Java programming language:
You will learn about in later chapters. If you are interested to learn more about it now, visit Java Variable Types.
As mentioned above, Java is a statically-typed language. This means that, all variables must be declared before they can be used.
int speed;
Here, speed is a variable, and the data type of the variable is int
. The int data type determines that the speed variable can only contain integers.
In simple terms, a variable's data type determines the values a variable can store. There are 8 data types predefined in Java programming language, known as primitive data types.
In addition to primitive data types, there are also referenced data types in Java (you will learn about it in later chapters).
boolean
data type has two possible values, either true
or false
.false
.
class BooleanExample {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag);
}
}
When you run the program, the output will be:
true
byte
data type can have values from -128 to 127 (8-bit signed two's complement integer).int
or other integer data types to save memory if it's certain that the value of a variable will be within [-128, 127].
class ByteExample {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range);
// Error code below. Why?
// range = 200
}
}
When you run the program, the output will be:
124
short
data type can have values from -32768 to 32767 (16-bit signed two's complement integer).
class ShortExample {
public static void main(String[] args) {
short temperature;
temperature = -200;
System.out.println(temperature);
}
}
When you run the program, the output will be:
-200
int
data type can have values from -231 to 231-1 (32-bit signed two's complement integer).
class IntExample {
public static void main(String[] args) {
int range = -4250000;
System.out.println(range);
}
}
When you run the program, the output will be:
-4250000
long
data type can have values from -263 to 263-1 (64-bit signed two's complement integer).
class LongExample {
public static void main(String[] args) {
long range = -42332200000L;
System.out.println(range);
}
}
When you run the program, the output will be:
-42332200000
Notice, the use of L
at the end of -42332200000
. This represents that it's an integral literal of long
type. You will learn about integral literals later in this article.
double
data type is a double-precision 64-bit floating point.
class DoubleExample {
public static void main(String[] args) {
double number = -42.3;
System.out.println(number);
}
}
When you run the program, the output will be:
-42.3
float
data type is a single-precision 32-bit floating point. Learn more about single precision and double precision floating point if you are interested.
class FloatExample {
public static void main(String[] args) {
float number = -42.3f;
System.out.println(number);
}
}
When you run the program, the output will be:
-42.3
Notice that, we have used -42.3f
instead of -42.3
in the above program. It's because -42.3
is a double
literal. To tell compiler to treat -42.3
as float
rather than double
, you need to use f or F.
'\u0000'
(0). The maximum value of char data type is '\uffff'
.'\u0000'
class CharExample {
public static void main(String[] args) {
char letter = '\u0051';
System.out.println(letter);
}
}
When you run the program, the output will be:
Q
You get the output Q because the Unicode value of Q is '\u0051'
.
Here is another example:
class CharExample {
public static void main(String[] args) {
char letter1 = '9';
System.out.println(letter1);
char letter2 = 65;
System.out.println(letter2);
}
}
When you run the program, the output will be:
9 A
When you print letter1, you will get 9 because letter1 is assigned character '9'.
When you print letter2, you get A because the ASCII value of 'A'
is 65. It's because java compiler treats character as integral type. Learn more about ASCII.
Java also provides support for character strings via java.lang.String class. Here's how you can create a String object in Java:
myString = "Programming is awesome";
Java String is an important topic which you will learn in detail in later chapters. However, if you are not a newbie in programming and want to learn it now, visit Java String.
To understand literals, let's take an example to assign value to a variable.
boolean flag = false;
Here,
A Literal is the source code representation of a fixed value.
Values like 1.5
, 4
, true
, '\u0050'
that appears directly in a program without requiring computation are literals.
In the above example, flag is a variable. Since, it's a boolean
type variable, it may store either false
or true
. For compiler to understand it, it requires computation. However, literals like -5
, 'a'
, true
represents fixed value.
byte
, short
, int
and long
.l
or L
, it's of type long
. Tip: it is better to use L
instead of l
.
// Error! literal 42332200000 of type int is out of range long myVariable1 = 42332200000; // 42332200000L is of type long, and it's not out of range long myVariable2 = 42332200000L;
// decimal int decNumber = 34; // 0x represents hexadecimal int hexNumber = 0x2F; // 0b represents binary int binNumber = 0b10010;
float
and double
.f
or F
, it's of type float. Otherwise, it's of type double. A double type can optionally end with D
or d
. However, it's not necessary.
class DoubleExample {
public static void main(String[] args) {
double myDouble = 3.4;
float myFloat = 3.4F;
// 3.445*10^2
double myDoubleScientific = 3.445e2;
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myDoubleScientific);
}
}
When you run the program, the output will be:
3.4
3.4
344.5
char
literals, single quotation is used. For example, 'a'
, '\u0111'
etc.String
literals, double quotation is used. For example, "programming"
, "Java 8"
\b
(backspace), \t
(tab), \n
(line feed), \f
(form feed), \r
(carriage return), \"
(double quote), \'
(single quote), and \\
(backslash).
class DoubleExample {
public static void main(String[] args) {
char myChar = 'g';
char newLine = '\n';
String myString = "Java 8";
System.out.println(myChar);
System.out.println(newLine);
System.out.println(myString);
}
}
When you run the program, the output will be:
g Java 8