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

Operations on int variables

New Java Syntax
Level 3 , Lesson 9
Available

1 Evaluating integer expressions

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


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

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


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 will be 2
int b = 20 / 3;
6.3333(3) The value of the variable will be 6
int c = 6 / 5;
1.2 The value of the variable will be 1
int d = 1 / 2;
0.5 The value of the variable will be 0


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 will be 1
int b = 20 % 4;
5 with a remainder of 0 The value of the variable will be 0
int c = 9 % 5;
1 with a remainder of 4 The value of the variable will be 4
int d = 1 % 2;
0 with a remainder of 1 The value of the variable 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.



4 Increment and decrement

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


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

Comments (8)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
David Richard Level 17, Palestine
30 September 2024
I completely don't understand this "%" What does it do?? I have no idea..... Please can someone explain? Like what is the operation to begin with!!?
AHMED ALI GMAIL ALAIL AL-GUNIED Level 6, Hadramout , Yemen
20 July 2023
note: in java left side in assignment operator can not perform any calcuoations: example : int a = 2; a + 2 = 50; // will preduce an error.
David Richard Level 17, Palestine
30 September 2024
Would it work if it's inside the "if" argument? E.g. if (a+2 == 5){}🤨
rags 3 Level 5, Wichita, United States Expert
1 July 2023
so maybe I'm and idiot but like all this info I needed before now i already know it cause needed it to pass the quizzes
abhishe_kira Level 18, India Expert
26 June 2023
You should also tell about ++a and --a .(pre increment and pre decrement) 1.++a(pre Increment) :- int a = 7; int b = 4+(a++); System.out.print(b); the output will be = 11 but if you use pre increment(++a) then int b = 4+(++a); System.out.print(b); the output will be 12; Surprised go and check this by your self. 2. --a (pre decrement) : - int a = 7; int b = 4+ (a--); System.out.print(b); the output will be = 11 But if you will use pre decrement(--a) then int b = 4+(--a); System.out.print(b); the output will be = 14
abhishe_kira Level 18, India Expert
26 June 2023
pre increment and decrement firstly increases or decreases the value of the variable and then assign it. So in the above example the value of a used in calculating the value of b is a++ = 11, while in next case ++a =12. and it is the same case for pre and post decrement for calculating variable b.
Romant Level 15, Russia, Russian Federation
27 February 2022
1+++ = ?
Aurélien Souville Level 25, Nice, France
24 March 2022
It means nothing But you still can use "+=n" ex: x++ x is incremented by 1. x += 2 x is incremented by 2. x += n x is incremented by n. It works for any of + - * / : x*=3 x is multiplied by 3