7.3. Statements

Statements in Java can be classified as follows:

Declaration statements: are statements with variable declarations with explicit initialization of the variables. We have already learnt about variable declaration and initialization, now you know that when both are done together, it is a declaration statement.

Let’s look at an example class consisting of declaration statements.

			package javatutorial;

			/**
			 * Example class to identify the declaration statements
			 *     
			 *  @author f5java.com   
			 */
			public class DeclarationStatements {

				public static void main(String[] args) {
					// 1. Declaration statement in variable declaration + initialization
					
					// Following are declaration statements
					int age = 31;
					float salary = 10000.00f;
					String name = "Bob";
					Car car = new Car();
					
					System.out.println("Printing the values from declaration statements");
					System.out.println("Age: " + age);
					System.out.println("Salary: " + salary);
					System.out.println("name: " + name);
					car.moveforward(); // calling method on car, prints moving forward message

				}
			}
		

Control flow statements: Are the statements which determine the flow of the control during the execution of the program. This usually involves altering the program flow based on some condition, looping or transferring the control.

This is one of the very important aspects Java and as a programmer you might be using this on a daily basis. If you do not fully understand the above paragraph, don’t worry, we have a separate chapter dedicated to learn control flow statements. However we’ll write a small program, which uses one of the simple control flow statements like if else statement.

			package javatutorial;

			/**
			 * Example class to examine one of the simple control flow statements (if else) in Java
			 *     
			 *  @author f5java.com   
			 */
			public class ControlFlowStatements {
				
				public static void main(String args[]){
					
					// 1. Instantiate and set values on voter object
					Voter bill = new Voter(); 
					bill.setAge(30);
					bill.setName("Bill");
					
					Voter mark = new Voter();
					mark.setAge(16);
					mark.setName("Mark");
					
					// 2. Call isEligibleToVote method with values in voter object
					
					isEligibleToVote(bill.getName(), bill.getAge());
					isEligibleToVote(mark.getName(), mark.getAge());
				
				}
				
				/**
				 * Determines whether the voter is eligible to vote or not
				 * based on voter's age 
				 */
				private static void isEligibleToVote(String name,int age){
					// Decision making logic: if else statement
					// If condition is true: if block gets executed
					// If condition is false: else block gets executed
					
					if(age > 18){ 
						System.out.println("Eligible to vote: " + name );
					}else{
						System.out.println("Not eligible to vote: " + name);
					}
				}

				
			}

			/**
			 * A Voter class : holds voter's name and age;
			 */
			class Voter{
				String name;
				int age;
				// Getters and Setter methods to set and get values
				// We'll take a closer look at this in coming chapters
				public String getName() {
					return name;
				}
				public void setName(String name) {
					this.name = name;
				}
				public int getAge() {
					return age;
				}
				public void setAge(int age) {
					this.age = age;
				}	
			}
		

Expression statements:

Expression: An expression statement is an expression terminated by a semicolon. The expression is evaluated for its side effect and its value discarded.

Expression statement can be one of the following:

Let’s look at an example class consisting of various kinds of expression statements.

			package javatutorial;

			/**
			 * Example class to identify the following types of expression statements
			 * 	 •	Assignments: Assigning the value of an expression to a variable.
			 *	 •	Increment and Decrement operators: Incrementing or decrementing the variable value
			 * 	 •	Method calls: calling a method
			 * 	 •	Object creation expression: creating an object with new operator
			 *     
			 *  @author f5java.com   
			 */
			public class ExperssionStatements {

				public static void main(String[] args) {
					
					int bookId;
					String bookName;
					
					// 1. Below assignments are expression statement
					bookId = 1;
					bookName = "f5java Java Tutorial";
					System.out.println("Printing assigment values");
					System.out.println("Book Id: " + bookId + " Book Name: " + bookName);
					
					// 2. Increment operation is assignment statement
					bookId = bookId + 1;
					System.out.println("Incremented book id to " + bookId);
					
					// Another shortcut way to increment
					bookId++;
					System.out.println("Incremented book id by shortcut way " + bookId);
					
					// 3. Decrement operation is assignment statement
					bookId = bookId - 1;
					System.out.println("Decremented book id to " + bookId);
					
					// Another shortcut way to increment
					bookId--;
					System.out.println("Decremented book id by shortcut way " + bookId);
					
					Student student;
					
					// 4. Object creation using new operator is an expression statement
					student = new Student("Bill", "Java Programming Course", "A");
					
					// 5. Method call is a declaration statement.
					student.displayStudentDetails();
					
				}
			}