Contoh Program Literal

Contoh Keyword Break :

public class Main{

public static void main (String[] args){

for (int b=1; b<9;>

if (b==7)

break;

System.out.println(“No: ” + b);

}

}

}
Output Keyword Break :

No: 1

No: 2

No: 3

No: 4

No: 5

No: 6

Contoh Keyword Continue :

class countinueNOlabel{

public static void main(String[] args){

for (int b=1; b<=7; b++){

if (b==4){

continue;

}

System.out.println(“No: ” + b);

}

}

}

Output Keyword Continue :

No: 1

No: 2

No: 3

No: 5

No: 6

No: 7

Contoh Keyword Return :

class Main{
  static int Kuadrat(int bil){
    return bil*bil;
  }
  public static void main(String[] args){
  int x=3;
  System.out.println(x +" kuadrat adalah "+ Kuadrat(x));
  }
}

Output Keyword Return :

3 kuadrat adalah 9

Contoh Method :

class Main{
  static void Cetak(int a){
    System.out.println("Nilai x: "+a);
  }
  public static void main(String[] args) {
  int x;
  for (x=0; x<4;>
    Cetak(x);
  }
  System.out.println("Nilai x terakhir: "+x);
  }
}

Output Method :

Nilai x: 0

Nilai x: 1

Nilai x: 2

Nilai x: 3

Nilai x terakhir: 4

Contoh Class :

class KomputerTemen{
  String merk;
  int tahunBELI;
}
class Main{
  public static void main(String[] args){
    KomputerTemen bernaz = new KomputerTemen();
    bernaz.merk = "Toshiba";
    bernaz.tahunBELI = 2008;
    System.out.println("Merk laptopnya " + bernaz.merk);
    System.out.println("Belinya tahun " + bernaz.tahunBELI);
    }
}


Output Class :

Merk laptopnya Toshiba
Belinya tahun 2008

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s