Assignments Operator
In java assignment operator is used to assign a value to a variable.<
Int x=10;
Here variable name is x and value is 10.
class AssignmentOperator {
public static void main(String[] args) {
int a, b;
// Assigning 5 to number1
a = 5;
System.out.println(number1);
// Assigning value of variable b to numbera
b = a;
System.out.println(b);
}
}
Arithmatic Operator
In java arithmetic operator used to perform mathematical operations like addition, subtraction, multiplication, and division.
Operator | Meaning |
---|---|
+ | Addition (also used for string concatenation) |
- | Subtraction Operator |
* | Multiplication Operator |
/ | Division Operator |
% | Remainder Operator |
public class Arithmatic operator{
public static void main(String[] args) {
double a = 16, b = 4, result;
// Using addition operator
result = a + b;
System.out.println("a + b = " + result);
// Using subtraction operator
result = a - b;
System.out.println("a - b = " + result);
// Using multiplication operator
result = a * b;
System.out.println("a * b = " + result);
// Using division operator
result = a / b;
System.out.println("a / b = " + result);
// Using remainder operator
result = a % b;
System.out.println("a % b = " + result);
}
}
public class Arithmatic Operator{
public static void main(String[] args) {
String a, b, result;
a = "Students";
b = "Tutorial";
result=a+b;
System.out.println(result);
}
}
Unary Operator
In java unary operator used to perform action like incrementing/decrementing a value by one and negative an expression.
Operator | Meaning |
---|---|
+ | Unary plus (not necessary to use since numbers are positive without using it) |
- | Unary minus; inverts the sign of an expression |
++ | Increment operator; increments value by 1 |
-- | decrement operator; decrements value by 1 |
! | Logical complement operator; inverts the value of a boolean |
public class Unary Operator{
public static void main(String[] args) {
double a = 10, result a;
boolean b = false;
System.out.println("+a = " + +a);
System.out.println("-a = " + -a);
System.out.println("a = " + ++a);
System.out.println("a = " + --a);
System.out.println("!b = " + !b);
}
}
public class Unary Operator{
public static void main(String[] args) {
double a = 7.8;
System.out.println(a++);
System.out.println(a);
System.out.println(++a);
System.out.println(a);
}
}
Equality and Relational operator
In java equality and relational operators represent the relationship between two operands.
It check wheather it is equal .greater and less.
Operator | Description | Example |
---|---|---|
== | equal to | 8 == 5 is evaluated to false |
!= | not equal to | 8 != 5 is evaluated to true |
> | greater than | 8 > 5 is evaluated to true |
< | less than | 8 < 5 is evaluated to false |
>= | greater than or equal to | 8>= 8 is evaluated to true |
<= | less then or equal to | 8 <= 8 is evaluated to true |
public class Equality and Relational Operators{
public static void main(String[] args) {
int a = 8, b = 6;
if (a > b)
{
System.out.println("a is greater than b.");
}
else
{
System.out.println("b is greater than a.");
}
}
}
public classInstance Operator{
public static void main(String[] args) {
String student = "tutorial";
boolean result;
result = student instanceof String;
System.out.println(result);
}
}
Logical Operator
&&-It is true if all the condition are satisfied.
||-It is true if either one condition is satisfied.
Operator | Description | Example |
---|---|---|
|| | conditional-OR; true if either of the boolean expression is true |
false || true is evaluated to true |
&& | conditional-AND; true if all boolean expressions are true |
false && true is evaluated to false |
public class Logical Opearator{
public static void main(String[] args) {
int a = 3, b= 5, c = 10;
boolean result;
result = (a > b) || (c > a);
System.out.println(result);
result = (a > b) && (c > a);
System.out.println(result);
}
}
public class Ternary Operator{
public static void main(String[] args) {
int a = 28;
int b = 29;
String result;
result = (a > b) ? "a is greater than b" : "b is greater than a";
System.out.println(result);
}
}
b is greater than a