INTRODUCTION
In today's free Java lesson, we will analyze how to define int (integer) variables and how they are used in Java code.
Also, as the next step, with the int variables already defined, we will perform some simple arithmetic operations using arithmetic operators.
int Data Type
In the previous free Java lesson, we saw how to define a variable in its general form. Now, we can extend this theory to define integer variables in Java.
Before we look at our first program, it is important to mention that Java provides four different integer data types:
The most common integer data type is int. In fact, it is not only the most common but also the default data type in Java when referring to integer numbers.
This means that Java always tries to convert all integer numbers to int, unless we explicitly specify a different data type. We will encounter this behavior soon in one of the examples we will write in this section.
Now we are ready to see how to officially define an int variable.
According to Java's rules, to define a variable, we must first specify the data type, then the variable name, and finally, either during the definition or later in the application's code, assign it a value.
So, we have the option to define the variable in two lines of code:
int x;
x = 5;
or, if we know the value in advance, we can assign it directly to the variable.
int x = 5;
We also have the option to define multiple variables in the same line of code when they all share the same data type.
int x1 = 5, x2 = 10;
Each statement or variable definition ends with a semicolon (;
), as required by Java.
Now, let's look at a complete program.
App.java
Output
In this line of code, we define a new integer variable named salary and simultaneously assign it the value 2500.
Here, we define another integer variable named bonus and assign it the value 1200.
As in every programming language, in Java, we can perform arithmetic operations with variables using simple symbols known as arithmetic operators.
In our example, using the addition operator (+), we add the variables salary and bonus. In reality, we are adding the values that each of these variables is equal to. The result of the addition is assigned to a new variable that we define with the name pay.
Now, we want to see the result of the addition, that is, the value contained in the pay variable. To print the result in the terminal, we call the println() method.
Since we don't want to print just the number but also an explanatory message along with the number, we combine a String message ("Your total pay is") with the pay variable. When println() is executed, the pay will be replaced with the value it holds, and the message we defined will be added in front.
Here, the symbol (+) does not perform addition but rather concatenation. When Java sees that the first operand inside println() is a String, it cannot perform addition because it wouldn't make sense to add characters to numbers. Concatenation is the operation where the terms "stick" together side by side. That's why we've left a space between the s and the " to prevent the number from being attached directly to the letter.
If you want to know the maximum or minimum value that a variable can have, you can call the Integer class as shown in the following program.
App.java
Output
Don't forget to make a small donation so that this blog can grow even more and have more features in delivering free lessons online.
full-width
0 Comments
What do you think about Ground of Code?