Unit 1 : - Basic Syntactical constructs in Java
1. Java Features
The primary objective of Java programming language creation was to make it
portable, simple and secure programming language. Apart from this, there are also
some excellent features which play an important role in the popularity of this
language.
I. Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun Microsystem, Java language is a simple programming
language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
II. Object Oriented
Java is an object-oriented programming language. Everything in Java is an
object. Object-oriented means we organize our software as a combination of
different types of objects that incorporate both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
III. Portable
IV. Platform Independent
V. Secure
VI. Robust
VII. Architectural – Neutral
2. Basic structure of java
Java is an object-oriented programming, platform-
independent, and secure programming language that makes it popular. Using the
Java programming language, we can develop a wide variety of applications.
1. Documentation 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. Whatever we write in the
documentation section, the Java compiler ignores the statements during the execution 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.
o Single-line Comment: It starts with a pair of forwarding slash (//). For example:
//First Java Program
o 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*/
o Documentation Comment: It starts with the delimiter (/**) and ends with */. For
example:
/**It is an example of documentation comment*/
2. 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. We use the
keyword package to declare the package name. For example:
package javatpoint; //where javatpoint is the package name
package [Link]; //where com is the root directory and javatpoint is the subd
irectory
3. Import Statements
The package contains the many predefined classes and interfaces. If we want to use
any class of a particular package, we need to import that class. The import statement
represents the class stored in the other package. We use the import keyword to import
the class. It is written before the class declaration and after the package statement.
We use the import statement in two ways, either import a specific class or import all
classes of a particular package. In a Java program, we can use multiple import
statements. For example:
import [Link]; //it imports the Scanner class only
import [Link].*; //it imports all the class of the [Link] package
4. Class Definitions
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
{
}
5. Main Method Class
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[])
{
}
Public : Public is access modifier. Main method need to be public so that JRE can
access and execute this method.
Static : When a java program starts there is no object o f a class present. The main
method has to be static so that JVM can load a class into memory and call the main
method without creating an instance of a class first.
Void : Every java method must have a return type void means method does not return
any value.
Main : It is main of the method which is predefined and cannot be changed.
3. Java Tokens
4. Data types in java
5. Constants and Symbolic constants
6. Variables