Syntax and Rules in Java

Keywords: Syntax, Rules, Comments

Hello World Java

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!

What is Syntax in Java?

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 international, so everyone follows the same standards.

Syntax in Java

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:

Syntax in Java – Upper and Lower Case

Java is case-sensitive, which means, for example, Number and number have completely different meanings.

Syntax in Java – Name of Methods start with a lowercase letter

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.

Syntax in Java –  Name of a Class starts with a capital letter

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

Rules and Tips in Java

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:

Rule in Java – All commands / operations must end with a semicolon  ;

All operations in Java end with a semicolon. For example, the command in the previous section that printed “Hello World” ended with a semicolon.

System.out.println("Hello, World");

If you forget to include the semicolon the program would crash.

Rule in Java – Special characters do not work in Java

Use English words when writing code. However, in print outs, for example, with System.out.println command, special characters works fine.

Advise: Comment your code!

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  

Summary: Syntax and Rules in Java

When programming, keep in mind,

  • The syntax is what you should follow to have the same style as others who program in Java
  • Rules are what you have to follow to avoid compiling errors.
  • The program can be run even if you break a syntax, but you cannot run a program that breaks a rule.