Page 247 - PowerPoint Presentation
P. 247
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
Example3: ~ and !
class OperatorExample {
public static void main(String[]args) {
int a = 10;
int b= -10
boolean c= true;
boolean d = false;
System.out.println(~a); // -11 (minus of total positive value which starts from 0)
System.out.println(~b); // 9 (positive of total minus positive starts from 0)
System.out.println(!c); // false (opposite of Boolean value)
System.out.println(!d) // true
} }
Java Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication,
division and getting the remainder. They act as basic mathematical operations.
class OperatorExample {
public static void main(String[]args) {
int a=10, b=5;
System.out.println( a+b ); Output:
System.out.println( a-b ); 15
System.out.println( a*b ); 5
System.out.println( a/b ); 50
System.out.println( a%b ); 2
} } 0
Java Assignment Operators
Assignment Operator is used to perform a specific operation and then assigning the
result as the new value of the variable.
Operator Operation Equivalent to
+= A += B A = A + B
-= A -= B A = A – B
*= A *= B A = A * B
/= A /= B A = A / B
%= A %= B A = A % B
Example:
class OperatorExample {
public static void main(String[]args) {
int a=10, b=5;
System.out.println( a += b ); // ( new value of a is 15)
System.out.println( a -= b ); // (new value of a is 5)
System.out.println( a *=b ); // (new value of a is 50)
System.out.println( a /= b ); // (new value of a is 0)
System.out.println( a %= b ); // (new value of a is 0)
} }
23