0% found this document useful (0 votes)
5 views10 pages

Simple Java Program

The document provides a comprehensive overview of a simple Java program, explaining its structure, including classes, methods, and variables. It covers key concepts such as the main method, printing output, user input, conditionals, loops, and object-oriented programming principles. Additionally, it summarizes important Java concepts like classes, objects, methods, and data types.

Uploaded by

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

Simple Java Program

The document provides a comprehensive overview of a simple Java program, explaining its structure, including classes, methods, and variables. It covers key concepts such as the main method, printing output, user input, conditionals, loops, and object-oriented programming principles. Additionally, it summarizes important Java concepts like classes, objects, methods, and data types.

Uploaded by

ssindhiya1992
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Java Program

public class Main {


public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Line-by-Line Explanation
public class Main

public

 Access modifier.
 Means this class can be accessed from anywhere.

Think of it like:
“Everyone can use this class.”

class

 Blueprint/template for objects and code.

Every Java program contains at least one class.

Main

 Class name.
 Can be any valid name.

Example:

class MyProgram

Curly Braces
{
}

 Define code blocks.


 Everything inside belongs to the class or method.
Main Method
public static void main(String[] args)

This is the most important line in a Java program.

Java starts execution from main().

public

 Accessible from anywhere.

static

 Method belongs to class itself.


 No object needed to run it.

void

 Method returns nothing.

Example:

void test()

means no return value.

main

 Entry point of Java application.

Program starts here.

(String[] args)

 Stores command-line arguments.


 String[] means array of strings.
Usually beginners don’t use it immediately.

Printing Output
[Link]("Hello, World!");

System

 Built-in Java class.

Contains useful tools.

out

 Output stream object.

Used to display text.

println()

 Prints text and moves to next line.

Example:

[Link]("Hi");
[Link]("Bye");

Output:

Hi
Bye

"Hello, World!"

 String/text value.

Strings use double quotes.

 Ends statement.
Almost every Java statement ends with semicolon.

Complete Program Flow


1. Java starts program
2. main() method runs
3. println() executes
4. Text appears on screen
5. Program ends

Example Output
Hello, World!

Variables
String name = "John";
int age = 20;

String

 Datatype for text.

Example:

String city = "Dubai";

int

 Datatype for whole numbers.

Example:

int marks = 95;

 Assignment operator.
 Stores value into variable.
Printing Variables
[Link](name);
[Link](age);

Output:

John
20

Taking User Input


import [Link];

Scanner sc = new Scanner([Link]);

[Link]("Enter your name: ");


String name = [Link]();

Scanner

 Built-in class for user input.

new Scanner([Link])

 Creates Scanner object.


 Reads keyboard input.

nextLine()

 Reads full line of text.

Example:

Enter your name: Alex

Combining Text and Variables


[Link]("Hello " + name);

If:

name = "Alex";
Output:

Hello Alex

 Concatenation operator.
 Joins strings together.

If Condition
int age = 18;

if (age >= 18) {


[Link]("Adult");
}

if

 Checks condition.

>=

 Greater than or equal to.

Examples:

10 >= 5 // true
3 >= 8 // false

Else Condition
if (age >= 18) {
[Link]("Adult");
} else {
[Link]("Minor");
}

else

 Runs when condition is false.


For Loop
for (int i = 0; i < 5; i++) {
[Link](i);
}

Output:

0
1
2
3
4

for

 Repeats code multiple times.

i++

 Increases value by 1.

Equivalent:

i = i + 1;

While Loop
int count = 1;

while (count <= 3) {


[Link](count);
count++;
}

Output:

1
2
3

Methods
public static void greet() {
[Link]("Hello");
}
Method
 Reusable block of code.

Calling Method
greet();

Output:

Hello

Method with Parameter


public static void greet(String name) {
[Link]("Hello " + name);
}

Calling
greet("Sam");

Output:

Hello Sam

Arrays
String[] fruits = {"Apple", "Banana", "Orange"};

 Stores multiple values.

Access Array Item


[Link](fruits[0]);

Output:
Apple

Array Indexes
Index Value
0 Apple
1 Banana
2 Orange

Classes and Objects


class Person {
String name;
}

Creating Object
Person p1 = new Person();
[Link] = "Alex";

new

 Creates object from class.

Accessing Object Data


[Link]([Link]);

Output:

Alex

Constructor
class Person {
String name;

Person(String n) {
name = n;
}
}
Constructor
 Special method.
 Runs automatically when object created.

Creating Object with Constructor


Person p1 = new Person("Alex");

Important Java Concepts Summary


Concept Meaning
Class Blueprint for objects
Object Instance of class
Method Reusable code
Variable Stores data
Loop Repeats code
Condition Decision making
Array Multiple values
Constructor Initializes object
String Text datatype
Scanner User input tool

You might also like