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:
System.out.println("hello");
System.out.println("world");
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”);
a = b;
{ 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:
Now that you know more about statements, let’s modify your initial code to include more statements:
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"); }