Core Java SQL Oracle PHP Examples JSP JDBC MORE

Java operator


  1. Assignments Operator
  2. Arithmatic Operator
  3. Unary Operator
  4. Equality and Relational Operator
  5. Instance Operator
  6. Logical Operator
  7. Increment And Decrement Operator
  8. String Concatenation Operator
  9. Bitwise Operator
  10. Conditional Operator
  11. New Operator
  12. [ ] Operator

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.

Example

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);
}
}

Output :5
5

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

Example

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);
}
}

Output :a + b = 20.0
a - b = 20.0
a * b = 20.0
a / b = 20.0
a % b = 20.0

Example

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);
}
}

Output : StudentsTutorial

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

Example

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);
}
}

Output : +a = 10.0
-a = -10.0
a = 11.0
a = 10.0
!b = true

Example

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);
}
}

Output : 7.8
8.8
9.8
9.8

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

Example

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.");
}
}
}

Output : a is greater than b.

Example

public classInstance Operator{
public static void main(String[] args) {
String student = "tutorial";
boolean result;
result = student instanceof String;
System.out.println(result);
}
}

Output : true

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

Example

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);
}
}

Output : true
false

Ternary Operator Example

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);
}
}

Answer

b is greater than a