Kamis, 20 April 2017

Tutorial Java - Operator

Java Operators

Operator dipakai untuk melaksanakan operasi pada variabel dan nilai.
Nilai ini disebut operand, sementara operasi (yang dilakukan antara dua operan) ditentukan oleh operator:
OperandOperatorOperand
100+50
Dalam rujukan di bawah ini, angka 100 dan 50 merupakan operand, dan tanda + yakni operator:

Contoh

int x = 100 + 50;
Run example »
Meskipun operator + sering dipakai untuk menambahkan dua nilai, menyerupai pada rujukan di atas, itu juga sanggup dipakai untuk menambahkan bersama variabel dan nilai, atau variabel dan variabel:

Contoh

int sum1 = 100 + 50;        // 150 (100 + 50)int sum2 = sum1 + 250;      // 400 (150 + 250)int sum3 = sum2 + sum2;     // 800 (400 + 400)
Run example »
Java membagi operator ke dalam grup berikut:
  • Operator aritmatika
  • Operator penugasan
  • Operator perbandingan
  • Operator logika
  • Operator Bitwise

Arithmetic Operators

Operator aritmatika dipakai untuk melaksanakan operasi matematika umum.
OperatorNameDescriptionExampleTry it
+PenjumlahanAdds together two valuesx + yTry it »
-PenguranganSubtracts one value from anotherx - yTry it »
*PerkalianMultiplies two valuesx * yTry it »
/PembagianDivides one value from anotherx / yTry it »
%ModulusReturns the division remainderx % yTry it »
++IncrementIncreases the value of a variable by 1++xTry it »
--DecrementDecreases the value of a variable by 1--xTry it »

Java Assignment Operators

Operator penugasan dipakai untuk menetapkan nilai ke variabel.
Pada rujukan di bawah ini, kami memakai operator penugasan (=) untuk menetapkan nilai 10 ke variabel berjulukan x:

Contoh

int x = 10;
Try it Yourself »
Operator penugasan pelengkap (+=) menambahkan nilai ke variabel:

Contoh

int x = 10;
x += 5;
Try it Yourself »
A list of all assignment operators:
OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 3x = x + 3Try it »
-=x -= 3x = x - 3Try it »
*=x *= 3x = x * 3Try it »
/=x /= 3x = x / 3Try it »
%=x %= 3x = x % 3Try it »
&=x &= 3x = x & 3Try it »
|=x |= 3x = x | 3Try it »
^=x ^= 3x = x ^ 3Try it »
>>=x >>= 3x = x >> 3Try it »
<<=x <<= 3x = x << 3Try it »

Java Comparison Operators

Comparison operators are used to compare two values:
OperatorNameExampleTry it
==Equal tox == yTry it »
!=Not equalx != yTry it »
>Greater thanx > yTry it »
<Less thanx < yTry it »
>=Greater than or equal tox >= yTry it »
<=Less than or equal tox <= yTry it »

Java Logical Operators

Operator logika dipakai untuk memilih logika antara variabel atau nilai:
OperatorNameDescriptionExampleTry it
&& Logical andReturns true if both statements are truex < 5 &&  x < 10Try it »
|| Logical orReturns true if one of the statements is truex < 5 || x < 4Try it »
!Logical notReverse the result, returns false if the result is true!(x < 5 ! && x < 10)Try it »

Related Posts