Skip to content

Instantly share code, notes, and snippets.

@dimMaryanto93
Last active July 30, 2016 16:35
Show Gist options
  • Save dimMaryanto93/519ff848171cb9924338d28343cb1989 to your computer and use it in GitHub Desktop.
Save dimMaryanto93/519ff848171cb9924338d28343cb1989 to your computer and use it in GitHub Desktop.
Java - Variable dan Scope
import java.time.LocalDate;
import java.util.Date;
public class VariableDate{
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("java.time.LocalDate : "+localDate);
Date date = new Date();
System.out.println("java.util.Date : "+date);
}
}
public class VariableDouble{
public static void main(String[] args) {
Double bilPositif = new Double(10);
Double bilNegatif = -5.4;
Double pertambahan = bilPositif + bilNegatif;
System.out.println("Bilangan Positif : "+ bilPositif);
System.out.println("Bilangan Negatif : "+ bilNegatif);
System.out.println("Perjumlahan antara bilPositif dan bilNegatif adalah "+ pertambahan);
}
}
public class VariableFloat{
public static void main(String[] args) {
float bilPositif = new Float(10);
float bilNegatif = -5.4F;
float pertambahan = bilPositif + bilNegatif;
System.out.println("Bilangan Positif : "+ bilPositif);
System.out.println("Bilangan Negatif : "+ bilNegatif);
System.out.println("Perjumlahan antara bilPositif dan bilNegatif adalah "+ pertambahan);
}
}
public class GlobalVariabel{
private static String nama = "Dimas Maryanto";
public static void main(String[] args) {
System.out.println("ini dari main : "+ nama);
fungsiLain();
}
public static void fungsiLain(){
System.out.println("Ini dari fungsiLain : "+ nama);
}
}
public class VariableInteger{
public static void main(String[] args) {
Integer bilPositif = 10;
Integer bilNegatif = -5;
Integer pertambahan = bilPositif + bilNegatif;
System.out.println("Bilangan Positif : "+ bilPositif);
System.out.println("Bilangan Negatif : "+ bilNegatif);
System.out.println("Perjumlahan antara bilPositif dan bilNegatif adalah "+ pertambahan);
}
}
public class LocalVariabel{
public static void main(String[] args) {
String nama = "Dimas Maryanto";
System.out.println(nama);
}
public void fungsiLain(){
// System.out.println(nama); # variabel nama tidak dikenali
}
}
public class ParamsVariabel{
private static String nama = "Dimas";
public static void main(String[] args) {
fungsiLain("Maryanto");
}
public static void fungsiLain(String nama){
System.out.println(ParamsVariabel.nama + " " + nama);
}
}
public class VariableString{
public static void main(String[] args){
String fistName = "Dimas";
String lastName = "Maryanto";
System.out.println(nama + " "+ lastName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment