Operators
Operators in Java can be classified into 6 types:
1. Arithmetic operators 2. Assignment operators 3. Relational operators 4. Logical operators
5. Unary operators 6. Bitwise operators
1. Arithmetic operators: are used to perform operations on variables and data.
+: addition
-: subtraction
*: multiplication
/: division
%: remainder, modulus, mod
2. Assignment operators: are used in Java to assign values to variables.
=; +=; -=; *=; /=; %=
3. Relational operators: are used to check the relationship between two operands. It returns either true or false
==: is equal to
!=: not equal to
>: greater than
<: less than
>=: greater than equal to
<=: less than equal to
4. Logical operators: are used to check whether an expression is true or false. They are used in decision making.
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
5. Unary operators: are used with only one operand.
+: unary plus. not necessary to use since numbers are positive without using it
-: unary minus. inverts the sign of an expression
++: increment operator
--: decrement operator
!: logical complement operator. inverts the value of a boolean.
6. Bitwise operators: are used to perform operations on individual bits.
~: bitwise complement
<<: left shift
<<: right shift
>>>: unsigned right shift
&: bitwise AND
^: bitwise exclusive OR
|: bitwise OR
Java Ternary operation
boolean b = false;
int x = (int) (Math.random() * 1000);
int y = (int) (Math.random() * 1000);
int z = (int) (Math.random() * 1000);
// x = 100;
// y = 20;
// z = 30;
b = x < y && (x < z ? true : false);
System.out.println(b);
b = x < y ? x < z ? true : false : false;
System.out.println(b);
b = (x < y ? (x < z ? true : false) : false);
System.out.println(b);
Questions:
1.
int i = 0;
System.out.println(i);
i = i++;
System.out.println(i);
i = i++ + 2;
System.out.println(i);
2.
int a = 10, b = 20;
int c = a > 20 ? b > 30 ? 10 : 20 : 30;
System.out.println(c);
Комментарии
Отправить комментарий