Persiapan Pemrograman Java di Linux
78 / 100

Java Program Basic Structure

Understanding the Structure and Writing Rules Java syntax is generally divided into 4 part:

  1. Package Declaration
  2. Impor Library
  3. Class Section
  4. Method Main

Let's look at an example:

package com.petanikode.program; //<- 1. deklarasi package

import java.io.File; //<- 2. Impor library

class Program { //<- 3. Bagian class

    public static void main(String args[]){ //<- 4. Method main
        System.out.println("Hello World");
    }

}

Let's discuss, one by oneā€¦

1. Package Declaration

Package is a folder that contains a set of Java programs.

Package declarations are usually done when creating large programs or applications.

Example of a package declaration:

package com.petanikode.program;

Usually the package name follows the domain name of the vendor that issued the program.

In the example above, com.petanikode is the domain name of the code farmer.

The rules: domain name reverse, then followed by the name of the program.

What if we don't declare package?

It's okay and the program will still run.

But later during production, for example when create android apps.

We have to declare package.

2. Import Section

In this section, we import the required libraries in the program.

The library is a group class and functions that we can use in making programs.

Example of library import:

import java.util.Scanner;

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

3. Class Section

Java is a programming language that uses the OOP paradigm (Object Oriented Programming).

Every 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?

Just understand class as a program name declaration.

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

This is a class block.

The class block is opened with parentheses { then closed or ended with }.

Inside the class block, we can fill it with methods or functions and also variables.

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

4. Method Main

Method main() or function main() is the program block to be executed first.

This is the entry point of the program.

Method main() we must make. If not, then the program will not be executed.

Example method main().

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

It should be written like this...

Method main() have parameters args[]. This parameter will later store a value of the argument in command line.

Please read: What is a String Function[] args in Java Programming?

Then in the method main(), there is a statement or function:

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

This is a function to display text to the monitor 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("Apa kabar?");
var x = 3;
var y = 8;
var z = x + y;

Statements and expressions will be instructions that will be executed by the computer.

In the example above, we tell the computer to display the text "Hello World", and "Apa kabar?".

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

Java Program Block

A program block is a collection of statements and expressions wrapped into one.

Program blocks are always opened with curly braces { and closed with }.

Example of a program block:

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

    // blok program if
    if( true ){
        System.out.println('True');
    }

    // blok program for
    for ( int i = 0; i<10; i++){
        System.out.println("Perulangan ke"+i);
    }
}

The main thing is: if you find the brackets { and }, then that is as much as a program block.

Program blocks can also contain other program blocks (nested).

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

We will study this block in:

Writing Comments in Java

Comments are part of the program that will not be executed by the computer.

Comments are usually used for:

  • Giving information on the program code;
  • Deactivating certain functions;
  • Creating documentation;
  • dll.

Writing comments on java, same as in C language. same as in C language:

  1. same as in C language (//) same as in C language;
  2. same as in C language (/*...*/) same as in C language.

Example:

public static void main(String args[]){
    // ini adalah komentar satu baris
    System.out.println("Hello World");
    
    // komentar akan diabaikan oleh komputer
    // berikut ini fungsi yang di-non-aktifkan dengan komentar 
    // System.out.println("Hello World");

    /*
    Ini adalah penulisan komentar
    yang lebih dari
    satu baris
    */
}

same as in C language

same as in C language. same as in C language.

same as in C language: "Hello world"

same as in C language, same as in C language.

same as in C language, same as in C language.

Example: 'Hello world'.

same as in C language:

  • same as in C language ("...") same as in C language;
  • While single quotation marks ('...') to make a character.

Case Sensitive

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

Example:

String nama = "Petani Kode";
String Nama = "petanikode";
String NAMA = "Petanikode.com";

System.out.println(nama);
System.out.println(Nama);
System.out.println(NAMA);

The three variables are three different variables, although equally bernam nama.

Many beginners are often wrong on this. Because you can't tell which variables use uppercase letters and which ones use lowercase letters.

When we create a variable like this:

String jenisKelamin = "Laki-laki";

Then we should call it like this:

System.out.println(jenisKelamin);

Not like this:

System.out.println(jeniskelamin);

Take note, alphabet K is a capital letter.

Case Writing Style

Case writing style (case style) used by Java is: camelCase, PascalCase, and ALL UPPER.

writing style camelCase used in variable names, object name, and the name of the method.

Example:

String namaSaya = "Dian";

Then for PascalCase used in writing class names.

Example:

class HelloWOrld {
    //...
}

Pay attention to the class name, we use capital letters at the beginning, and capital letters on the letters W to separate two syllables.

Whereas camelCase lowercase first letter, and the prefix of the next syllable is capitalized.

// ini camelCase
belajarJava

// ini PascalCase
BelajarJava

Then, writing ALL UPPER or arbitrarily capital is used in the creation of constant names.

Example:

public final String DB_NAME = "petanikode";

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

Can I write arbitrarily?

For example, for class name using ALL UPPER?

It's okay, the program will not error. But the program code that you write will look dirty and out of the guideline that has been set.

source : https://www.petanikode.com/java-sintaks/

 862 total views,  4 views today

By admin

Writer

Leave a Reply

Your email address will not be published.

Web Hosting Murah Yang Terbaik Di Indonesia