Java Statement and Expression

Java Statement and Expression

  • Java statements appears inside the methods of the classes and they describe all activities of a Java program.
  • Expressions produce values as results and will be used as part of another expression or in a statement.
    Statement and Expression program example:
    public class General {
     public static void main(String[] args) {
      int a=0, b=0, x=0, y=35;
      System.out.print("a: " +a +" b: " +b);
      System.out.print(" x: " +x +" y: " +y +"\n");
      a = 9;
      b = 7;
      y = x = a+b;
      System.out.print("a: " +a +" b: " +b);
      System.out.print(" x: " +x +" y: " +y +"\n");
     }
    }
    The result will bee:
    a: 0 b: 0 x: 0 y: 35
    a: 9 b: 7 x: 16 y: 16
    
  • Although every statement in the block ({...}) must end with a semicolon, the block itself does not end with a semicolon.
  • Method calls, object allocations, and of course, mathematical expressions are examples of expressions.
    Expressions example:
    ...
    int size = 5;
    
     if ( size > 10 )
        doSomething(  );
    
     for( int x = 0; x < size; x++ ) { ... }
    ...
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.