Starting to Code in Java (Part 2)

Introduction to Java syntax

Now that you have created, compile and executed your first line of code, let’s look at Java syntax.

When you are using Java, you are communicating with the system asking it to perform actions. To communicate with the system, you are using a language or a set of words bound by a syntax that the computer and you know and understand. This language consists of keywords, key phrases, and a syntax that ensures that the instructions are written and (more importantly) understood properly.

In computer science, this language needs to be exact, precise, unambiguous, and with a correct syntax. In other words, it needs to be exact.

When writing Java code, you will be following a syntax; this syntax will consist of a set of rules that will make it possible for you to communicate your instructions unambiguously. In addition to its syntax, Java also uses classes, and your Java applications will, by default, be saved as classes.

In the next section, we will learn how to use this syntax. If you have already coded in JavaScript, some of the information provided in the rest of this chapter may look familiar, and this prior exposure to other programming languages will definitely help you.

When programming in Java, you will be using a specific syntax to communicate with the system; this syntax will be made of sentences that will be used to convey information on what you would like the computer to do; these sentences or statements will include a combination of keywords, variables, methods, or events; and the next section will explain how you can confidently build these sentences together and consequently program in Java.

Statements

When you code in Java, you need to tell the system to execute your instructions (e.g., print information) using statements. A statement is literally an order or something that you ask the system to do. For example, in the next line of code, the statement will tell the system to print a message in the Console window:

System.out.println(“Hello Word”);

When writing statements, you will need to follow several rules, such as:

 The order of the statements: each statement is executed in the same order as it appears in the application. For example, in the next example, the code will print hello, and then world; this is because the associated statements are in that specific sequence.
System.out.println("hello");
System.out.println("world");
 Statements are separated by semi-colons (i.e., semi-colon at the end of each statement).

Note that several statements can be added on the same line, as long as they are separated by a semi-colon.

For example, the next line of code has a correct syntax, as all of its statements are separated by a semi-colon.

System.out.println(“hello”); System.out.println(“world”);

 Multiple spaces are ignored for statements; however, it is good practice to add spaces around the operators +, , /, or % for clarity. For example, in the next code snippet, we say that a is equal to b. You may notice that spaces have been included both before and after the operator =.
a = b;
 Statements to be executed together (e.g., based on the same condition) can be grouped using code blocks. In Java, code blocks are symbolized by curly brackets (e.g., { or }). So, in other words, if you needed to group several statements, you would include all of them within the same set of curly brackets, as follows:
{ 
  System.out.println("hello stranger!"); 
  System.out.println("today, we will learn about programming"); 
}

 

 

As we have seen earlier, a statement usually employs or starts with a keyword (i.e., a word that the computer knows and understands). All these keywords have a specific purpose, and common ones are as follows:

 Printing a message in the console window: the keyword is System.out.println.
 Declaring a variable: the keyword, in this case, depends on the type of the variable that is declared (e.g., int for integers, String for text, or bool for Boolean variables), and we will see more about these in the next sections.
 Declaring a method: the keyword to be used depends on the type of the data returned by the method. For example, in Java, the name of a method is preceded by the keyword int when the method returns an integer; it is preceded by the keyword String when the method returns a String, or by the keyword void when the method does not return any information.
 Marking a block of instructions to be executed based on a condition: the keywords are if and else.
 Exiting a method: the keyword is return.

.

Now that you know more about statements, let’s modify your initial code to include more statements:

 Please open the file MyClass that we have used earlier.
 Add the following code to it (new code in bold):
System.out.println("Hello, this is my fist line of code");
{
    System.out.println("this is my first application");
    System.out.println("This statement and the previous one belong to the same block of instructions");
}