Java Programming Operators (JPO)


JPO:   SOLD       




1. Arithmetic Operators 

Arithmetic operators used in Java are almost the same as those used in VB. Only the modulus operator has a different notation. VB uses mod while Java uses the %. Table 8.2 shows the arithmetic operators available in Java.


Operators In Java Programming

Table 8.2. Arithmetic operators in Java

| Operator | Fungsi                                       | Contoh       |
|----------|----------------------------------------------|--------------|
| +        | Penjumlahan                                  | 3 + 5 = 8    |
| -        | Pengurangan                                  | 72 = 5    |
| *        | Perkalian                                    | 5 * 2 = 10   |
| /        | Pembagian                                    | 6 / 3 = 2    |
| %        | Sisa hasil bagi (modulus)                    | 5 / 2 = 1    |
| ++       | Menambahkan nilai 1 ke variabel (increment)  | C++ = C + 1  |
| --       | Mengurangi nilai 1 ke variabel               | C-- = C - 1  |

The following example shows how to use arithmetic operators. Type the code in the following example then compile and run it. Observe the output of the program.

Example 8.4. Use of arithmetic operators

public class DemoAritmatika
{
    public static void main(String[] args)
   {           int i = 21;           int j = 38;           double x = 9.123;           double y = 12.78;       //Cetak nilai variabel
        System.out.println("Nilai Variabel...");
        System.out.println("    i = " + i);
        System.out.println("    j = " + j);
        System.out.println("    x = " + x);
        System.out.println("      y   =   "   +   y);
    //penjumlahan
        System.out.println("Penjumlahan...");
        System.out.println("    i + j = " + (i + j));
        System.out.println("    x + y = " + (x + y));
    //pengurangan
        System.out.println("Pengurangan...");
        System.out.println("    i - j = " + (i - j));
        System.out.println("    x - y = " + (x - y));
    //perkalian
        System.out.println("Perkalian...");
        System.out.println("    i * j = " + (i * j));
        System.out.println("    x * y = " + (x * y));
    //pembagian
        System.out.println("Pembagian...");
        System.out.println("    i / j = " + (i / j));
        System.out.println("    x / y = " + (x / y));
    //modulus
        System.out.println("Sisa Hasil Bagi...");
        System.out.println("    i % j = " + (i % j));
        System.out.println("    x % y = " + (x % y));
    //increment
        System.out.println("Increment...");
        System.out.println("    i++ = " + (i++));
        System.out.println("    ++i = " + (++i));
        System.out.println("    j++ + i = " + (j++ + i));
        System.out.println("    ++j + i = " + (++j + i));
    }
}

2. Relational Operators 

Relational or comparison operators in Java are also similar to VB. They only differ in the comparison of equal and not equal symbols. In VB to compare two operands whether they are equal or not, use the operator = for equal and <> for not equal. While in Java, == is used for equal and != is used for not equal. Type the code in the following example then compile and run. Note the output of the program.

Example 8.5. Using relational operators

public class DemoRelasional
{
    public static void main(String[] args) {           int i = 20;           int j = 16;           int k = 16;      //Cetak nilai variabel
        System.out.println("Nilai variabel...");
        System.out.println("    i = " + i);
        System.out.println("    j = " + j);
        System.out.println("    k = " + k);
   //lebih besar dari
        System.out.println("Lebih besar dari...");
        System.out.println("    i > j = " + (i > j));
        System.out.println("    j > i = " + (j > i));
        System.out.println("    k > j = " + (k > j));
   //lebih besar atau sama dengan
        System.out.println("Lebih besar atau sama dengan...");
        System.out.println("    i >= j = " + (i >= j));
        System.out.println("    j >= i = " + (j >= i));
        System.out.println("    k >= j = " + (k >= j));
   //lebih kecil dari
        System.out.println("Lebih kecil dari...");
        System.out.println("    i < j = " + (i < j));
        System.out.println("    j < i = " + (j < i));
        System.out.println("    k < j = " + (k < j));
   //lebih kecil atau sama dengan
        System.out.println("Lebih kecil atau sama dengan...");
        System.out.println("    i <= j = " + (i <= j));
        System.out.println("    j <= i = " + (j <= i));
        System.out.println("    k <= j = " + (k <= j));
   //sama dengan
        System.out.println("Sama dengan...");
        System.out.println("    i == j = " + (i == j));
        System.out.println("    k == j = " + (k == j));
   //tidak sama dengan
        System.out.println("Tidak sama dengan...");
        System.out.println("    i != j = " + (i != j));
        System.out.println("    k != j = " + (k != j));
    }
}

3. Logical Operators 

There are 3 logical operators provided by Java, namely: && (AND), || (logical OR), | and ! (logical NOT). The use of these operators is exactly the same as in VB. Only the notation is different.


Post a Comment

Previous Next

نموذج الاتصال