Chapter 002 (Arithmetic, Assignment, Comparison, Logical) - Operators, String Concatenation
Chapter 002 (Arithmetic, Assignment, Comparison, Logical) - Operators, String Concatenation
In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value,
or a variable and another variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical
operations.
Example
int x = 5;
int y = 3;
System.out.println(x + y);
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println(x - y);
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println(x * y);
System.out.println("----------------------------");
int x = 12;
int y = 3;
System.out.println(x / y);
System.out.println("----------------------------");
int x = 5;
int y = 2;
System.out.println(x % y);
System.out.println("----------------------------");
int x = 5;
++x;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
--x;
System.out.println(x);
System.out.println("----------------------------");
Example
int x = 10;
Example
int x = 5;
x += 3;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
x -= 3;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
x *= 3;
System.out.println(x);
System.out.println("----------------------------");
double x = 5;
x /= 3;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
x %= 3;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
x &= 3;
System.out.println(x);
System.out.println("----------------------------");
int x = 5;
x |= 3;
System.out.println(x);
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
In the following example, we use the greater than operator (>) to find out if 5 is greater
than 3:
Example
int x = 5;
int y = 3;
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println("----------------------------");
int x = 5;
int y = 3;
int x = 5;
int y = 3;
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println("----------------------------");
int x = 5;
int y = 3;
System.out.println(x <= y); // returns false because 5 is neither less than or equal to 3
== Equal to x == y
!= Not equal x != y
Logical operators are used to determine the logic between variables or values:
! Logical not Reverse the result, !(x < 5 && x < 10)
returns false if the
result is true
Example
String firstName = "John";
String lastName = "Doe";
Note that we have added an empty text (" ") to create a space between firstName and
lastName on print.
You can also use the concat() method to concatenate two strings:
Example
String firstName = "John ";
System.out.println(firstName.concat(lastName));