Java Syntax Rules (original) (raw)

In this tutorial for beginners, we will explain the basics of Java and its Syntax Rules.

Firstly, we will talk about the simple program syntax and we will analyze a simple program. After that, we will explicate the modifiers, enums, and comments. Last but not least we will talk about what keywords are allowed to use as classes, variables, or methods.

You can learn about the basics of Java syntax in the following Java Tutorial for Beginners video:

Java Tutorial for Beginners: How to download and use Java

1. Java Syntax – Introduction

A Java program is a simple way to communicate with a computer in order for the computer to do a task for us. But how we create a program and what are the restrictions of the code that we must follow to have the right result without syntax errors?

Java Syntax - java tutorial for beginners

2. Technologies Used

The example code in this article was built and run using:

3. Simple program

At first, we must know the basics of the structure of a program. The four most important elements of a Java program are:

Here we can see an example:

Hello_world.java

public class Hello_world { public static void main(String []args) { System.out.println("Hello World"); } }

You can also take a look at this Java Hello World Example for more practise.

4. Case Sensitivity

Java is a case-sensitive language. This means that if you declare two variables with the same name but the one has a capital character then the code understands that are two different variables. For example:

int value=1; int Value=1;//These variables are different

5. Java Conventions

Here are some naming conventions that we need to follow at java programming for good maintenance and readability of the code. In Java, we use the CamelCase to write the name of methods variables, classes, constants.

class Dog implements Animal

void pauseTime(int time);

int number=8;

public static final float MAX_NUMBER= 100;

6. Brackets

In Java, we use brackets for many reasons. The first reason is to collect statements in blocks with curly brackets (‘{}’). We can see these brackets when we open or close classes, interfaces, methods, if and loop commands. The second reason is to index into an array with square brackets(‘[]’).For example:

Square brackets:

String[] animals= {"dog", "cat", "horse", "lion"};

Curly brackets:

public static int Number(int j) { int i = 0; i=i*j; return i; }

7. Semicolons

The semicolon(‘;’) is a character that we see almost at every line of code. The use of the semicolon is that it indicates an end of the statement. Some statements have a special characteristic which is that they are self-close. This commands are if(){}, for(){}, while(){} and functions(){}.

8. Arrays syntax in Java

An array is a reference type that can store a collection of values of a specific type. The declaration of an array is:

type[] identifier = new type[length];

For example:

int[] numbers=new int[30];

In Java, the array indexes start at zero. To insert an item in array we use the variable name and its index:

numbers[0] = 1; numbers[1] = 2;

9. Modifiers

Modifier: Is the access type of methods, classes, etc.. There are two categories of modifiers:

Access Modifiers:

Non-access Modifiers:

10. Java Enums

Enums in java are used to restrict a variable to have specific values.With enums its easier to reduce the number of bugs in your code. For example:

Enums.java

public class Enums { enum difficulty { easy, normal, hard } public static void main(String[] args) { difficulty dif = difficulty.hard; System.out.println(dif); } }

The output is:

hard

Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing an alternative code. there are 2 different ways to make a comment:

Here is an example:

/*this

//this is one-line comment

You can take a look at this article related to Java Comments.

12. Keywords

There are some words that are reserved in Java and we cannot use them for variable names or identifiers names. These words are:

13. Download the Source Code

That was a tutorial for beginners. We explained the basics of Java and its Syntax Rules.

Download
You can download the full source code of this example here: Java Syntax Rules

Photo of Ioannis Makrygiannakis

John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.