This time we'll talk about literals and variables in the Java language.
Literals and variables are components you'll always use when writing a program, because these 2 things connect directly to data and how you manipulate it.
Let's get into it.
Literals in the Java Language
In programming, the term literal means every raw value you type into a program's code. A literal can be a number, a letter, a word, a sentence, a boolean value, and so on.
Still not picturing it? Let's look at this code snippet.
System.out.println("Hello World!");
So which part of that code counts as a literal?
In that code, the literal is “Hello World!”. It's a literal of the string type, or what we call a "string literal".
If you look closely, there's a sign that "Hello World!" is a string literal. A string literal is always wrapped in quote marks (”…”).
That means you can create other string literals like:
“saya makan nasi”"dadu bermata 9""1945""@$#&^*"
All of those are valid string literals.
We mentioned earlier that literals come in many kinds. Each kind has its own traits and rules, matching its data type.
For example:
- Integer literal (whole numbers) —
1945,-24434,0x4b,0b001,0xFeeL - Float literal (decimal numbers) —
3.14159,.25,314E-2L,120f - Boolean literal —
true,false - Character literal —
'a','*','\n','\t' - String literal — you can see the examples above
For the full picture, we'll cover literals in a later piece when we talk about data types.
So what's the point?
Literals can't be separated from programming. Data manipulation means variables. Variables mean literals. If a variable is a container, the literal is what goes inside it. That's why these 2 terms can't be split.
Okay, next we'll talk about variables in Java.
Variables in the Java Language
In programming, a variable is a container for data. You can fill a variable in many ways, including with a literal like the ones above, or with an expression.
Creating a Variable (Initialization)
The main thing about variables is that each one must have a name, usually called an identifier.
The identifier marks the variable and refers to it in a format humans understand.
Beyond the identifier, as a language with static typing, Java also requires every variable to have an explicit data type.
As we touched on earlier, the data type decides which literals you can put into that variable.
More on data types in the Java language in later pieces. If you run into type keywords like
String,int,float, and so on, don't think too hard about what they mean hehe..
In general, the format for creating a variable in Java is:
[tipe_data] [identifier] = [nilai_awal]
For example:
public class VariabelDanLiteral{
public static void main(String[] args) {
String nama = "Inva";
int angka = 125;
float pecahan = 0.12f;
boolean benar = true;
System.out.println("nama: " + nama);
System.out.println("angka: " + angka);
System.out.println("pecahan: " + pecahan);
System.out.println("benar: " + benar);
}
}
If we run it, the output looks like this:
nama: Inva
angka: 125
pecahan: 0.12
benar: true
In that example we create 4 variables with different data types and initial values.
One thing to watch in variable initialization: the initial value must match the data type you give it. If you put in a value or literal that doesn't fit, the program errors out immediately, and the same goes for variable assignment.
For example:
public class VariabelDanLiteral{
public static void main(String[] args) {
String nama = 125; // a number can't go into a string
int angka = "125"; // a string can't go into a number
System.out.println("nama: " + nama);
System.out.println("angka: " + angka);
}
}
If we run it, a data type error will come out, more or less like this:
/VariabelDanLiteral.java:3: error: incompatible types: int cannot be converted to String
String nama = 125; // angka tidak bisa masuk ke string
^
/VariabelDanLiteral.java:4: error: incompatible types: String cannot be converted to int
int angka = "125"; // string tidak bisa masuk ke angka
^
2 errors
The error explains it well enough.
Accessing a Variable's Value
A variable you've created can be accessed by calling its name. When you call its name, the variable gives up the value inside it.
Take the code above. We call the variables nama, angka, pecahan, and benar by writing them as arguments to the println function. That's why what appears on screen is the contents of those variables.
People often wonder what the difference is between calling a variable and writing a string literal. Especially when you have a variable name identical to the literal you want to print.
For example like this:
public class VariabelDanLiteral{
public static void main(String[] args) {
String nama = "Inva"
System.out.println("nama");
System.out.println(nama);
}
}
In that example we create a variable with the identifier nama. Then, in println we pass arguments with values that look the same at a glance.
Remember! To call a variable, you only write its name. In the first println, because we used quote marks, Java treats the argument as an ordinary string literal.
In the second println, we call the nama variable, so what comes out is the contents of that variable, "Inva".
So the output looks like this:
nama
Inva
You can see the difference.
Calling a variable also works inside data operations, like addition, subtraction, and so on. For example like this:
public class VariabelDanLiteral{
public static void main(String[] args) {
int a = 9;
int b = 12;
String namaDepan = "Inva";
String namaBelakang = "sikode";
// the value of variable jumlah is 9 + 12 = 21
int jumlah = a + b;
// the value of variable namaLngkp is Invasikode
String namaLengkap = namaDepan + namaBelakang;
System.out.println(jumlah);
System.out.println(namaLengkap);
}
}
Output:
21
Invasikode
We'll cover data type operations more in later pieces.
Putting a Value into a Variable (Assignment)
As a data container, a variable's contents can also change. That change happens by putting in new data, which we call assignment.
To do an assignment, call the name of a variable you've already initialized, followed by an equals sign (=) and the value you want to put in.
For example like this:
public class VariabelDanLiteral{
public static void main(String[] args) {
int a = 0; // initialize variable a
a = 12; // change the contents of a from 9 to 12
System.out.println(a);
}
}
If we run it, the number that comes out is 12.
Besides using a literal directly, you can also use an expression to fill a variable. For example like this:
public class VariabelDanLiteral{
public static void main(String[] args) {
int a = 0; // initialize variable a
a = 10 * 3 - 3; // value of a = 27
System.out.println(a);
}
}
If we run that program, what comes out is 27. That's because Java automatically calculates the arithmetic in the assignment to variable a.
Now, what if you call a variable to fill itself? What do you think happens? Let's try.
public class VariabelDanLiteral{
public static void main(String[] args) {
int a = 10; // initialize variable a
a = a * a; // value of a = ?
System.out.println(a);
}
}
In that program we create variable a with the initial value 10. On the next line we do the assignment a = a * a, meaning we try to put the value of variable a into itself.
Let's run it.
100
Well now, the value 100 comes out. Where does that come from?
Following the expression we typed in the program, when you call a variable the identifier is replaced with its value.
Since here the value of a is 10, the assignment a = a * a reads as a = 10 * 10, and operating on that gives a = 100. That's where the 100 comes from.
Simply put, with an assignment you can read it from right to left. The value on the right of the equals sign is the new value, while the left side is the variable whose value changes.
As an experiment, run the following code and figure out why the output comes out that way.
public class VariabelDanLiteral{
public static void main(String[] args) {
int a = 10; // initialize variable a
a = a * a;
a = a * a;
a = a * a;
System.out.println(a);
}
}
Next Up
Now that you know about variables and literals in Java, next time we'll cover how to make your code more interactive with the output system.
Happy coding 👋