T a b l e o f contents B asic Structure of Java Programs. A Simple Java Program to Print "Hello Java" Let's Look into Various Parts of the Above Java Program
B a s i c s t r u cture of J a v a program
Document Section The documentation section is an important section but optional for a Java program . I t i ncludes basic information about a Java program. The information includes the author's name, date of creation, version, program name, company name, and description of the program. It improves the readability of the program. To w rite the statements in the documentation section, we use comments. The comments may be single-line, multi-line, and documentation comments.
E x a m p le Single-line Comment: It starts with a pair of forwarding slash (//). For example / /First Java Program Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For example: /*It is an example of multiline comment*/ Documentation Comment: It starts with the delimiter (/**) and ends with */. For example: /**It is an example of documentation comment*/
Package S tatement The package declaration is optional. It is placed just after the documentation section. I n this section, we declare the package name in which the class is placed. Note that there can be only one package statement in a Java program. It must be defined before any class and interface declaration. I t is necessary because a Java class can be placed in different packages and directories based on the module they are use d .
C o n t i n u e d . . . For all these classes package belongs to a single parent directory. We use the keyword package to declare the package name. For example : p ackage javatpoint; //where javatpoint is the package name package com.javatpoint; //where com is the root directory and javatpoint is the subdirectory
I n t e r f a ce S e c t i o n I t i s an optional section. We can create an interface in this section if required. We use the interface keyword to create an interface. An interface is a slightly different from the class. I t contains only constants and method declarations. Another difference is that it cannot be instantiated.
C o n t i nued . . . We can use interface in classes by using the implements keyword. An interface can also be used with other interfaces by using the extends keyword. For example: interface car { void start(); void stop(); }
C l a s s d e f i n i t ion In this section, we define the class. It is vital part of a Java program. Without the class, we cannot create any Java program. A Java program may conation more than one class definition. We use the class keyword to define the class. The class is a blueprint of a Java program. It contains information about user-defined methods, variables, and constant s . Every Java program has at least one class that contains the main() method. For example: class Student //class definition { }
Method d e f i nition In this section, we define the main() method. It is essential for all Java programs. Because the execution of all Java programs starts from the main() method. In other words, it is an entry point of the class. It must be inside the class. Inside the main method, we create objects and call the methods. We use the following statement to define the main() method: public static void main(String args[]) { }
A s a m p l e p r o g r am to print " H e l l o J a v a " / / Name of this file will be "Hello.java" public class Hello { /* Author: www.w3schools.in Date: 2018-04-28 Description: Writes the words "Hello Java" on the screen */ public static void main(String[] args) { System.out.println("Hello Java"); } }
Let's look i n to various p a r t s of the p r o g r a m public class Hello This creates a class called Hello. All class names must start with a capital letter. The public word means that it is accessible from any other classes.
C o n t i nued . . . /* Comments */ The compiler ignores comment block. Comment can be used anywhere in the program to add info about the program or code block . which will be helpful for developers to understand the existing code in the future easily.
C o n t i n u e d . . . Braces Two curly brackets {...} are used to group all the commands . S o it is known that the commands belong to that class or method .
C o n t i n u e d . . . p ublic static void main When the main method is declared public, it means that it can also be used by code outside of its class, due to which the main method is declared public. The word static used when we want to access a method without creating its object, as we call the main method, before creating any class object s . The word void indicates that a method does not return a value. main() is declared as void because it does not return a value. main is a method; this is a starting point of a Java program.
C o n t i n u e d . . . String[] args I t is an array where each element of it is a string, which has been named as "args". If your Java program is run through the console, you can pass the input parameter, and main() method takes it as input .
C o n t i n u e d . . . System.out.println(); This statement is used to print text on the screen as output . where the system is a predefined class, and out is an object of the PrintWriter class defined in the system. The method println prints the text on the screen with a new line. You can also use print() method instead of println() method. All Java statement ends with a semicolon.