Select Page

Welcome to “Java Diaries” a series in which I document my self-teaching of Java as both a record of what I’ve done and a reference for me to check back on later – hopefully aiding the information I’ve been consuming to sink in. Click here to view all posts in this series.

After learning about operators it was on to briefly looking at what keywords, expressions and code blocks are within Java.

Keywords

In the Java programming language, a keyword is one of 50 reserved words that have a predefined meaning in the language; because of this, programmers cannot use keywords as names for variables, methods, classes, or as any other identifier. – Wikipedia

I have been using keywords from the start without realising and the great thing about IntelliJ and its code highlighting features is that I now know any word that shows up in blue is a keyword. Some of the keywords I have been using already are:

  • Double
  • Float
  • Int
  • Long
  • Boolean
  • If
  • Else

You get the drill! If you’d like to check out the full list of Java keywords you can head over to the official documentation.

As it states in the Wikipedia quote at the top of the page, you can’t name keywords using another keyword, for example, you couldn’t call a float “float” though you could call it “float2” and this would be valid.

Expressions

Expressions are built using values, variables, and operators etc. Hopefully the following diagram will help make sense of this:

Note that the semicolon is not part of the term expression. Another way of defining expressions can be as follows, courtesy of ThoughtCo.com

Expressions are essential building blocks of any Java program, usually created to produce a new value, although sometimes an expression simply assigns a value to a variable. Expressions are built using values, variables, operators and method calls.

When it comes to if statements it is slightly different, from what I understand. For example in the following code:

if (topScore == 100) {}

if would be the keyword and topScore == 100 would be the expression – the brackets are not included under the definition of expression in this case.

Challenge

The challenge that Tim set for the next section was to look at the following code and decide what parts of the code would be an expression.

int score = 100;
if (score > 99) {
   System.out.println("You got the high score!");
   score = 0;
}

In the above code the following underlined pieces are the expression.

int score = 100;
if (score > 99) {
System.out.println(“You got the high score!”);
score = 0;
}

NOTE: When defining a variable the data type and ; aren’t part of the expression.

Statements

At first glance a statement is just one line of code, and this is an okay way of thinking about it but statements can actually span multiple lines.

double myDouble = 120d;
System.out.println("This is myDouble: " + myDouble)

The above code is two statements, one per line.

However:

double myDouble = 120d;
System.out.println("This is myDouble: " + 
                    myDouble);

The above is still two statements, this time spanning three lines. Tim noted it could be worth considering doing this sometimes as it can help make the code more readable in certain circumstances, for example, breaking up text and variables.

double myDouble = 120d; System.out.println("This is myDouble: " + myDouble); int myInt = 1; System.out.println("This is myInt: " + myInt);

And you can actually have more than one statement on one line – this is not recommended though as it is really hard to read. For example, in the above code I wrote four statements on the same line.

Statements are usually signified as ending when you see a ;

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. [Certain] types of expressions can be made into a statement by terminating the expression with a semicolon (;). – Java Documentation

Code Blocks

Code blocks are signified by curly brackets { }

A lot of code editors, such as IntelliJ will note where the opening and closing curly bracket is and you will be able to collapse parts of the code because of this.

IntelliJ feature example

IntelliJ doing IntellJant things.

In the example above you can see that everything between the curly brackets that start on line 5 can be collapsed.

When using curly brackets on if statements you don’t actually need them for the code following the statement to run if there is only one statement after the test. Both of the following pieces of code would run correctly:

if (score == 5000;)
    System.out.println("You won!")

if (score == 100;) {
    System.out.println("You lost!")
}

Using curly brackets are recommended even when not needed like in the above example for readability and getting into the practice.

Code blocks also have another interesting quirk. Variables can be accessed from within a code block, but outside variables can’t go inside code blocks. This is because Java deletes what is inside code blocks after it has run them.

When talking about variables and how they work with different code blocks, programmers call this “Scope”.

The scope of a variable defines the section of the code in which the variable is visible. As a general rule, variables that are defined within a block are not accessible outside that block. The lifetime of a variable refers to how long the variable exists before it is destroyed. Destroying variables refers to deallocating the memory that was allotted to the variables when declaring it. – Java With Us.

Here’s an example of the above in practice:

int playerScore = 45;

if (playerScore < 50;) {
    System.out.println("You lost, try again!")
    boolean isWinner false;
    playerScore = 0;
}

In this piece of code you can see we have defined an int called playerScore and set its value to 45.  Then, in the if statement we tested whether playerScore was LessThan 50 which it wasn’t. “You lost, try again” is then printed to the console, a boolean named isWinner is declared and set to false, then we reset playerScore to 0.

Because we defined the boolean inside the code block we can not access this outside, so after that final curly bracket on line 7, isWinner will be deleted. However, because on line 1 we have an int named playerScore which is outside the code block, this can be used within the code block.

If you’re confused by this I don’t blame you because it is confusing at first, but I think I am starting to understand the concept and the benefits of it being this way. For example, if you use a variable inside a code block which is then deleted you can re-use the variable name in later pieces of code as it technically no longer exists. For further reading on scope you should definitely check out this Java With Us article.

That’s it for this post, whilst I didn’t learn a lot of new coding techniques, learning how to correctly construct code was a valuable skill to learn. The next one will be about Methods in Java which are pretty interesting so keep your eyes peeled for that one.