0% found this document useful (0 votes)
0 views15 pages

Programming in Java

Java is a high-level, object-oriented, platform-independent programming language that allows developers to write code once and run it anywhere using the Java Virtual Machine (JVM). Key features include simplicity, security, robustness, and the ability to handle multiple tasks simultaneously. The document also explains the structure of Java programs, data types, and provides examples of control structures and static methods.
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)
0 views15 pages

Programming in Java

Java is a high-level, object-oriented, platform-independent programming language that allows developers to write code once and run it anywhere using the Java Virtual Machine (JVM). Key features include simplicity, security, robustness, and the ability to handle multiple tasks simultaneously. The document also explains the structure of Java programs, data types, and provides examples of control structures and static methods.
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

PROGRAMMING IN JAVA:

 Java is a high-level, object-oriented, platform-independent


programming language developed by Sun Microsystems in 1995,now
owned by Oracle.
 It is easy to learn, works on all devices, and is based on the idea of
objects (like real-life items).
 It allows developers to write code once and run it anywhere, thanks to
the Java Virtual Machine (JVM).

Features of Java:

Feature Description
1. Simple Easy to learn and use, especially if you know C or
C++.
2. Object-Oriented Everything is based on objects and classes.
[Link]- Write code once, run it anywhere (WORA) using the
Independent Java Virtual Machine (JVM).
4. Secure Java programs run in a controlled environment
(JVM), protecting your system.
5. Robust Java has strong memory management and error
handling (try-catch blocks).
6. Portable Java code can run on any machine with Java installed
— no need to change the code.
7. Multithreaded Java can run multiple tasks at once (like downloading
while watching a video).
[Link] Performance Fast enough for most tasks.
9. Compiled + Java code is compiled into bytecode and then
Interpreted interpreted by the JVM.

C++ is Platform Dependent because:

 When you write a C++ program, it is compiled into machine code.


 Machine code is specific to the operating system and hardware (like
Windows, Mac, or Linux).
 So, if you compile a C++ program on Windows, it will only run on
Windows — not on Mac or Linux unless you recompile it on those
systems.
Java Is Different (Platform-Independent)

 Java code is compiled into bytecode (not machine code).


 Bytecode runs on the Java Virtual Machine (JVM).
 JVMs exist for all platforms (Windows, Mac, Linux), so the same Java
program runs anywhere.

How Java Program is Executed

Java uses both a compiler and an interpreter.

Step 1: Write the Code

You write a Java program:

public class Hello {


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

Step 2: Java Compiler

 The Java compiler (javac) takes your code and converts it into
bytecode.
 Bytecode is not for any specific computer — it’s like a universal
language.
 It creates a file like: [Link]

Think of this as:

Compiler translates your code into a special file (bytecode) that any Java
system can understand.

Step 3: Java Interpreter (JVM)

 Now the Java Virtual Machine (JVM) reads the bytecode file
([Link]) and interprets it line by line.
 It runs the program on your computer.

Think of this as:

Interpreter (JVM) reads and runs your program step by step.

Simple Analogy:

🔸 You write a letter in English (Java code).


🔸 The compiler translates it into a universal language (bytecode).
🔸 The interpreter (JVM) reads it out loud in the local language (your
computer system).

✅ Basic Structure of a Java Program


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

Line What It Does


public class Hello { Declares a class named
Hello. A class is like a
blueprint for the program.
public static void This is the main method – the
main(String[] args) { starting point of every Java
program.
[Link]("Hello, Prints text to the screen. Here,
world!"); it shows: Hello, world!
} These are closing brackets to
end the method and the class.

Think of it like a box (class) that has a main switch (main method), and inside
that switch, you put actions like printing or doing math.
What is public static void main(String[] args)?

It’s the starting point of every Java program.


This is the first line the computer looks for to start running your code.

Word Meaning
public Anyone can use it. It is visible to everything.
static Belongs to the class, not to an object. You can run it
without creating an object.
void Means this method does not return anything.
main The name of the method — this is where the program
starts.
String[] A way to receive input from the user when the program
args starts.

VARIABLES:
A container to store data.
int age = 25;

Data Types in Java:

Primitive Data Types

Primitive types in Java are predefined and built into the


language, while non-primitive types are created by the
programmer (except for String).

There are eight primitive data types in Java:


Non-primitive data types - such as String, Arrays and Classes
JDK (Java Development Kit):

Used to CREATE (develop) Java programs

 Includes everything you need to write, compile, and run Java code.
 Contains:
o JRE (Java Runtime Environment)
o Compiler (javac)
o Debugger, tools, documentation

JRE (Java Runtime Environment):

Used to RUN Java programs

 Includes:
o JVM (Java Virtual Machine) → Runs .class files
o Java libraries (classes, files)
 No compiler or development tools.

JVM (Java Virtual Machine):

 The Java Virtual Machine (JVM) is a crucial component of the JRE that
provides the runtime environment for Java programs.
 It is responsible for executing Java bytecode on any platform that
supports JVM, making Java a “write once, run anywhere” language.
 The JVM is included in the JRE and is not installed separately.

BASIC JAVA PROGRAMS;


public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
IFELSE EXAMPLE:
public class IfElseExample {
public static void main(String[] args) {
int number = -5; // You can change this value

if (number >= 0) {
[Link]("The number is positive.");
} else {
[Link]("The number is negative.");
}
}
}

ELSE-IF LADDER:
public class GradeChecker {
public static void main(String[] args) {
int marks = 75; // You can change this value to test

if (marks >= 90) {


[Link]("Grade: A");
} else if (marks >= 75) {
[Link]("Grade: B");
} else if (marks >= 50) {
[Link]("Grade: C");
} else {
[Link]("Grade: Fail");
}
}
}

SWITCH CASE:

public class DayOfWeek {


public static void main(String[] args) {
int day = 3; // You can change this number from 1 to 7

switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;

case 3:
[Link]("Wednesday");
break;

case 4:
[Link]("Thursday");
break;

case 5:
[Link]("Friday");
break;

case 6:
[Link]("Saturday");
break;

case 7:
[Link]("Sunday");
break;

default:
[Link]("Invalid day number!");
}
}
}

For Loop – Print numbers 1 to 5


public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Number: " + i);
}
}
}

While Loop – Print numbers 1 to 5


public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("Number: " + i);
i++;
}
}
}

Do-While Loop – Print numbers 1 to 5


public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
[Link]("Number: " + i);
i++;
} while (i <= 5);
}
}

Java Program Using for-each Loop


public class ForEachExample {
public static void main(String[] args) {
// Create an array of fruits
String[] fruits = {"Apple", "Banana", "Orange", "Mango"};

// Use for-each loop to print each fruit


for (String fruit : fruits) {
[Link]("Fruit: " + fruit);
}
}
}

What is a Class in Java?


A class is like a blueprint or template.

🔹 It defines what an object will have (variables) and what it can do


(methods).
Think of a class as a design plan for a car — it describes features (like
color, speed) and actions (start, stop) but isn’t a real car itself.

What is an Object in Java?


An object is a real thing created based on the class.

It's like a real car built using the blueprint. It has its own data and can perform
actions.

EXAMPLE:
// This is the class (blueprint)
class Car {
// variables
String color = "Red";

// method
void drive() {
[Link]("The car is driving.");
}
}

public class CarDemo {


public static void main(String[] args) {
// This is an object created from the class
Car myCar = new Car();

// Accessing object's data and method


[Link]("Car color: " + [Link]);
[Link]();
}
}

STATIC KEYWORDS:
The static keyword means something belongs to the class, not the object.
EXAMPLE:

Suppose we have a Student class.

Each student (object) has their own name, age, marks (these are non-static).

But the school name is the same for all students – this should be static.

Static = Common for All Objects


You don’t need to create an object to use a static variable or method.

PROGRAM 1: STATIC VARIABLE

class Student {

static String schoolName = "ABC School"; // Static variable

String name;

void display() {

[Link]("Name: " + name);

[Link]("School: " + schoolName); // Same for all

}
}

public class StaticExample {

public static void main(String[] args) {

Student s1 = new Student();

[Link] = "Ravi";

Student s2 = new Student();

[Link] = "Priya";

[Link]();

[Link]();

PROGRAM 2: STATIC METHOD

class Calculator {

static int add(int a, int b) {

return a + b;
}

public class StaticMethodExample {

public static void main(String[] args) {

int result = [Link](5, 3); // No object needed

[Link]("Result: " + result);

You didn't create an object of Calculator — because add() is static,


you can call it directly using the class name.

You might also like