Skip to content

Commit 9ef9957

Browse files
authored
Merge pull request #10 from javaistic/expressions-statements-blocks
Add Java Expressions, Statements and Blocks
2 parents bddf955 + cd829a5 commit 9ef9957

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export const documentationNav = {
1717
pages['variables-primitive-data-types'],
1818
pages['operators'],
1919
pages['basic-input-output'],
20+
pages['expressions-statements-blocks'],
2021
],
2122
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Java Expressions, Statements and Blocks
3+
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.
4+
---
5+
6+
import { Heading } from '@/components/Heading'
7+
import Link from 'next/link'
8+
import { TipInfo } from '@/components/Tip'
9+
10+
11+
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.
12+
13+
## Java Expressions
14+
15+
A Java expression consists of variables, operators, literals, and method calls. To know more about method calls, visit Java methods. For example,
16+
17+
```java
18+
int score;
19+
score = 90;
20+
```
21+
Here, `score = 90` is an expression that returns an `int`. Consider another example,
22+
23+
```java
24+
Double a = 2.2, b = 3.4, result;
25+
result = a + b - 3.4;
26+
```
27+
Here, `a + b - 3.4` is an expression.
28+
29+
```java
30+
if (number1 == number2)
31+
System.out.println("Number 1 is larger than number 2");
32+
```
33+
Here, `number1 == number2` is an expression that returns a boolean value. Similarly, `"Number 1 is larger than number 2"` is a string expression.
34+
35+
## Java Statements
36+
37+
In Java, each statement is a complete unit of execution. For example,
38+
39+
```java
40+
int score = 9*5;
41+
```
42+
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`.
43+
44+
In the above statement, we have an expression `9 * 5`. In Java, expressions are part of statements.
45+
46+
### Expression statements
47+
48+
We can convert an expression into a statement by terminating the expression with a `;`. These are known as expression statements. For example,
49+
50+
```java
51+
// expression
52+
number = 10
53+
// statement
54+
number = 10;
55+
```
56+
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;`).
57+
58+
Consider another example,
59+
60+
```java
61+
// expression
62+
++number
63+
// statement
64+
++number;
65+
```
66+
67+
Similarly, `++number` is an expression whereas `++number;` is a statement.
68+
69+
### Declaration Statements
70+
71+
In Java, declaration statements are used for declaring variables. For example,
72+
73+
```java
74+
Double tax = 9.5;
75+
```
76+
The statement above declares a variable tax which is initialized to 9.5.
77+
<TipInfo>
78+
79+
**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.
80+
81+
</TipInfo>
82+
83+
## Java Blocks
84+
85+
A block is a group of statements (zero or more) that is enclosed in curly braces { }. For example,
86+
87+
```java
88+
class Main {
89+
public static void main(String[] args) {
90+
91+
String band = "Beatles";
92+
93+
if (band == "Beatles") { // start of block
94+
System.out.print("Hey ");
95+
System.out.print("Jude!");
96+
} // end of block
97+
}
98+
}
99+
```
100+
Output:
101+
102+
```text
103+
Hey Jude!
104+
```
105+
In the above example, we have a block `if {....}`.
106+
107+
Here, inside the block we have two statements:
108+
109+
- `System.out.print("Hey ");`
110+
- `System.out.print("Jude!");`
111+
112+
However, a block may not have any statements. Consider the following examples,
113+
114+
```java
115+
class Main {
116+
public static void main(String[] args) {
117+
118+
if (10 > 5) { // start of block
119+
120+
} // end of block
121+
}
122+
}
123+
```
124+
This is a valid Java program. Here, we have a block `if {...}`. However, there is no any statement inside this block.
125+
126+
```java
127+
class AssignmentOperator {
128+
public static void main(String[] args) { // start of block
129+
130+
} // end of block
131+
}
132+
```
133+
Here, we have block `public static void main() {...}`. However, similar to the above example, this block does not have any statement.

0 commit comments

Comments
 (0)
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