Copyright

Java Syntax: Expressions, Statements & Blocks

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Like any language, Java lets you create variables, run tests on them and bundle them in nice packages. In programming terms, these concepts are defined as expressions, statements and blocks. This lesson will define each and provide Java code examples for each.

An Expression of Code

A key component of programming is the means of calculating and testing values. In Java, an expression is the line of code that either holds or calculates these values. An expression can be thought of as the setup to the rest of the code. That is, we'll setup a variable to count transactions: This line of code has only one purpose, and that is to declare the variable.

You can give a value to a variable when you declare it. However, for this lesson, we need to understand the concept of expression versus statement. It is the setup to the rest of the program. The following code example is a great example of a simple Java expression:


Java Basic Expression


In this code, all we have done is declare the variable. Now, if we actually want to DO something with that variable, we can. These actions are called statements.

A Statement Made

Think of a statement as a command to Java - an order to perform a task. The expression above only defined the variable; when we convert that expression into a statement, we will actually do something with it.

In the expression, we simply declared the variable for our user counter. The next line of code will take action on the variable. In the following example, we'll add a value (in this case another variable) to the user count:


Java basic statement


An expression sets up the path while the statements perform actions on the variables.

Blocks

Expressions and statements often need to be placed together. Let's say you have a module or function that carries out a task, such as updating a pay rate. Blocks are declared by using an open curly bracket ({) and a closing curly bracket (}).

The following example shows a basic block structure. There are no methods or functions. It is a perfectly legal code declaration on Java to organize statements and expressions this way.

Notice how there are actually two blocks here. This is called nesting. You can have blocks within blocks. Just be sure to indent each block so that the code is easy to follow!


Java Block Basic


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support