Java 4
1. Importance of the main() Method in Java
Every Java application must have a main() method as the entry point.
Signature:
public static void main(String[] args)
Breakdown:
Keyword Meaning
public Accessible from anywhere (required for JVM to access it)
static Can be called without creating an object
void No return value
String[] args Input arguments passed from command-line, stored as a String array
Example:
java
CopyEdit
public class Example {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
2. Understanding import and Library Classes
Java has many predefined classes in packages (e.g., [Link] , [Link] ).
Java 4 1
You import classes from packages to use their features.
Example:
java
CopyEdit
import [Link];
public class Calculator {
double i = 25;
double x = [Link](i); // Using predefined Math class
}
3. Creating and Using Classes
Java supports OOP. You define classes and use objects to access their properties
and methods.
Example:
java
CopyEdit
class Calculator {
double i;
double x;
void computeSquareRoot() {
x = [Link](i);
}
}
public class Example {
public static void main(String[] args) {
Calculator a = new Calculator();
a.i = 20;
Java 4 2
[Link]();
[Link]("Result: " + a.x);
}
}
4. Output in Java: [Link]() , println() , printf()
Method Description
print() Outputs without newline
println() Outputs and moves to next line
printf() Formatted output like in C
Example:
java
CopyEdit
[Link]("Line 1");
[Link]("Line 2 ");
[Link]("Continues");
[Link]("\nResult: %.2f", 5.6789);
🧑💻 5. Command Line Input ( String[] args )
You can pass inputs when running a Java program from terminal.
Example:
java
CopyEdit
public class Echo {
public static void main(String[] args) {
for (int i = 0; i < [Link]; i++) {
Java 4 3
[Link](args[i] + " ");
}
}
}
Run Command:
bash
CopyEdit
java Echo Hello World Java
# Output: Hello World Java
6. Runtime Input Using Scanner (Recommended)
Use Scanner class (from [Link] ) to read user input at runtime.
Example:
import [Link];
public class SumExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Sum: " + (a + b));
}
}
Java 4 4
7. Reading a List of Inputs
Example (Reading numbers until user stops):
import [Link].*;
public class AverageCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ArrayList<Integer> numbers = new ArrayList<>();
[Link]("Enter numbers (Ctrl+Z to stop):");
while ([Link]()) {
[Link]([Link]());
}
int sum = 0;
for (int num : numbers) sum += num;
double avg = (double) sum / [Link]();
[Link]("Average: " + avg);
}
}
8. Input with DataInputStream (Old Method, Less Used)
Used to read raw input and then convert to required type.
Requires [Link].*
Example:
java
CopyEdit
import [Link].*;
Java 4 5
public class InterestCalculator {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream([Link]);
[Link]("Enter principal: ");
float principal = [Link]([Link]());
[Link]("Enter rate: ");
float rate = [Link]([Link]());
[Link]("Enter time (years): ");
int time = [Link]([Link]());
float interest = (principal * rate * time) / 100;
[Link]("Simple Interest: " + interest);
}
}
import [Link];
public class InterestCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter principal: ");
float principal = [Link]();
[Link]("Enter rate: ");
float rate = [Link]();
[Link]("Enter time (years): ");
int time = [Link]();
float interest = (principal * rate * time) / 100;
[Link]("Simple Interest: " + interest);
Java 4 6
[Link](); // Optional but good practice
}
}
Java 4 7