Selasa, 20 November 2012

Operator



§  Operator adalah simbol dan karakter khusus (matematika) yang digunakan dalam suatu ekspresi
§  Contoh:
      int x = 3;
      int y = x;
      int z = x * y;
      boolean status = true;

Jenis Operator Berdasar Operand
Operator Unary: operator yang melibatkan satu operand
Operator Binary: operator yang melibatkan dua operand
Operator Ternary: operator yang melibatkan tiga operand

Jenis-jenis Operator

     Operator Aritmatika
     Operator Penugasan
     Operator Penggabungan
     Operator Increment dan Decrement
     Operator Bit
     Operator Pembanding
     Operator Logika

Operator Aritmatika


§  Hasil operasi matematika akan mengikuti tipe data  operand
§   Operand bertipe int akan menghasilkan int

Operator Penugasan

  • Operator penugasan berguna untuk memberi nilai ke suatu variabel
  • Operator penugasan menggunakan tanda sama dengan ( = )
  • Operator penugasan digabungkan dengan operator aritmatika membentuk operator penugasan gabungan (compound assignment) 


Operator Penggabungan

§  Operator + dapat digunakan untuk penggabungan String dan String maupun String dan Bilangan
§  Contoh:
System.out.println(“Saya adalah” + “Mahasiswa”);
int mahasiswa = 30;
System.out.println(“Jumlah Mahasiswa” + mahasiswa);

Increment dan Decrement

§  Increment: menambahkan 1 ke nilai variabel


(operator = ++, prefix atau postfix)

int x=5;i                                                                      nt x=5;
y = x++;                                                                        y = ++x;
(nilai saat ini : y = 5, x=6)                                        (nilai saat ini: y = 6, x=6)

§  Decrement: mengurangkan 1 ke nilai variabel


(operator = --)

int  x=5;                                                                       int x=5;
y = x--;                                                                        y = --x;
(nilai saat ini: y = 5, x=4)                                         (nilai saat ini: y = 4, x=4)

Source Code Penambahan.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Nur Lathifah
 */
public class Penambahan {
    public static void main(String[] args){
        int x, y, z;
        x=42;
        y= x++;
System.out.println("hasil saat ini:");
System.out.println("x = "+ x +
                   " y = " + y);
        x= 42;     
        z= ++x;
System.out.println("hasil saat ini:");
System.out.println("x = "+ x +
                    " z = " + z);
    }
}

Source Code Pengurangan.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Nur Lathifah
 */
public class Pengurangan {
    public static void main(String[] args){
        int x, y, z;
        x=42;
        y= x--;
System.out.println("hasil saat ini:");
System.out.println("x = "+ x +
                    " y = " + y);
        x= 42;     
        z= --x;
System.out.println("hasil saat ini:");
System.out.println("x = "+ x +
                    " z = " + z);
    }
}

Source Code Penambahan2.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nur Lathifah
 */
public class Penambahan2 {
    public static void main (String[] args){
        int w, x, y, z;
        w=5;
        x=5;
        y=8 - x++;
        z=8 - ++w;
    System.out.println("hasil saat ini:");
    System.out.println("w = " + w +
                       "x = " + x +
                       "y = " + y +
                       "z = " + z); 
    }
}

Source Code Pengurangan2.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @autho Nur Lathifah
 */
public class Pengurangan2 {
     public static void main (String[] args){
        int w, x, y, z;
        w=5;
        x=5;
        y=8 - x--;
        z=8 - --w;
    System.out.println("hasil saat ini:");
    System.out.println("w = " + w +
                       "x = " + x +
                       "y = " + y +
                       "z = " + z); 
    }
}


Source Code Kali2.java

import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nur Lathifah
 */
public class Kali2 {
    public static void main(String[] args){
        int x, y, z;
        Scanner scan = new Scanner(System.in);
       
        System.out.print("Masukkan angka = ");
        x = (int) scan.nextDouble();
       
        System.out.print("dan ");
        y = (int) scan.nextDouble();
       
        System.out.println("Hasil " + x + " << " + y + " = " + (x << y));  
    }
}
                 
Source Code Bagi2.java

import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nur Lathifah
 */
public class Bagi2 {
    public static void main(String[] args){
        int x, y, z;
        Scanner scan = new Scanner(System.in);
       
        System.out.print("Masukkan angka = ");
        x = (int) scan.nextDouble();
       
        System.out.print("dan ");
        y = (int) scan.nextDouble();
       
        System.out.println("Hasil " + x + " >> " + y + " = " + (x >> y));   
    }
}


Source Code Pembanding.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nur Lathifah
 */
public class Pembanding {
    public static void main(String[] args){
int age = 36;
boolean hasilBanding1 = age < 25;
boolean hasilBanding2 = age != 26;
                                    
System.out.println("Menampilkan Hasil Banding 1 dan 2");
                                    
System.out.println("hasil banding1 = " + hasilBanding1);
System.out.println("hasil banding2 = " + hasilBanding2);
     }
}









0 komentar:

Posting Komentar