×

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

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 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.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Java Speech Recognition

The customer experience and convenience are improved with its utilization. Command and control interest in buying and voice synthesizers are supported by the cross-platform API defined by the API. For...

4 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.