Java Tutorial/Statement Control/Statement

Материал из Java эксперт
Перейти к: навигация, поиск

An Overview of Java Statements

  1. In programming, a statement is an instruction to do something.
  2. It controls the sequence of execution of a program.
  3. In Java, a statement is terminated with a semicolon and multiple statements can be written on a single line.



   <source lang="java">

x = y + 1; z = y + 2;</source>





Combining both statements into one

   <source lang="java">

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
 }

}</source>



Animal


Declaring and defining multiple variables in a single statement

A comma separates each variable.



   <source lang="java">

public class MainClass{

 public static void main(String[] arg){
  long a = 999999999L, b = 100000000L;
  int c = 0, d = 0;     
  
  System.out.println(a);
  System.out.println(b);
  System.out.println(c);
  System.out.println(d);
 }

}</source>



999999999
100000000
0
0


Expressions

Some expressions can be made statements by terminating them with a semicolon. For example, x++ is an expression. However, this is a statement:



   <source lang="java">

x++;</source>



Statements can be grouped in a block. A block is a sequence of the following programming elements within braces:

  1. statements
  2. local class declarations
  3. local variable declaration statements


How to write multiple assignments in a single statement

   <source lang="java">

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);
 }

}</source>



777
777
777


Label a statement block

  1. A statement and a statement block can be labeled.
  2. Label names follow the same rule as Java identifiers and are terminated with a colon.



   <source lang="java">

public class MainClass {

 public static void main(String[] args) {
   int x = 0, y = 0;
   sectionA: x = y + 1;
 }

}</source>



Label statement can be referenced by the break and continue statements.


Spreading a single declaration over several lines

   <source lang="java">

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);
 }

}</source>





Statement Blocks

  1. You can have a block of statements enclosed between braces.
  2. If the value of expression is true, all the statements enclosed in the block will be executed.
  3. Without the braces, the code no longer has a statement block.



   <source lang="java">

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");
   }
 }

}</source>



in the block
in the block