0% found this document useful (0 votes)
2 views

1725382350

The document outlines the structure of a Java program, including sections such as documentation, package declaration, import statements, interface section, class definition, and the main method. It explains the use of comments, variable declarations, identifiers, and exception handling, as well as data types and user input using the Scanner class. Additionally, it provides examples of basic Java programs and their outputs.

Uploaded by

ayushyadav73026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1725382350

The document outlines the structure of a Java program, including sections such as documentation, package declaration, import statements, interface section, class definition, and the main method. It explains the use of comments, variable declarations, identifiers, and exception handling, as well as data types and user input using the Scanner class. Additionally, it provides examples of basic Java programs and their outputs.

Uploaded by

ayushyadav73026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lect-04: Programming Structures in Java

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.

import java.util.*; //imports all the classes in util package


import java.util.StringTokenizer; // imports only the StringTokenizer class from util package
Interface Section
It is an optional section in the Java Structure Program. We can create an interface using the interface keyword. An
interface is slightly different from a class. An interface only contains constants and method declarations. There is
one more difference between an interface and a class interface that cannot be instantiated.
interface Code {
void write();
void debug();
}
In the above code, we have defined an interface named Code, which contains two method declarations,
namely write() and debug(), with no method body. The body of abstract methods is implemented in those classes
that implement the interface Code
Class Definition
This is a mandatory section in the structure of Java program. Each Java program has to be written inside a class as
it is one of the main principles of Object-oriented programming that Java strictly follows, i.e., its Encapsulation for
data security.
There can be multiple classes in a program. Some conventions need to be followed to name a class. They should
begin with an uppercase letter.
class Program{
// class definition
}

Class Variables and Variables


Identifiers are used to name classes, methods, and variables. It can be a sequence of uppercase and lowercase
characters. It can also contain '_' (underscore) and '$' (dollar) signs. It should not start with a digit (0-9) and not
contain any special characters.
Variables are also known as identifiers in Java. It is a named memory location which contains a value. In a single
statement, we're able to declare multiple variables of the same type.

Declaring (Creating) Variables : Syntax:


<Data-Type> <Variable Name> or
<Data-Type> <Variable Name> = value;

String name = "John";


int myNum = 15;

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

Main Method Class


This is the entry point of the compiler where the execution starts. It is called/invoked by the Java Virtual Machine
or JVM.
The main () method should be defined inside a class. We can call other functions and create objects using this
method. The following is the syntax that is used to define.
public static void main (String[] args) {
// Method logic
}
Java is an OOP language which is case-sensitive, platform-independent, and uses both compiler and interpreter.
Java code is case sensitive.
Statements are terminated with a semicolon (;).
Blocks of code are enclosed in curly braces ({ }).
Exception Handling
Exception handling allows you to handle errors and unexpected situations gracefully and controlled.
Try-Catch Blocks: Try-catch blocks are used to handle exceptions in Java.
Throwing Exceptions: Exceptions can also be thrown manually using the throw keyword.
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There are two types of data
types in Java:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

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

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values

Basic Java program


class HelloWorld {
public static void main(String[] args) {
System.out.println("\t Compile time program ");
System.out.println(" Welcome II-Year Mechanical");
}}
Output of Compile time program
Welcome II-Year Mechanical students
Java User Input (Scanner)
The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner
class documentation. In our example, we will use the nextLine() method, which is used to read Strings:
Example-01
import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}}
Output
Enter username
Allen
Entered Username is: Allen

Input Types Method Description

nextBoolean() Reads a boolean value from the user

nextByte() Reads a byte value from the user

nextDouble() Reads a double value from the user

nextFloat() Reads a float value from the user

nextInt() Reads a int value from the user

nextLine() Reads a String value from the user

nextLong() Reads a long value from the user

nextShort() Reads a short value from the user

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

You might also like