Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Examples of Java Operators: 1. Assignment Operator

The document describes different types of operators in Java including assignment, arithmetic, unary, equality and relational, logical, and ternary operators. Assignment operators assign values to variables. Arithmetic operators perform mathematical operations like addition and subtraction. Unary operators operate on a single operand. Equality and relational operators check relationships between operands and return true or false. Logical operators combine boolean expressions using AND and OR. The ternary operator provides a shorthand if-then-else statement.

Uploaded by

Phantom Sans
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Examples of Java Operators: 1. Assignment Operator

The document describes different types of operators in Java including assignment, arithmetic, unary, equality and relational, logical, and ternary operators. Assignment operators assign values to variables. Arithmetic operators perform mathematical operations like addition and subtraction. Unary operators operate on a single operand. Equality and relational operators check relationships between operands and return true or false. Logical operators combine boolean expressions using AND and OR. The ternary operator provides a shorthand if-then-else statement.

Uploaded by

Phantom Sans
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Examples of Java operators

1. Assignment Operator
1. class AssignmentOperator {
2. public static void main(String[] args) {
3.
4. int number1, number2;
5.
6. // Assigning 5 to number1
7. number1 = 5;
8. System.out.println(number1);
9.
10. // Assigning value of variable number2 to number1
11. number2 = number1;
12. System.out.println(number2);
13. }
14. }

2. Arithmetic Operator
1. class ArithmeticOperator {
2. public static void main(String[] args) {
3.
4. double number1 = 12.5, number2 = 3.5, result;
5.
6. // Using addition operator
7. result = number1 + number2;
8. System.out.println("number1 + number2 = " + result);
9.
10. // Using subtraction operator
11. result = number1 - number2;
12. System.out.println("number1 - number2 = " + result);
13.
14. // Using multiplication operator
15. result = number1 * number2;
16. System.out.println("number1 * number2 = " + result);
17.
18. // Using division operator
19. result = number1 / number2;
20. System.out.println("number1 / number2 = " + result);
21.
22. // Using remainder operator
23. result = number1 % number2;
24. System.out.println("number1 % number2 = " + result);
25. }
26. }
3. Unary Operator
1. class UnaryOperator {
2. public static void main(String[] args) {
3.
4. double number = 5.2, resultNumber;
5. boolean flag = false;
6.
7. System.out.println("+number = " + +number);
8. // number is equal to 5.2 here.
9.
10. System.out.println("-number = " + -number);
11. // number is equal to 5.2 here.
12.
13. // ++number is equivalent to number = number + 1
14. System.out.println("number = " + ++number);
15. // number is equal to 6.2 here.
16.
17. // -- number is equivalent to number = number - 1
18. System.out.println("number = " + --number);
19. // number is equal to 5.2 here.
20.
21. System.out.println("!flag = " + !flag);
22. // flag is still false.
23. }
24. }

4. Equality and Relational Operators


1. class RelationalOperator {
2. public static void main(String[] args) {
3.
4. int number1 = 5, number2 = 6;
5.
6. if (number1 > number2)
7. {
8. System.out.println("number1 is greater than number2.");
9. }
10. else
11. {
12. System.out.println("number2 is greater than number1.");
13. }
14. }
15. }
5. Logical Operators
1. class LogicalOperator {
2. public static void main(String[] args) {
3.
4. int number1 = 1, number2 = 2, number3 = 9;
5. boolean result;
6.
7. // At least one expression needs to be true for result to be true
8. result = (number1 > number2) || (number3 > number1);
9. // result will be true because (number1 > number2) is true
10. System.out.println(result);
11.
12. // All expression must be true from result to be true
13. result = (number1 > number2) && (number3 > number1);
14. // result will be false because (number3 > number1) is false
15. System.out.println(result);
16. }
17. }

6. Ternary Operator
1. class ConditionalOperator {
2. public static void main(String[] args) {
3.
4. int februaryDays = 29;
5. String result;
6.
7. result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
8. System.out.println(result);
9. }
10. }
Assignment Operator - The assignment operator assigns the value on its right to the variable on its left.
Here, 5 is assigned to the variable age using = operator.

Arithmetic Operator - Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication etc

Unary Operator - Unary operator performs operation on only one operand

Equality and Relational Operator - The equality and relational operators determines the relationship
between two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending
on the relationship, it results to either true or false

Logical Operator - The logical operators || (conditional-OR) and && (conditional-AND) operates on boolean
expressions. Here's how they work

Ternary Operator - The conditional operator or ternary operator ?: is shorthand for if-then-
else statement. The syntax of conditional operator is

You might also like