1. Declaring Variables
Let’s once again recall how to create variables. To create a variable, you need to write the following command: type name;
Examples:
| Statement | Explanation |
|---|---|
|
A variable s of type String is created. Such a variable can store text. |
|
A variable x of type int is created. Such a variable can store integers. |
|
Variables a, b, c, d of type int are created. Such variables can store integers. |
You cannot create two variables with the same name within the same block of code. However, in different blocks — you can. It’s like boxes sitting in different houses. More about code blocks — in the next lectures.
There are also restrictions on the variable name. On the one hand, it can be anything, but on the other, it cannot contain spaces, the characters +, -, etc. It’s best to use only Latin letters and digits in a variable name.
And as a reminder, in the Java language, case matters — whether you write uppercase or lowercase letters. int a is not the same as Int a.
By the way, in Java you can declare a variable and assign a value at the same time. This saves time and space:
| Compact code | Longer code equivalent to the one on the left |
|---|---|
|
|
|
|
|
|
|
|
|
|
This is much more compact and clearer.
Now that we’ve figured out how to create variables, let’s get acquainted with two of the most commonly used types in Java. These are int (integers) and String (text/strings).
2. The int type
A variable of type int can store integers: 5, -10, 100500. You can also perform various arithmetic operations on int: addition, subtraction, multiplication, division, and others. Examples:
int x = 1; // x will equal 1
int y = x*2; // y will equal 2
int z = 5*y*y + 2*y + 3; // z will equal 20+4+3, which equals 27
int a = 5; // a will equal 5
int b = 1; // b will equal 1
int c = (a-b) * (a+b); // c will equal 4*6, which equals 24
int a = 64; // a will equal 64
int b = a/8; // b will equal 8
int c = b/4; // c will equal 2
int d = c*3; // d will equal 6
If you didn’t skip math in school, everything here should make sense 🧐
3. The String type
The String type allows you to store text strings. To specify a text string in Java, write the text and put double quotes on both sides. Example:
| Code | Explanation |
|---|---|
|
s will contain the text Amigo |
|
s will contain the text 123 |
|
s will contain the text Bond 007 |
Looks simple, right? If so, here’s another interesting fact.
In Java, strings can be concatenated using the plus sign — +. Example:
String s1 = "Amigo" + " the best"; // s1 will contain `Amigo the best`
String s2 = ""; // s2 will contain an empty string — with no characters at all.
int x = 2025;
String s3 = "Amigo" + x; // s3 will contain Amigo2025
Notice the last example: we added a string and a number. Here, too, it’s simple: the number will be converted into a string, then the two strings will be concatenated. When adding strings and numbers, the result is always a string.
4. Printing a Variable to the Screen
Everything seems obvious and elementary. So, can you guess right away which command can print a variable to the screen?
It’s actually simple. To output something to the screen, we use the command System.out.println(), passing what we want to print as the parameter.
System.out.println("Amigo"); // Amigo
System.out.println("Ami" + "go"); // Amigo
String s1 = "Amigo";
System.out.println(s1); // Amigo
String s2 = "Am";
System.out.println(s2 + "igo"); // Amigo
I hope things are a bit clearer now. And whether you’ve understood everything correctly, we’ll check right away. Practice — the criterion of truth: only through practice can you verify how well you’ve understood everything.
GO TO FULL VERSION