0% found this document useful (0 votes)
15 views5 pages

Java Topics With Examples

Uploaded by

sabeershaik009
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)
15 views5 pages

Java Topics With Examples

Uploaded by

sabeershaik009
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
You are on page 1/ 5

Java Basics - Topic-wise Explanation

with Code Examples


1. Java Introduction
Java ek high-level, object-oriented language hai jo platform-independent hai.
Java code pehle compile hota hai (javac se), fir .class file JVM pe run hoti hai.
WORA = Write Once, Run Anywhere.

2. First Java Program


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

Explanation:
- Class ka naam HelloWorld hai
- main method se Java program start hota hai
- println output print karta hai

3. Variables and Data Types


Code:
int age = 20;
float pi = 3.14f;
char grade = 'A';
boolean isPass = true;
String name = "Sabeer";

Explanation:
- int = numbers
- float = decimal
- char = single character
- boolean = true/false
- String = text
4. Operators
Code:
int a = 10, b = 5;
[Link](a + b); // 15
[Link](a > b); // true

Explanation:
- +, -, *, / = arithmetic operators
- >, <, == = comparison
- &&, || = logical operators

5. Control Statements (if-else, switch)


Code:
int marks = 85;
if(marks >= 90) [Link]("A");
else if(marks >= 75) [Link]("B");
else [Link]("C");

switch example:
int day = 2;
switch(day) {
case 1: [Link]("Mon"); break;
case 2: [Link]("Tue"); break;
}

Explanation:
- if-else for conditions
- switch for fixed value conditions

6. Loops
Code:
for(int i=1; i<=5; i++) { [Link](i); }

while loop:
int i=1;
while(i<=5) { [Link](i); i++; }

do-while:
int i=1;
do { [Link](i); i++; } while(i<=5);
Explanation:
- Repeating tasks ke liye use hota hai

7. Arrays
Code:
int[] arr = {10, 20, 30};
[Link](arr[0]); // 10

Explanation:
- Arrays same type ke elements ka group hai
- Index 0 se start hota hai

8. Functions (Methods)
Code:
public static int add(int a, int b) {
return a + b;
}

Calling:
int result = add(5, 10);
[Link](result); // 15

Explanation:
- Function code reuse karta hai
- return value wapas bhejta hai

9. OOP Concepts
Code:
class Student {
String name; int age;
}
Student s = new Student();
[Link] = "Ali";

Inheritance:
class Dog extends Animal {}

Explanation:
- Class = blueprint
- Object = real-world item
- Inheritance, Encapsulation, Polymorphism, Abstraction = 4 pillars of OOP
10. Exception Handling
Code:
try {
int a = 10 / 0;
} catch(Exception e) {
[Link]("Error: " + e);
}

Explanation:
- Error aane pe program crash na ho, uske liye try-catch use hota hai

11. File Handling


Code:
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java");
[Link]();

Scanner sc = new Scanner(new File("[Link]"));


while([Link]()) { [Link]([Link]()); }
[Link]();

Explanation:
- File write aur read karne ke liye Java IO classes use hoti hain

12. Access Modifiers


Code:
private int data;
public void setData(int d) { data = d; }

Explanation:
- public = sab jagah accessible
- private = sirf class ke andar
- protected = subclass ke liye
- default = same package

13. Packages & Libraries


Code:
import [Link];
Scanner sc = new Scanner([Link]);
Explanation:
- [Link], [Link] jaise packages already Java mein built-in hote hain
- import se inka use karte hain

You might also like