-
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Lecture1.7
-
Lecture1.8
-
Lecture1.9
-
Lecture1.10
-
Lecture1.11
-
Lecture1.12
-
Lecture1.13
-
Lecture1.14
-
Java Operators
1. Assignment Operator
You can change the value of a variable in your program by using an assignment statement, which has the general form:
Syntax: target=expression
[php]
a = 10;
b = a;
c = (x – 2)/(2 + 2);
[/php]
2. Arithmetic Operators
Arithmetic operators are used for basic mathematical operations.
+ | addition |
– | subtraction |
* | multiplication |
/ | division |
% | modulus |
[php]
public class ArithmeticExample {
public static void main(String[] args) {
int num1, num2, add, sub, multi, div, modu;
num1 = 5;
num2 = 2;
add = num1 + num2;
sub = num1 – num2;
multi = num1 * num2;
div = num1 / num2;
modu = num1 % num2;
System.out.println("Addition = " + add);
System.out.println("Subtraction = " + sub);
System.out.println("Multiplication = " + multi);
System.out.println("Division = " + div);
System.out.println("Modulus = " + modu);
}
}
[/php]
3. Relational Operators
There are six relational operators that compare values of other types and produce a boolean result:
< | Less than | <= | Less than or equal to |
> | Greater than | >= | Greater than or equal to |
== | Equals | != | Not equals |
[php]
public class RelationalExample {
public static void main(String args[]) {
int a, b, c;
a = 1;
b = 2;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a < b is " +( a<b)); System.out.println("a > b is " +( a>b));
System.out.println("a == c is " +( a==c));
System.out.println("a <= c is " +( a<=c)); System.out.println("a >= b is " +( a>=b));
System.out.println("b != c is " +( b!=c));
System.out.println("b == a+c is " +( b==a+c));
}
}
[/php]
4. Logical Operators
There are three logical operators in Java, these are.
&& | and |
|| | or |
! | not |
[php]
public class LogicalExample {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
[/php]
5. Increment and Decrement Operator
The ++ and — are increment and decrement operator in java, respectively.
The increment operator increase its operand by one. The decrement operator decreases its operand by one. By using increment operator we can write
x = x+1;
as x++;
Both have the same meaning. In the same way, the statement:
x = x-1;
as x--;
[php]
public class IncrementOperator {
public static void main (String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
[/php]
6. Compound Assignment Operators
These are also called the Shorthand operators because it somewhat simplify the use of the operators and the assignment operation.
+= | Assignment by Additional |
-= | Assignment by subtraction |
*= | Assignment by multiplication |
/= | Assignment by division |
%= | Assignment by modulus |
[php]
public class ArithAssignOperator
{
public static void main(String[] args)
{
int a = 10, b = 15, c = 15;
System.out.println("Arithmeic Assignment operators");
System.out.println(" Addition = " + (a += b));
System.out.println(" Subtraction = " + (c -= b));
System.out.println(" Division = " + (a /= 2));
System.out.println(" Multiplication = " + (a *= 2));
}
}
[/php]