Now that we have our feet wet with a few short Java programs, it is time to start going deeper and understand the concepts in detail. In this lesson we will look at Code Blocks and Tokens which are the fundamental unit of all Java programs.
Code Blocks
You can group two or more statements together by enclosing them between opening and closing curly braces:
{
statement1;
statement2;
..
..
..
}
Such a group of statements is called a code block. Consider code block as a logical unit that can be used at any place where a single statement can. The best way to demonstrate code blocks are the if
and for
statements we just learnt in the last lesson.
public class CodeBlockWithIf
{
public void demoIfWithCodeBlock() {
int a, b;
a = 1;
b = 2;
if (a < b) { //This opening brace starts the code block
/*
* All statements within this set of braces
* form the code block
*/
System.out.println("a is less than b");
a = 10;
b = 20;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
} //This closing brace ends the code block
}
}
In this program, we have replaced the single statement after if
with a code block containing multiple statements. Let’s execute it.
Here, a
is less than b
so the condition is true. The target of the if
statement is a code block so all the statements within that code block gets executed. Had the condition being false, none of the statements in the code block would get executed. I will change the less than to greater than sign which will make the condition false. After that, when I run the program, nothing will get printed.
As expected, nothing gets printed on the console window. The block of code behaves as one logical unit as the target of the if
statement. Either all the statements in the code block gets executed or nothing is executed.
Similarly, code block can be the target of for
loop too. Let’s see an example of this as well.
public class CodeBlockWithFor
{
public void demoForWithCodeBlock() {
int i, j;
j = 10;
for (i = 0; i < 5; i = i + 1) { //This opening brace starts the code block
/*
* All statements within this set of braces
* form the code block
*/
System.out.println("Value of i is " + i);
System.out.println("Value of j is " + j);
j = j - 2;
} //This closing brace ends the code block
}
}
In this program, the target of for
loop is a code block containing 3 statements. Let’s run this program.
As you can see in the output, in each iteration of the for loop all the 3 statements inside the code block get executed.
Code blocks are used at many other places in the program apart from if
statement and for
loops and they have some additional properties as well which we will see later. The most important thing to remember about code blocks is that they are used to create logically inseparable units of code.
Whitespace
Before starting our discussion on tokens lets first understand how Java handles whitespace. Java is a free-form language. It doesn’t enforce any special indentation rules for its programs. Let me show you what I mean by this.
I have removed all the whitespace from this program and written it on a single line. It compiles and runs without any error. All the newlines and formatting are for improving the readability of the programs. A properly formatted program is much easier to read and understand as opposed to when all the code is on a single line. The compiler however doesn’t need all these whitespaces to properly compile the program. In fact, compiler ignores all the whitespace in your program. In Java, whitespace includes space, tab and newline.
Tokens
A token is the smallest element of a program that is meaningful to the compiler. The different types of tokens in Java are:
- Identifiers
- Literals
- Operators
- Separators
- Keywords
Identifiers
In general, programming languages make use of identifiers for identifying all the different things in the program. In Java, Identifiers are used to name things like classes, objects, variables, arrays, functions an so on. Java has some rules for forming identifiers:
- The first rule says that an identifier can be a sequence of alphabets, digits, underscore and dollar sign characters only. To give a few examples, all these identifiers -
studentName
,roll_no
,java4champs
,$1_2_$10
,_subject
are valid identifiers as they are a combination of alphabets, digits, underscore and dollar sign characters only. - The second rule says that identifiers cannot start with a digit. So,
2nd
and20_20
are invalid identifiers as they start with a digit. - As per the third rule, an identifier must not be a Keyword or a Boolean or null literal. We haven’t talked about Keywords and Literals yet. This rule will become clear after we discuss about them. For now, just keep in mind that Keywords, Boolean literal and null literal can’t be used as identifiers.
One very important thing to keep in mind is that Java is case-sensitive. I am repeating this point again as beginning programmers make many mistakes due to this. Lets understand this with an example.
Lets say you declare a variable in your program as studentMarks
. Later in your code, when you want to use this variable you write it as StudentMarks
. You will get an error for undefined variable StudentMarks
. Since Java is case sensitive, studentMarks
is different from StudentMarks
which is different from studentmarks
. For Java, these are 3 different identifiers. So, watch out for the case of your identifiers and don’t let this problem happen to you.
Literals
Any constant value which can be assigned to the variable is called as literal. Some examples of literals are:
500
which is an integer3.141
which is a floating-point number‘A’
which is a character“Hello”
which is a string
There are also 2 special types of literals – The Boolean literals represented by the words true
and false
and the null literal represented by the word null
.
This is just an overview, I will have a lot more to say about literals after we complete our discussion about Data Types.
Separators
Separators help define the structure of a program. Java has 9 different types of separators as shown in the table below:
Symbol | Name |
---|---|
() | Parentheses |
{} | Braces |
[] | Brackets |
; | Semicolon |
, | Comma |
. | Period |
:: | Colons |
... | Ellipsis |
@ | Ampersand |
() Parentheses
Parentheses are used at multiple places in Java. The parameters that a method can accept are enclosed within a set of parentheses in the method definition. While calling the method, the values for these parameters are also enclosed within parentheses. This use of parentheses will become clearer after our discussion of functions.
Parentheses are also used in control statements. We briefly looked at this during our discussion of if
and for
statements. We will revisit it again when we will discuss control statements.
Parentheses are used for defining precedence in expressions and surrounding cast types. These usages will become clear after operators and data types discussion.
{} Braces
Braces are most commonly used for defining code blocks which we saw earlier in this lesson. They are also used to contain values of automatically initialized arrays.
[] Brackets
Brackets are used in arrays to declare array types and dereference array values. We will see its use in detail when we will discuss arrays.
; Semicolon
Semicolon is the most commonly used separator in Java. It is used to terminate statements. As you have seen in the programs so far, Java compiler mandates that statements end with a semicolon.
, Comma
Comma is used to chain statements together inside a for loop. It also separates successive identifiers in variable declarations.
. Period
Period is used to separate package names from subpackages and classes. It is also used to select a field or method from an object.
I have mentioned the next 3 separators - :: colons, ... ellipsis and @ at-sign for completeness. ICSE Computer Applications syllabus doesn’t cover them so they are out of scope for this course.
Keywords
A keyword is a word that has a predefined meaning in the language. Due to this, keywords can’t be used as names for variables, methods, classes or any other identifier. Java language specification defines 61 keywords. Here is the complete list:
_ (underscore) as a special case
_ (underscore) is a special case here. It was added as a keyword in JDK 9. Just by itself, it is considered a keyword and can’t be used as a variable name. But Java allows its usage in variable names when it is combined with other characters. Lets look at a program to understand it.
public class KeywordDemo
{
public void demoUnderscore() {
int _ = 10; //As _ is a keyword, this line will not compile
int _num = 20; // _ can be used with other characters
int roll_no = 34;
System.out.println("Value of _ is " + _);
System.out.println("Value of _num is " + _num);
System.out.println("Value of roll_no is " + roll_no);
}
}
The lines int _ = 10;
and System.out.println("Value of _ is " + _);
give compilation error as shown below. The error message clearly tells us that '_' is a keyword added in release 9 so it can’t be used as an identifier.
When I comment out these lines, I am able to compile and run the program. The reason is that in _num
and roll_no
, I am using underscore in combination with other characters. This usage of underscore in variable names is allowed by Java.
const and goto
Keywords const and goto are reserved but not used. So, what could be the reason for reserving them? These keywords are used in C++. Reserving these words as keywords in Java helps the compiler to produce better error messages. If a programmer coming from C++ background uses these keywords incorrectly in Java, then the compiler can provide appropriate error messages to the programmer.
You don’t need to learn all the keywords by heart, but it is good to have an idea about which words are reserved as keywords because you can’t use them as variable names in your programs.