Java – Basic Syntax

SyntaxWhen we consider a java program, it can be defined as a collection of objects that interact by calling each other’s methods. Now let us take a quick look at the Java syntax and what class, object, methods, and instance variables mean.

Object – objects have state and behavior. For example: a dog can have a state – color, name, as well as behavior – nod, run, bark, eat. An object is an instance of a class.

Class – can be defined as a template that describes the behavior of an object.

Method is basically a behavior. A class can contain multiple methods. It is in methods that logically recorded data is manipulated and performed.

Instance Variables – Each object has its own unique set of instance variables. The state of the object is generated by the values ​​assigned to these instance variables.

java syntax

The first program and familiarity with the syntax of the language

look at a simple code that will display the words “Hello world!” And Java syntax for one thing.

public class MyFirstJavaProgram {
    public static void main (String [] args) {
    / * This is my first java program.
    As a result, the screen will display 'Hello world!'
    * /
       System.out.println ("Hello world!"); // Display the message on the screen

Let’s look at how to save the file in order to compile and run the program. Please follow the instructions below:

  • Open notepad and add the code above.
  • Save the file as “”. Below we will look at the Java syntax and find out why under that name.
  • Open a command prompt window and change to the directory where the file was saved. Let’s assume it’s “C: \”.
  • Type “Javac” and hit enter to compile the code. If there is no error in the code, the command line will take you to the following line: (Assumption: The path variable is set).
  • Now enter “java MyFirstJavaProgram” to run the program.
  • You will now see “Hello World!” Printed in the window.
C:> javac
C:> java MyFirstJavaProgram
Hello World!

Java syntax basics

It is very important to know and remember the following points in the syntax:

  • Case Sensitivity – Java is case sensitive, meaning Hello and hello have different meanings.
  • Class names – for all the first letter must be in upper case.
  • If multiple words are used to form a class name, the first letter of each inner word must be in uppercase, for example, “MyJavaClass”.
  • Method Names – In Java syntax, all method names must begin with a lowercase letter.
  • If multiple words are used to form a method name, then the first letter of each inner word must be in uppercase, for example, “public void myMethodName ()”.
  • Program file name – the program file name must exactly match the class name.
  • When saving the file, you must save it using the class name (remember about case sensitivity) and add “.java” at the end of the name (if the names do not match, your program will not compile), for example, “MyJavaProgram” is the name of the class then the file should be saved as “”.
  • public static void main (String args []) – program processing begins with the main () method, which is an obligatory part of every program.

Java identifiers

Identifiers are names used for classes, variables and methods. All Java components require names.

There are several rules in Java syntax to remember about an identifier. They are as follows:

  • Each identifier must start with “A” through “Z” or “a” through “z”, “$” or “_”.
  • Any combination of characters can be after the first character.
  • The keyword cannot be used as an identifier.
  • Most importantly, an identifier in Java is case sensitive.
  • Correct spelling example: age, $ salary, _value, __1_value.
  • An example of a misspelling: 123abc, -salary.


Enumerations were introduced in Java 5.0. They constrain the variable to only select one of several predefined values. The values ​​in this enumerated list are called enumerations .

By using enumeration in Java, you can reduce errors in your code.

For example, if looking at orders for fresh juice in a store, one could limit the size of the juice pack for small, medium, and large. This makes it possible to use enumeration in Java so that no one orders any other package size other than small, medium, or large.

Java enum code example

class FreshJuice {

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

public class FreshJuiceTest {

    public static void main (String args []) {
       FreshJuice juice = new FreshJuice ();
       juice.size = FreshJuice.FreshJuiceSize.MEDIUM;
       System.out.println ("Size:" + juice.size);

The resulting output from the above example:


Note: in Java, enums can be declared either independently or within a class. Methods, variables, constructors can also be defined inside an enumeration.

Variable types

Later in Java we will see the following existing types:

  • Local variables.
  • Class variables (static).
  • Instance variables (dynamic).


As in other languages, in Java you can modify classes, methods, and so on, using modifiers. Modifiers in Java fall into two categories:

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

We’ll take a closer look at class modifiers, method modifiers, and others in the next section.


In Java, an array is an object that stores multiple variables of the same type. However, the array itself is an object. We’ll look at how to create and populate an array in later chapters.

Comments in Java

The Java language supports single-line and multi-line comments , which are very similar to those used in C and C ++. All symbols are not available inside any comments and are ignored by the compiler.

Comment example

public class MyFirstJavaProgram {

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

     public static void main (String [] args) {
        // One line comment.
        / * Also a one-line comment. * /
        System.out.println ("Hello world");

Keywords in Java

There are 50 keywords in total in Java . The table below shows a list of reserved keywords.

Keyword list
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while    

Important! The keyword cannot be used as a constant or variable, or any other identifier name.

Empty line

Empty string – strings in Java containing only spaces, possibly with a comment. Java completely ignores a line with spaces and comments.


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


An interface in Java can be defined as a contract between objects on how to communicate with each other. It plays a vital role when it comes to the concept of inheritance.

An interface defines the methods of a derived class (subclass) and how they should be used. However, the implementation of the methods depends entirely on the subclass.