Java Program Structure

Java is an object-oriented programming language but also retains all features of a high-level programming language. An object-oriented programming language is class-based where a class represents a real-world entity.

Advertisements

The class itself does not do anything, but it is used to create objects that have properties and functions ( called methods) and it behaves like the real world entity that it represents. The object derives its set of properties and methods from the class.

For example,

A fruit is a class that represents a fruit, is a real-world entity. You can create objects from class fruit such as mango that has properties like color, taste, shape, and methods like hang_from_tree( ), fall_from_tree( ).

A person is a class that represents a real-world entity person. The driver, teacher are objects of class Person, the driver has properties like legs, arm, dress_code, salary and method like driving( ), starting_vehicle( ) and stopping_vehicle( ).

All properties and methods correspond to the real-world entity such as mango, driver whose properties and functions we already know.

Structure of a Java Program

In the previous section, we learned about classes and objects. Every java program has at least one class, if the program has only one class, then it is a public class.

The Java language put a lot of restrictions on classes such as class visibility. Class visibility decides who will access a particular Java class from outside of class or from another class.

The public class is visible to other classes, but the private classes are strictly restricted for other classes and functions outside of the class. We will discuss more on visibility in future articles.

Java File Name

The source code is saved in a file with specific extensions. In c/c++ language, the extension is .c or .cpp. The java has certain rules for a file name.

In a java program, there is only one public class. If the public class name is the car then the file name should be car.java. No restriction on what or how to decide a name for your class.

Import command

The java program starts with an import statement located at the top of the program source code. The import statement has the same task as the #include statement in C language. We import packages that hold some predefined classes.

When java program runs it looks for two places for classes, first, in the current user-defined package and second, inside imported packages.

Advertisements

Let us understand what packages does in java program. Consider the following example.

String name = "Jack";

A string is a class and its location is in java.lang package.

The above command is the same as

java.lang.String name = "jack";

By using the import statement you can avoid the lengthy prefixes in the program.

import java.lang.*;

The asterisk (*) represents all classes inside the java.lang package, but you can also specify a specific class if you know about it.

The Main Function

Similar to c/c++ language, Java also has a main function. Since Java is a object-oriented programming, it is hard to decide which object to create first and which method to invoke at the beginning. To solve this problem, the main function in java is a static function which does not belong to any particular object and starts even before other objects are created.

The main resides in the public class of java, which happens to be the only public class in java program. It is time to write to you first program.

Your First Program in Java – Hello World!

It is a convention to write a “hello world !” program when you start a programming language. The source code for ‘hello world” program is given below, that consists of all the elements we discuss above.

/*
 * File name   : helloworld.java
 * Data Created: July 12, 2018
 * Author      : Notesformsc.org
 * File Size   : Na
 * Licence     : Free
*/

import java.lang.*;

public class helloworld
{

    public static void main(String[] args)
    {

        //prints the output

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

    }

}

Output

Hello World !

Comments in Java

Comments are very useful in the java program to bring clarity to the program, especially if the program has a huge amount of code.

There are two types of comments available in java.

  1. Single line comments (  // ) – It is used for writing short comments that span only a single line.
  2. Multiline comments ( /*    … */) – It is usually for writing long-form comments at the beginning of the program providing all sorts of information about the author and the program.

There are no restrictions on commenting as far as position or placement is concerned, but common practice is to put a comment above or below the code block.

Also, you cannot nest or mix two types of comments.

Related Articles:

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.