×

Statements in java

What is Statement in java:

A statement in java is an instruction that explains what will happen based on the condition.

Types of java statements:

There are different statements in java

  • Expression statement
  • Declaration statement
  • Control flow statement

Expression statement:

An expression is used to generate a new value and we can also assign a new value to a variable. it is the combination of values, variables, operators, and methods calls.

There are three types of expressions in Java:

  • The expression is used to produce a value. we should enclose the expression in the parentheses that will be evaluated first, after that rest of the expression which is present will be evaluated.
  • Expressions are used to assign a new value to a variable.
  • Expression is used to neither produces any result nor assign a value like an increment or decrement 
  • expressions that produce a value use a high range of Java arithmetic, comparison, or conditional operators.
  • arithmetic operators like +, *, /, <, >, ++ and %. Some conditional operators are like ||, and the comparison operators are like <, <= and >.

Example1:

// expression

number = 10

// statement

number = 10;

Example 2:

// expression

++number

// statement

++number;

Declaration statement:

Declare statements are used to declare the variable by specifying its data types and name.

Syntax:

Data_type variable_name = value; 

Example:

public class CreateVariable {
    public static void main(String[] args)   
{  
//variable declaration  
        int student_id = 10;  
        String student_name = "Java coder";  
        double numbers = 3.21;  
        Boolean shows = true;  
        System.out.println("Name:"+student_name+ "\nAge:" +student_id);  
        System.out.println("Number:"+numbers+ "\nBoolean:" +shows);  
    }  
}

Output:

Name: java coder
Age:10
Number:3.21
Boolean: true

Control statements:

Control statements are used to decide the flow of a java program. The control statements are used to interrupt a flow of control of a particular section of a program based on the condition.

Control statements are divided into different types

  1. Conditional or Selection Statements
    • if Statement
    • if-else statement
    • if-else-if statement
    • switch statement
  2. Loop or Iterative Statements
    • while Loop
    • do-while Loop
    • for loop
    • for each Loop
  3. Flow Control or Jump Statements
    • Break
    • Continue
    • return

Example:

//declaring the statement  
int number;  
//expression statement  or initialising the value
number = 250; 
//control flow statement  
if (number > 50)  
{  
//expression statement  
System.out.println(number + " is greater than 100");  
}

Output:

250 is greater than 100

Related Topics

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.