Java Keywords and Statements

We’ll cover the following

  • Java – keywords
  • case-sensitive
  • Java Statements
  • Types of statements
    • Declaration statements
    • Assignment statement
    • Expression statement
    • Control statement
    • White space
  • Java Statement Blocks

Java Keywords

A keyword is a word that has a special meaning defined by the Java programming language.

Java has 53 keywords.

They’re listed in alphabetical order in the following table.

abstract default goto       package      this 

assert   do      if         private      throw 

boolean  double  implements protected    throws 

break    else    import     public       transient 

byte     enum    instanceof return       true 

case     extends int        short        try 

catch    false   interface  static       var 

char     final   long       strictfp     void 

class    finally native     super        volatile 

const    float   new        switch       while 

continue for     null       synchronized _ (underscore)

Instead, true, false, and null are literals used to represent predefined values.

var is a special type of identifier.

Two keywords, const and goto, are reserved by Java but don’t do anything.

The underscore _ character is reserved as a keyword with no purpose. The underscore character is reserved just in case it may be used for some purpose in a future release of Java.

Case

Like everything else in Java, keywords are case-sensitive.

Thus, if you type If instead of if or For instead of for, the compiler complains about your error.

Java Statements

Java uses statements to build programs. Every Java class must have a body, and the body of a class is made up of one or more statements.

Types of statements

Java has many types of statements like Declaration statements, Assignment statements, Expression statement, Control statement etc., we will learn one by one about each of them.

Declaration statements

Some statements simply create variables that you can use to store data. These types of statements are often called declaration statements:

For example:

int i; 

String name;

Assignment statement

Another common type of statement is an assignment statement, which assigns a value to a variable:

For example:

i = 42; 

name = "codingtimes.in";

Declaration and assignment statements can be combined into a single statement, like this:

int i = 42; 

String name = "codingtimes.in";

Expression statement

Another common type of statement is an expression statement, which performs calculations or other operations.

For example:

System.out.println("Hello, World!");

Control statement

Java if statements executes other statements only if a particular condition has been met.

Statements such as for, while, and do execute whole groups of statements one or more times.

Here’s a typical if statement:

if (total > 1000) 
   discountPercent = 10;

Here, the variable named discountPercent is given a value of 10 if the value of the total variable is greater than 1000.

The assignment statement ends with a semicolon, but the if statement itself doesn’t.

White space

In Java, the term white space refers to one or more consecutive space characters, tab characters, or line breaks. All white space is considered the same.

A single space is treated the same as a tab or line break or any combination of spaces, tabs, and line breaks.

In Java, you don’t have to do anything special to continue a statement onto a second line. Thus the statement

x = (y + 15) / z;

is identical to this statement:

x = 

(y + 15) / z;

You can’t put white space in the middle of a keyword or identifier.

The following example won’t work:

p u b l i c static vo id main(String[] args)

Here the extra spaces between the letters in the words public and void will confuse the compiler.

Good practice

Place each statement on a separate line. In addition, you can break a longer statement into several lines for clarity.

Use tabs or spaces to line up elements that belong together.

The compiler ignores the extra white space, so it doesn’t affect the byte code that’s created for your program.

Using extra white space in your program doesn’t affect your program’s performance in any way, but it does make the program’s source code easier to read.

Java Statement Blocks

A block is a group of one or more statements that’s enclosed in braces.

A block begins with an opening brace { and ends with a closing brace }.

Between the opening and closing braces, you can code one or more statements.

Here’s a block that consists of three statements:

{ 

          int i, j; 

          i = 10; 

          j = 20; 

}

A block is itself a type of statement.

Any time the Java language requires a statement, you can substitute a block to execute more than one statement.

The basic syntax of an if statement is this:

if ( expression ) 
   statement

Here statement can be a single statement or a block.

You can code the braces that mark a block in two popular ways.

One is to place both braces on separate lines and then indent the statements that make up the block. For example:

if ( i > 0) 
{ 
    String st = "i is " + i; 
    System.out.print(st); 

}

The other style is to place the opening brace for the block on the same line as the statement the block is associated with, like this:

if ( i > 0) { 
    String st = "i is " + i; 
    System.out.print(st); 
}

You should not end a block with a semicolon.

The statements within the block may require semicolons, but the block itself does not.

Example

The following program uses a block of code as the target of a for loop.

package JAVA.Basic;

// Demonstrate a block of code.
//   Call this file Main.java

public class Main {
    public static void main(String args[]) {
      int x, y;
  
      y = 20;
  
      // the target of this loop is a block
      for(x = 0; x<10; x++) {
        System.out.println("This is x: " + x);
        System.out.println("This is y: " + y);
        y = y - 4;
     }
    }
  }
  

The output generated by this program is shown here:

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Hello World Program


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


One comment

Leave a Reply

Your email address will not be published.