Last Updated:

Java Syntax - Tutorial

Java Syntax

A Java program is a collection of objects that interact by calling each other's methods. It is important to understand the syntax of the Java language, namely what an instance class, object, methods, and variables are.

  • Object—Objects have behavior and state. For example, a dog may have a condition – name, color, as well as behavior – to run, jump, eat. The object is an instance of the class.
  • Class—A template that describes the behavior of an object.
  • The method is basically behavior. A class can include multiple methods. Logically recorded data in the methods manipulate and carry out all actions.
  • Instance variables—Each object has its own set of instance variables that are unique. The state of the object is formed by the values assigned to these instance variables.

First acquaintance with the syntax of the Java language on the example of a program

Here is a simple code, as a result of which the words "The World Will Accept!" will be displayed on the screen.


publicstaticvoidmain(String []args){
/* This is my first java program.

As a result of the execution, the screen will be displayed 'Hello World!'

System.out.println ("Hello World!"); Displaying a message on the screen

Now let's look at the steps of how to save a file in order to compile and run the program.


  • To get started, open Notepad, where you'll add the code above.
  • Save the file with the following name: "". Then you will understand why it was necessary.
  • Open a Command Prompt window, and then navigate to the directory where you saved the file. For example, this is the directory "C:\".
  • Now type "Javac" and then press Enter to compile the code. If you didn't make a mistake in the code, the command line will take you to: (Assumption: The path variable is set).
  • To run the program you see "java MyFirstJavaProgram".
  • Then you will see the inscription "Hello World!", which is printed in the window.

C:> javac
C:> java MyFirstJavaProgram
Hello world!

Java Syntax Basics

It is important to know such points in the syntax:

  • Case sensitivity – Java is case-sensitive, so hello and hello are considered different.
  • Class Name–The first letter for all classes must be uppercase.
  • If several words are used to form the name of the class, the first letter of all words should be in uppercase, for example, "MyJavaClass".
  • Method names—All method names in Java syntax must begin with a lowercase letter.
  • If multiple words are used to form the method name, the first letter of all internal words must be uppercase, for example, "public void myMethodName()".
  • It is important that the name of the program file exactly matches the name of the class.
  • Save the file using the class name (don't forget about case sensitivity) and add ".java" at the end of the name. If the names do not match, the program will not compile.
  • public static void main(String args[]) — program processing begins with the obligatory main() method.

Identifiers in Java

Identifiers are names that are used for classes, methods, and variables. Names are present in all Java components. Rules in Java syntax that you need to know about the identifier:

  • Each identifier should always start with "a" to "z" or "A" to "Z", "$" or "_".
  • After the first character, the identifier can have any combination of characters.
  • You can't use a keyword as an identifier.
  • An identifier in Java is case sensitive.
  • An example of an incorrect spelling of the identifier: 123abc, -salary.
  • An example is correct: age, $salary, _value, __1_value.


They began to be used in Java 5.0. Their task is to limit a variable in order to select only one value from several predefined ones. In this list, the values are called enumerations.

By using enumerations, you can reduce the number of errors in your code. For example, if we consider applications for fresh juice, it makes sense to limit the size of its packaging for large, medium and small. Thus, with the help of enumeration in Java, it is possible to make sure that none of the buyers can order another size of juice, except for a larger, medium or small.

Example enumeration:


enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
FreshJuiceSize size;



publicstaticvoidmain(String args[]){
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM;
System.out.println ("Size: » + juice.size);


The result is an example above:


Importantly! Enumerations in Java can be declared either independently or within a class. Variables, methods, constructors can also be defined within an enumeration.

What are the types of variables

Later in Java, we will see the following types of variables:

  • local;
  • Static (class variables)
  • Dynamic (instance variables).


In Java, as in other languages, you can modify methods, classes, and so on using modifiers. Modifiers are divided into 2 categories:

  • With access: public, protected, private, default.
  • Without access: abstract, strictfp, final.



In Java, an array is an object that stores several variables of the same type. But whatever it was, the array itself acts as an object.

Comments in Java


Java supports both single-line and multi-line comments. It is similar to those used in C and C++. Characters are not available inside comments and are ignored by the compiler.

Here's an example of a comment:


/* This is my first program.
* As a result of the execution, the screen will display 'Hello World'
* This is a multi-line comment, an example of writing.


publicstaticvoidmain(String []args){
// One-line comment.
/* Also one-line comment. */
System.out.println (Hello World);


Keywords in Java


The Java language uses 50 keywords. Here is a list of them in the form of a table:

List of keywords
of thedoubleelseenum

Attention! A keyword cannot be used as a variable or constant, or as other identifier names.

Empty string

Strings in Java that consist only of spaces are possible with a comment. The Java language ignores the line with comments and spaces.

Inheritance in Java

Inheritance is a concept that allows you to reuse the methods and fields of an existing class without rewriting the code again. In this case, the existing class is called a superclass, and the derived class is called a subclass.

Interface in Java

An interface can be defined as a contract between objects on how to communicate with each other. When it comes to the concept of inheritance, it plays a vital role. The interface defines the methods of the resulting subclass (class) and how they should be used. But the implementation of methods directly depends on the subclass.