1725382350
1725382350
Structure of JAVA
Documentation Section
The documentation section is an important section in the Structure of the Java Program. It includes basic
information about the Java program. However, it is also optional, which means you can omit it from the Java
program. It improves the readability of the program. Java compiler ignores them during the program execution.
Comments are non-executable parts of a program
There are different types of comments: single-line, multi-line, and documentation comments.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed).
This example uses a single-line comment before a line of code:
public class Main {
public static void main(String[] args) {
// This is a comment
System.out.println("Hello World");
}}
Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by Java.
public class Main {
public static void main(String[] args) {
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
}
}
Documentation comment- It is a comment that starts with /** and ends with */
Package Declaration
A package declaration appears within a compilation unit to indicate the package to which the compilation unit
belongs. A package is a collection of related Java files stored in a directory or folder. Ideally, each package
represents a logical grouping of functionality
In Java, we have to save the program file name with the same name as the name of public class in that file.
The extension should be java. Java programs are run using the following two commands:
javac fileName.java // To compile the Java program into byte-code
java fileName // To run the program
Import Statements
Import statements are used to import classes, interfaces, or enums (An enum is a special "class" that represents a
group of constants) that are stored in packages or the entire package. A package contains many predefined classes
and interfaces.
We need to mention which package we are using at the beginning of the program. We do it by using the import
keyword. We either import the entire package or a specific class from that package.
Identifiers
All Java variables must be identified with unique names. These unique names are called identifiers.
The general rules for naming variables are:
● Names can contain letters, digits, underscores, and dollar signs
● Names must begin with a letter
● Names should start with a lowercase letter, and cannot contain whitespace
● Names can also begin with $ and _ (but we will not use it in this tutorial)
● Names are case-sensitive ("myVar" and "myvar" are different variables)
● Reserved words (like Java keywords, such as int or boolean) cannot be used as names
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
Example-02
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
String name = myObj.nextLine();
int age = myObj.nextInt();
float salary = myObj.nextFloat();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}}}
Output of the Input_type Program
Enter name, age and salary:
Allen
25
25000
Name: Allen
Age: 25
Salary: 25000.0