Label statement can be referenced by the break and continue statements.
Spreading a single declaration over several lines
public class MainClass {
public static void main(String[] arg) {
int a = 0, // comment for a
b = 0, // comment for b
c = 0, // comment for c
d = 0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
How to write multiple assignments in a single statement
public class MainClass{
public static void main(String[] argv){
int a, b, c;
a = b = c = 777;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
777
777
777 Combining both statements into one
class Animal {
public Animal(String aType) {
type = aType;
}
public String toString() {
return "This is a " + type;
}
private String type;
}
public class MainClass {
public static void main(String[] a) {
System.out.println(new Animal("a").getClass().getName()); // Output the
// class name
}
}
|
Statement Blocks - You can have a block of statements enclosed between braces.
- If the value of expression is true, all the statements enclosed in the block will be executed.
- Without the braces, the code no longer has a statement block.
public class MainClass {
public static void main(String[] arg) {
int a = 0;
if (a == 0) {
System.out.println("in the block");
System.out.println("in the block");
}
}
} sumber disini
|
No comments:
Post a Comment