CodeGym/Java Course/Java Syntax Zero/Operations on int variables

Operations on int variables

Available

1. Evaluating integer expressions

The right side of an assignment operator (equal sign) can be any expression — any combination of numbers, variables, and mathematical operators (+, -, *, /).

You can also use parentheses (). In Java, as in mathematics, expressions inside parentheses are evaluated first, and then what is outside the parentheses.

Multiplication and division have equal precedence and are higher than addition and subtraction.

Examples:

Statement Note
int a = (2 + 2) * 2;
The value of the variable will be 8
int b = (6 - 3) / (9 - 6);
The value of the variable will be 1
int c = (-2) * (-3);
The value of the variable will be 6
int d = 3 / 0;
Executing this statement will produce a "division by zero" error, and the program will terminate.

An expression can also include variables:

Statement Note
int a = 1;
int b = 2;
int c = a * b + 2;
The value of the variable a  will be 1
The value of the variable b  will be 2
The value of the variable c  will be 4

What's more, the same variable can be on both the left and the right of the assignment operator:

Statement Note
int x = 5;
x = x + 1;
x = x + 1;
x = x + 1;
x = x + 1;
x = x + 1;
The value of the variable x  will be 5
The value of the variable x  will be 6
The value of the variable x  will be 7
The value of the variable x  will be 8
The value of the variable x  will be 9
The value of the variable x  will be 10

The point here is that in Java the = symbol does not mean equality. Instead, it is an operator that assigns to the variable on the left of the = sign the calculated value of the expression to the right of the = sign.


2. Division of integers

In Java, dividing an integer by an integer always results in an integer. The remainder of the division operation is discarded. Or, you could say that the result of division is always rounded down to the nearest integer.

Examples:

Statement Result of division Note
int a = 5 / 2;
2.5 The value of the variable a will be 2
int b = 20 / 3;
6.3333(3) The value of the variable b will be 6
int c = 6 / 5;
1.2 The value of the variable c will be 1
int d = 1 / 2;
0.5 The value of the variable d will be 0

2
Task
New Java Syntax,  level 2lesson 4
Locked
Let's change the code
Change the program so that the variable name has the value "Amigo".

3. Remainder of division of integers

Besides addition, subtraction, multiplication, and division of integers, Java also has the modulo operator. It is the percent symbol (%). This operator returns the whole number remainder of dividing an integer by an integer (not the fractional part).

Examples:

Statement Result of division Note
int a = 5 % 2;
2 with a remainder of 1 The value of the variable a will be 1
int b = 20 % 4;
5 with a remainder of 0 The value of the variable b will be 0
int c = 9 % 5;
1 with a remainder of 4 The value of the variable c will be 4
int d = 1 % 2;
0 with a remainder of 1 The value of the variable d will be 1

This is a very useful operator. It is used a lot. For example, to find out whether a number is even or odd, just divide it by 2 and compare the remainder with zero. If the remainder is zero, then the number is even; if it is equal to one, then the number is odd.

Here's what this check looks like:

(a % 2) == 0

where, you guessed it, a % 2 is the remainder of division by 2 (i.e. 0 or 1), and == is used to compare with zero.


2
Task
New Java Syntax,  level 2lesson 4
Locked
A few more corrections
Change the program to display the following text: "Coding in Java". Use variables.

4. Increment and decrement

In programming, increasing or decreasing a variable by one are very common operations. There are special commands for these actions in Java:

The increment (increment by one) operator looks like this:

a++;
Increment

This statement is exactly the same as a = a + 1; It increases the variable a by one.

The decrement (decrement by one) operator looks like this:

a--;
Decrement

This statement is exactly the same as a = a - 1; It decreases the variable a by one.

Examples

Statement Note
int x = 5;
x++;
x++;
x++;
x++;
x++;
The value of the variable x  will be 5
The value of the variable x  will be 6
The value of the variable x  will be 7
The value of the variable x  will be 8
The value of the variable x  will be 9
The value of the variable x  will be 10
int x = 5;
x--;
x--;
x--;
x--;
x--;
x--;
The value of the variable x  will be 5
The value of the variable x  will be 4
The value of the variable x  will be 3
The value of the variable x  will be 2
The value of the variable x  will be 1
The value of the variable x  will be 0
The value of the variable x  will be -1

2
Task
New Java Syntax,  level 2lesson 4
Locked
I'm 15 again!
Change the program so that the variable age has the value 15.
Comments (53)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Anonymous #11487639
Level 2 , France, France
14 March 2024, 13:50
And now there's no way to continue due to being behind a paywall. Come on, CodeGym. Love the material, but making to Lesson 2 in the first Quest is too soon to be forced to pay to continue due to tasks being blocked and limited to Premium users.
ameliorate
Level 2 , CodeGym University in India, India
3 March 2024, 19:31
this is my lesson ... ofc i don't have that much money as a student ,,, but for sure will buy it in future if i got placed!!! 😖🤐.. wish me luck ....
Kevin
Level 5 , United States
12 February 2024, 11:01
Cancel my last comment, was given promotional offer for $99 for a year.
Kevin
Level 5 , United States
12 February 2024, 10:22
I'll pay $50 to unlock the first 40 levels. (offer). Student here. University doesn't offer a programming lab.
6 October 2023, 14:04
Nice freemium package ... thumbs up. I like the way the course is dished out but i must confes, Codegym.cc is not as popular as it should be... this site should be the go to place for java...
Kartik Noula
Level 2 , CodeGym University in India, India
1 October 2023, 13:55
code gym is not free??
John Squirrels Website Admin at CodeGym
2 October 2023, 08:49
In our Java course, you have an opportunity to pass the introductory level and the Games section for free. In order to continue learning one of our subscriptions is required.
Anonymous #11367710
Level 7 , Bowie, United States
31 July 2023, 03:51
Why does 9 is an inverted 6 allow you to use a for loop to repeatedly apply the increment operator on six instead of typing it out multiple times?
dmpc92
Level 2 , Houston, United States
20 August 2023, 23:23
Progression...I am sure they will get there in the course, these are the first levels.
15 July 2023, 19:59
So interesting
4 dns
Level 2 , Romania
2 May 2023, 14:41
i love this but its fucked up that you have to buy premium to complete tasks
Atanepes Gochmyradov
Level 2 , Romania
3 July 2023, 18:00
Ya That's pretty bad that you have to pay in the other hand its free to learn
Anonymous #11335583
Level 2 , San Francisco, United States
27 April 2023, 19:48
I can pay premiun help me