Keywords: Syntax, Rules, Comments
Syntax and rules in Java are how the programmer should write his code, much like grammar works in languages. The compiler doesn’t care about the syntax – but programmers need to adhere to the correct syntax. Otherwise, it will be challenging for other programmers to understand your code. If everyone adheres to the same syntax, then it becomes easier for everyone to understand what it is that is programmed!
As a programmer, it is essential to write with the right syntax to get structured and easily understandable code. Especially in large programming projects, there are often several different programmers working on the project. Then it is extra important that everyone writes in the same way. Otherwise, it is impossible to get an overview of what is happening in the code. Furthermore, each programming language has its syntax, and it differs slightly between different programming languages. The syntax in java is international, so everyone follows the same standards.
The syntax in Java is learned gradually as you progress and learn new programming features. Therefore, in each section, we show you how the syntax for that section. However, some examples to give you a hint of different syntaxes in Java:
Java is case-sensitive, which means, for example, Number and number have completely different meanings.
All methods should start with a lowercase letter, e.g. myAnimalMethod(), countAllValues(), or count(). In Java, you don’t use underlining in the words that you do in some other languages, for example, count_All_Values() is the incorrect syntax in Java. However, you will not get any error from the compiler if you use incorrect syntax, but it’s not the correct way. We will see more about methods in Java in Chapter 5.
All names of classes in Java begin with a capital letter, e.g. AnimalClass. We will see more about classes in Java in Chapter 7.
Finally, if you want more information: Wikipedia – Java Syntax
There are certain rules that the programmer must follow when programming. If you break any rule, you will get a so-called compilation error, in other words, the compiler does not understand how to translate the code, and then the computer does not know what to do. The program will then crash.
As with syntax, rules are something you learn gradually, but it is still important to start thinking about how to structure and write your programs. Some examples of rules in Java are:
All operations in Java end with a semicolon. For example, the command in the previous section that printed “Hello World” ended with a semicolon.
If you forget to include the semicolon the program would crash.
Use English words when writing code. However, in print outs, for example, with System.out.println command, special characters works fine.
To better understand your code, it is a good idea to comment on the code. This also helps if someone else should use your code, or if you later forgot what the code did for something. You can easily make a comment with two slashes //, which creates a comment in the code. These are ignored by the compiler. You can also write a longer comment with a slash + asterisk: / * comment * /
// Comment on a single row /* A comment on multiple rows can be effective when you need to write a longer comment */
When programming, keep in mind,