Java Beginners - Java For Beginners Java Statements Rating: 5.0/5 (2 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java Expressions, Statements, and BlocksJava fundamentally is an object-oriented language; all Java code centers around defining the attributes and behaviors of objects. Within methods and static initializers, program logic is constructed using time-honored structured programming techniques. These techniques employ expressions, statements, and blocks to implement program logic and control execution flow.
Using Java Expressions and Statements Expressions
The work of Java programs is performed by expressions Expressions can have a single operation or they can have multiple operations combined to form a compound expression. In all cases, only one value is returned. The three purposes of expressions are: - to compute values,
- to assign values to variables,
- to control the flow of execution.
Examples of expressions: // multiply a and b, then add the // result to c, then assign that value to x x = a * b + c;
//add b and c, then multiply the //result by a, then assign that value to x x = a * (b + c);
//three expressions, two compute //a value and the third assigns it to total total = (quantity * price) + tax.compute();
The evaluation order of compound expressions is determined by both the order of precedence of the operators and the existence of any parentheses. For instance, the ++ operator is evaluated before the * operator, by default. Often, the evaluation order of compound expressions becomes unclear or is different than what the programmer requires. In these cases, you should use parentheses '()' to either clarify or change the order of evaluation. Examples of compound expressions: - Object creation,
- Variable declaration,
- Method call,
- Assignment (= op= ++ -- ).
Examples of expression statements: String city = new String("London"); int age = 19; age++; myObject.incrementCount(); city = city.trim();
Blocks A block is a sequence of statements wrapped in curly braces '{}'. Blocks usually accompany control flow statements (such as if or while) to delineate the set of statements that are controlled by the accompanying control flow statement. Occasionally a programmer will have the need to create a block independently of any control flow statements. If this is the case, the set of statements wrapped in curly braces is executed as if the curly braces didn't exist, with one exception: The variables declared in that block are local to the block. This behavior holds for all variables declared within blocks. Any variables declared within the block are local to that block. Local variables expire upon exiting the block in which they are declared. Also, variables declared within a block shadow (or hide) instance variables with the same name. package mysource;
public class BlockTest {
static int myClassInt = 100;
public static void main(String args[]) {
System.out.println( "myClassInt before block: " + myClassInt);
int localInt = 1;
System.out.println( "localInt before block: " + localInt);
//starting a block { //this will shadow, or hide, the instance //version of myClassInt int myClassInt = 1; System.out.println( "myClassInt inside block: " + myClassInt);
//BROKEN: This cannot compile because localInt is //already declared int localInt = 2; System.out.println( "localInt inside block: " + localInt);
int blockInt = 1; System.out.println( "blockInt inside block: " + blockInt); }
//this now references the instance version //of myClassInt again System.out.println( "myClassInt after block: " + myClassInt);
// BROKEN: This cannot compile because // blockInt no longer exists //System.out.println( // "blockInt inside block: " + blockInt); } }
Here is an example run of BlockTest: $ javac mysource/BlockTest.java $ java mysource.BlockTest myClassInt before block: 100 localInt before block: 1 myClassInt inside block: 1 localInt inside block: 1 blockInt inside block: 1 1 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|