Skip to content

Add Java Expressions, Statements and Blocks #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/navs/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export const documentationNav = {
pages['variables-primitive-data-types'],
pages['operators'],
pages['basic-input-output'],
pages['expressions-statements-blocks'],
],
}
133 changes: 133 additions & 0 deletions src/pages/docs/expressions-statements-blocks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: Java Expressions, Statements and Blocks
description: In this tutorial, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks with the help of examples.
---

import { Heading } from '@/components/Heading'
import Link from 'next/link'
import { TipInfo } from '@/components/Tip'


In previous chapters, we have used expressions, statements, and blocks without much explaining about them. Now that you know about variables, operators, and literals, it will be easier to understand these concepts.

## Java Expressions

A Java expression consists of variables, operators, literals, and method calls. To know more about method calls, visit Java methods. For example,

```java
int score;
score = 90;
```
Here, `score = 90` is an expression that returns an `int`. Consider another example,

```java
Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;
```
Here, `a + b - 3.4` is an expression.

```java
if (number1 == number2)
System.out.println("Number 1 is larger than number 2");
```
Here, `number1 == number2` is an expression that returns a boolean value. Similarly, `"Number 1 is larger than number 2"` is a string expression.

## Java Statements

In Java, each statement is a complete unit of execution. For example,

```java
int score = 9*5;
```
Here, we have a statement. The complete execution of this statement involves multiplying integers `9` and `5` and then assigning the result to the variable `score`.

In the above statement, we have an expression `9 * 5`. In Java, expressions are part of statements.

### Expression statements

We can convert an expression into a statement by terminating the expression with a `;`. These are known as expression statements. For example,

```java
// expression
number = 10
// statement
number = 10;
```
In the above example, we have an expression `number = 10`. Here, by adding a semicolon (`;`), we have converted the expression into a statement (`number = 10;`).

Consider another example,

```java
// expression
++number
// statement
++number;
```

Similarly, `++number` is an expression whereas `++number;` is a statement.

### Declaration Statements

In Java, declaration statements are used for declaring variables. For example,

```java
Double tax = 9.5;
```
The statement above declares a variable tax which is initialized to 9.5.
<TipInfo>

**Note:** There are control flow statements that are used in decision making and looping in Java. You will learn about control flow statements in later chapters.

</TipInfo>

## Java Blocks

A block is a group of statements (zero or more) that is enclosed in curly braces { }. For example,

```java
class Main {
public static void main(String[] args) {

String band = "Beatles";

if (band == "Beatles") { // start of block
System.out.print("Hey ");
System.out.print("Jude!");
} // end of block
}
}
```
Output:

```text
Hey Jude!
```
In the above example, we have a block `if {....}`.

Here, inside the block we have two statements:

- `System.out.print("Hey ");`
- `System.out.print("Jude!");`

However, a block may not have any statements. Consider the following examples,

```java
class Main {
public static void main(String[] args) {

if (10 > 5) { // start of block

} // end of block
}
}
```
This is a valid Java program. Here, we have a block `if {...}`. However, there is no any statement inside this block.

```java
class AssignmentOperator {
public static void main(String[] args) { // start of block

} // end of block
}
```
Here, we have block `public static void main() {...}`. However, similar to the above example, this block does not have any statement.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy