Chapter 3: Overview of Java Language
3.1 Simple JAVA Program
Class SampleOne
{
Public static void main (String args[])
{
System.out.println (“Java is better than C++.”);
}
}
3.1.1 Class Declaration
The first line
Class SampleOne
Declares a class. As Java is a object-oriented language and therefore everything must be placed inside a
class. Class is a keyword and declares that a new class definition follows. SampleOne is a Java identifier
that specifies the name of the class to be defined.
3.1.2 Opening Brace
Every class definition in Java begins with an opening brace “{“ and ends with a matching closing brace
“}”.
3.1.3 The main Line
Public static void main (String args[])
Keyword Description
Public The keyword public is an access specifier that declares the main method as
unprotected and therefore making it accessible to all other classes.
Static Static which declares this method as one that belongs to the entire class and
not a part of any objects of the class. The main always be declared as static
since interpreter uses this method before any objects are created.
Void The type modifier void states that the main method does not return any value.
3.1.4 The Output Line
System.out.println (“Java is better than C++.”);
This line prints the string Java is better than C++. To the screen. Every Java statement must end with a
semicolon.
3.2 Java Program Structure
3.2.1. Documentation Section
The documentation section comprises a set of comment lines giving the name of the program, the
author and other details. Comments must explain why and what of classes and how of algorithms.
/**…… */ this form of comment is called documentation comment.
3.2.2. package Statement
The first statement allowed in a Java file is a package statement. This statement declares a package
name and informs the compiler that the classes defined here belong to this package.
Example:
Package student;
Package statement is optional.
3.2.3. Import Statements
The next thing after a package statement may be several import statements.
3.1 Simple JAVA Program
Class SampleOne
{
Public static void main (String args[])
{
System.out.println (“Java is better than C++.”);
}
}
3.1.1 Class Declaration
The first line
Class SampleOne
Declares a class. As Java is a object-oriented language and therefore everything must be placed inside a
class. Class is a keyword and declares that a new class definition follows. SampleOne is a Java identifier
that specifies the name of the class to be defined.
3.1.2 Opening Brace
Every class definition in Java begins with an opening brace “{“ and ends with a matching closing brace
“}”.
3.1.3 The main Line
Public static void main (String args[])
Keyword Description
Public The keyword public is an access specifier that declares the main method as
unprotected and therefore making it accessible to all other classes.
Static Static which declares this method as one that belongs to the entire class and
not a part of any objects of the class. The main always be declared as static
since interpreter uses this method before any objects are created.
Void The type modifier void states that the main method does not return any value.
3.1.4 The Output Line
System.out.println (“Java is better than C++.”);
This line prints the string Java is better than C++. To the screen. Every Java statement must end with a
semicolon.
3.2 Java Program Structure
3.2.1. Documentation Section
The documentation section comprises a set of comment lines giving the name of the program, the
author and other details. Comments must explain why and what of classes and how of algorithms.
/**…… */ this form of comment is called documentation comment.
3.2.2. package Statement
The first statement allowed in a Java file is a package statement. This statement declares a package
name and informs the compiler that the classes defined here belong to this package.
Example:
Package student;
Package statement is optional.
3.2.3. Import Statements
The next thing after a package statement may be several import statements.