Table of contents
•Basic Structure of Java Programs.
•A Simple Java Program to Print
"Hello Java"
•Let's Look into Various Parts of the
Above Java Program
Basic structure of Java
program
Document Section
• The documentation section is an important section but
optional for a Java program.
• It includes 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 write the statements in the documentation section,
we use comments.
• The comments may be single-line, multi-line, and
documentation comments.
Example
• 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 Statement
• The package declaration is optional. It is placed
just after the documentation section.
• In 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.
• It is necessary because a Java class can be
placed in different packages and directories
based on the module they are used.
Continued...
• For all these classes package belongs to a single
parent directory.
• We use the keyword package to declare the
package name.
• For example:
• package javatpoint; //where javatpoint is the
package name
• package com.javatpoint; //where com is the
root directory and javatpoint is the subdirectory
Interface Section
• It is 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.
• It contains only constants and method
declarations. Another difference is that it
cannot be instantiated.
Continued...
• 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();
}
Class definition
• 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
constants.
• Every Java program has at least one class that contains
the main() method. For example:
class Student //class definition
{
}
Method definition
• 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 sample program to print
"Hello Java"
//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 into various parts of
the program
• 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.
Continued...
• /* 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.
Continued...
• Braces
• Two curly brackets {...} are used to group all
the commands.
• So it is known that the commands belong to
that class or method.
Continued...
• public 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 objects.
• 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.
Continued...
• String[] args
• It 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.
Continued...
• 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.

Multi Threading- in Java WPS Office.pptx

  • 2.
    Table of contents •BasicStructure of Java Programs. •A Simple Java Program to Print "Hello Java" •Let's Look into Various Parts of the Above Java Program
  • 3.
    Basic structure ofJava program
  • 4.
    Document Section • Thedocumentation section is an important section but optional for a Java program. • It includes 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 write the statements in the documentation section, we use comments. • The comments may be single-line, multi-line, and documentation comments.
  • 5.
    Example • 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*/
  • 6.
    Package Statement • Thepackage declaration is optional. It is placed just after the documentation section. • In 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. • It is necessary because a Java class can be placed in different packages and directories based on the module they are used.
  • 7.
    Continued... • For allthese classes package belongs to a single parent directory. • We use the keyword package to declare the package name. • For example: • package javatpoint; //where javatpoint is the package name • package com.javatpoint; //where com is the root directory and javatpoint is the subdirectory
  • 8.
    Interface Section • Itis 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. • It contains only constants and method declarations. Another difference is that it cannot be instantiated.
  • 9.
    Continued... • We canuse 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(); }
  • 10.
    Class definition • Inthis 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 constants. • Every Java program has at least one class that contains the main() method. For example: class Student //class definition { }
  • 11.
    Method definition • Inthis 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[]) {
  • 12.
    A sample programto print "Hello Java" //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"); }
  • 13.
    Let's look intovarious parts of the program • 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.
  • 14.
    Continued... • /* 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.
  • 15.
    Continued... • Braces • Twocurly brackets {...} are used to group all the commands. • So it is known that the commands belong to that class or method.
  • 16.
    Continued... • public staticvoid 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 objects. • 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.
  • 17.
    Continued... • String[] args •It 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.
  • 18.
    Continued... • System.out.println(); • Thisstatement 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.