Friday, May 15, 2020

Understanding The Basics Structure And Java Syntax


Each programming language has a different syntax writing structure and rules. Java is a programming language developed from C language and of course it will follow the writing style of C.

When you first see a Java program, you might be wondering. What the f**k is that?


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

Many things we do not know yet.
What the package is about?

What the class is about?

And why should it be written like that?

Therefore, we need to learn the basic syntax and structure of Java programs.

Let's get started…

Basic Structure of The Java Program

Java program structure is generally divided into 4 parts:
  • Package Declaration
  • Import Library
  • Class section
  • Main method
Let's look at an example:

// package declaration
package com.codingforjava.program;

// Import library

// Class section
class Program {
    // Method Main
    public static void main(String args[]){
        System.out.println("Hello World");


Let's discuss them one by one…

1. Package Declaration
Package is a folder that contains a collection of Java programs. Package declarations are usually used when creating large programs or applications.

Example package declaration:

package com.codingforjava.program;

Normally, the package name follows the domain name of a vendor who made the program.

In the example above, I use com.codingforjava where it is the domain name of Coding For Java. The rule is that domain name is reversed, then the program name is followed.

What if we don't declare the package?

It's okay and the program will still work. But, later during production, for example when creating an Android application, we must declare the package.

2. Import Section
In this section, we import the libraries needed for the program. Library is a collection of classes and functions used in creating programs.

Example of importing a library:

import java.util.Scanner;

In the example above, we import the Scanner class from the java.util package.

3. Class section
Java is a programming language that uses the OOP (Object Oriented Programming) paradigm. Each program must be wrapped in a class so that later it can be made into an object.

If you don't understand what OOP is?

Simply understand the class as a program name declaration.

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

The above is an example of a class block.

The class block is opened with curly braces {then closed or ended with}. Inside the class block, we can fill it with methods or functions as well as variables.

In the example above, there is the main() method.

4. Main method
The main () method or the main () function are the blocks of the program to be executed first.

This is the entry point of the program.

We must make the main() method. Otherwise, the program will not be executed.

Example of the main() method.

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

The writing should be like this ...

The main() method has the args[] parameter. This parameter will store a value from the argument in the command line.

Then, inside the main () method, we have a statement or function:

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

The code above is a function to display text to the screen.

Statements and Expressions in Java

Statements and expressions are the smallest parts of the program. Every statement and expression in Java must end with a semicolon (;).

Examples of statements and expressions:

System.out.println("Hello World");
System.out.println("How are you?");
var x = 3;
var y = 8;
var z = x + y;

Statements and expressions are instructions that will be run by the computer.

In the example above, we tell the computer to display the text "Hello World", and "How are you?".

Then we tell him to calculate the value of x + y.

Java Program Blocks

A program block is a collection of statements and expressions wrapped together. Program blocks are always opened with curly braces {and closed with}.

Example block program:

// main program block
public static void main(String args[]){
    System.out.println("Hello World");
    System.out.println("Hello Code");

    // if block
    if( true ){

    // for block
    for ( int i = 0; i<10; i++){
        System.out.println("Recurrence to"+i);

The point is, if you find the {and} brackets, then it is a program block. Program blocks can also contain other program blocks (nested).

In the example above, the main() program block contains the if and for blocks.

We will learn more about blocks in:

Branching on Java.
Iteration in Java.

Comments on Java

Comments are a part of the program which is not executed by the computer.

The comment function is:
  • Give information on the program code.
  • Disabling certain functions.
  • Make documentation, etc.
Writing comments on java is the same as in C language using:

Double slashes (//) for single-line comments;
Star slash (/*...*/) for long comments.


public static void main(String args[]){
    // this is a one-line comment
    System.out.println("Hello World");
    // comments will be ignored by the computer
    // The following functions are deactivated by comments
    // System.out.println("Hello World");

     with more than
     one line

Strings and Characters

A string is a collection of characters. We often know it as a text.

Example string: "Hello world"

Strings in Java must be enclosed in double quotes as in the example above. If it is enclosed in single quotes, it will become a character.

Example: 'Hello world'.

So please make it a concern:

Double quotes ("...") to make a string;
While single quotation marks ('...') to make a character.

Case Sensitive

Java is Case Sensitive, meaning uppercase or capital letters and lowercase letters are distinguished.


String name = "My Name";
String Name = "myname";
String NAME = "yourname";


The three variables above are three different variables, although they all use names as their variable names.

Many beginners are often wrong on this matter. Because they cannot distinguish which variables use uppercase letters and which use lowercase letters.

If we make a variable like this:

String myCar = "Mercedes-Benz";

Then we have to call like this:


Not like this:


Note, the letter C is capitalized.

Case Writing Style

The case styles used by Java are camelCase, PascalCase, and ALL UPPER.

The camelCase writing style is used for variable names, object names, and method names.


String myName = "Howard";

Then for PascalCase, it is used for writing class names.


class HelloWorld {

Note the name of the class, we use capital letters in the beginning, and capital letters in the letter W to separate the two prefix.

As for camelCase, the front letters use lowercase letters, and the next prefix uses uppercase letters.

// this is camelCase

// this is PascalCase

Then, writing ALL UPPER or all capital is used to create a constant name.


public final String DB_NAME = "javacoder";

For writing two or more syllables, ALL UPPER is separated by the bottom line or underscore (_).

Is it okay I write about anything?

For example for the class name using ALL UPPER?

It's okay, the program won't get an error. But the program code will look dirty and out of the guide line that has been set.

Those are some rules of writing Java syntax and basic program structure that should be known. Happy coding ...