Complete study material for degree & diploma students – with examples in both languages
What is Java? Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It follows the principle "Write Once, Run Anywhere" (WORA), meaning compiled Java code can run on any platform that supports Java without recompilation.
Key Features:
Basic Structure of a Java Program:
main() method – entry pointJava क्या है? Java एक हाई-लेवल, ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग भाषा है जिसे 1995 में Sun Microsystems (अब Oracle) ने बनाया था। यह "Write Once, Run Anywhere" (एक बार लिखो, कहीं भी चलाओ) के सिद्धांत पर काम करती है – मतलब Java का बना हुआ कोड बिना बदलाव के किसी भी प्लेटफॉर्म पर चल सकता है।
मुख्य विशेषताएँ:
Java प्रोग्राम की बेसिक संरचना:
main() मेथड – यहीं से प्रोग्राम शुरू होता है// My first Java program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation: public class HelloWorld defines a class. public static void main(String[] args) is the entry point. System.out.println() prints output.
व्याख्या: public class HelloWorld एक क्लास को डिफाइन करता है। public static void main(String[] args) प्रोग्राम का एंट्री पॉइंट है। System.out.println() आउटपुट प्रिंट करता है।
Basic Syntax Rules:
;).// single line or /* multi line */.{ }.myVar and myvar are different.Identifiers: Names given to classes, methods, variables. Must begin with a letter, underscore, or dollar sign; cannot use keywords.
बेसिक सिंटैक्स के नियम:
;) लगाना जरूरी है।// एक लाइन वाला या /* कई लाइनों वाला */।{ } के अंदर होते हैं।myVar और myvar अलग-अलग हैं।आइडेंटिफायर्स: क्लास, मेथड, वेरिएबल को दिए गए नाम। नाम अक्षर, अंडरस्कोर या डॉलर चिह्न से शुरू हो सकता है; कीवर्ड नहीं हो सकते।
// Syntax example
public class SyntaxDemo {
public static void main(String[] args) {
int number = 10; // variable declaration
System.out.println("Number is: " + number);
}
}
Explanation: The program declares an integer variable and prints it. Notice the semicolons and braces.
व्याख्या: इस प्रोग्राम में एक इंटिजर वेरिएबल डिक्लेयर किया गया है और उसकी वैल्यू प्रिंट की गई है। सेमीकोलन और ब्रेसेस पर ध्यान दें।
↑ Back to TopJava has two categories of data types:
1. Primitive Data Types (8 types):
byte (1 byte) – -128 to 127short (2 bytes)int (4 bytes) – most common integer typelong (8 bytes) – suffix L or lfloat (4 bytes) – suffix F or fdouble (8 bytes) – default for decimalschar (2 bytes) – single Unicode characterboolean (1 bit) – true/false2. Non-Primitive (Reference) Types: classes, interfaces, arrays, enums.
Java में डेटा टाइप दो तरह के होते हैं:
1. प्रिमिटिव डेटा टाइप (8 प्रकार):
byte (1 बाइट) – -128 से 127 तकshort (2 बाइट्स)int (4 बाइट्स) – सबसे ज्यादा इस्तेमाल होने वाला इंटिजर टाइपlong (8 बाइट्स) – अंत में L या l लगाते हैंfloat (4 बाइट्स) – अंत में F या f लगाते हैंdouble (8 बाइट्स) – दशमलव के लिए डिफॉल्ट टाइपchar (2 बाइट्स) – एक अक्षर (Unicode)boolean (1 बिट) – true या false2. नॉन-प्रिमिटिव (रेफरेंस) टाइप: क्लास, इंटरफ़ेस, ऐरे, एनम।
// Data types example
public class DataTypesDemo {
public static void main(String[] args) {
byte b = 100;
short s = 5000;
int i = 100000;
long l = 100000L;
float f = 5.75f;
double d = 19.99;
char c = 'A';
boolean bool = true;
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + bool);
}
}
Explanation: Each variable is declared with its data type. Note the suffixes for long and float.
व्याख्या: हर वेरिएबल को उसके डेटा टाइप के साथ डिक्लेयर किया गया है। long और float के अंत में L/l और F/f लगाना जरूरी है।
↑ Back to TopVariables: A variable is a container that holds a value. It must be declared with a type and a name. Variables can be local (inside a method), instance (inside a class but outside methods), or static (class variables).
Constants: In Java, constants are declared using the final keyword. A final variable cannot be reassigned. Convention: constant names are in uppercase with underscores.
final int DAYS_IN_WEEK = 7;
वेरिएबल (Variables): वेरिएबल एक कंटेनर होता है जिसमें वैल्यू स्टोर होती है। इसे एक टाइप और नाम के साथ डिक्लेयर करना पड़ता है। वेरिएबल तीन तरह के हो सकते हैं: लोकल (मेथड के अंदर), इंस्टेंस (क्लास के अंदर लेकिन मेथड के बाहर), या स्टेटिक (क्लास वेरिएबल)।
कॉन्स्टैंट्स (Constants): Java में कॉन्स्टैंट बनाने के लिए final कीवर्ड का इस्तेमाल करते हैं। final वेरिएबल की वैल्यू एक बार सेट करने के बाद बदली नहीं जा सकती। नामकरण की परंपरा: सभी अक्षर बड़े (uppercase) और शब्दों के बीच अंडरस्कोर।
final int DAYS_IN_WEEK = 7;
// Variables and constants
public class VarConstDemo {
static int staticVar = 10; // static variable
int instanceVar = 20; // instance variable
final double PI = 3.14159; // constant
public static void main(String[] args) {
int localVar = 30; // local variable
System.out.println("Local: " + localVar);
System.out.println("Static: " + staticVar);
VarConstDemo obj = new VarConstDemo();
System.out.println("Instance: " + obj.instanceVar);
System.out.println("Constant PI: " + obj.PI);
}
}
Explanation: The program demonstrates local, instance, and static variables, as well as a final constant.
व्याख्या: इस प्रोग्राम में लोकल, इंस्टेंस और स्टेटिक वेरिएबल दिखाए गए हैं, साथ ही एक final कॉन्स्टैंट भी।
↑ Back to TopOutput: System.out.println() prints a line; System.out.print() prints without newline.
Input: Java provides the Scanner class (in java.util) for reading input. Other classes: BufferedReader, Console.
आउटपुट: System.out.println() से एक लाइन प्रिंट होती है; System.out.print() से बिना नई लाइन के प्रिंट होता है।
इनपुट: Java में इनपुट लेने के लिए Scanner क्लास (java.util पैकेज में) सबसे आसान तरीका है। अन्य क्लास: BufferedReader, Console।
// Input and output using Scanner
import java.util.Scanner;
public class IODemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", you are " + age + " years old.");
sc.close();
}
}
Explanation: Scanner is used to read input. nextLine() reads a string, nextInt() reads an integer. Always close the scanner.
व्याख्या: Scanner से इनपुट लिया जाता है। nextLine() स्ट्रिंग पढ़ता है, nextInt() इंटिजर पढ़ता है। स्कैनर को बंद करना (close) अच्छी आदत है।
Java provides rich set of operators:
+ - * / %== != < > <= >=&& || != += -= *= /= %=++ --& | ^ ~ << >>? :Java में कई तरह के ऑपरेटर होते हैं:
+ - * / %== != < > <= >=&& || != += -= *= /= %=++ --& | ^ ~ << >>? :// Operators example
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a > b = " + (a > b));
System.out.println("(a > 5) && (b < 5) = " + ((a > 5) && (b < 5)));
a += 5; // a = a + 5
System.out.println("a after += 5: " + a);
int max = (a > b) ? a : b;
System.out.println("Max using ternary: " + max);
}
}
Java supports if, else if, else, and switch statements.
Java में if, else if, else, और switch स्टेटमेंट होते हैं, जिनसे कंडीशन के आधार पर अलग-अलग कोड चलाया जा सकता है।
// Control structures
public class ControlDemo {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Other day");
}
}
}
Java provides while, do-while, for, and enhanced for-each loops.
Java में while, do-while, for, और एन्हांस्ड for-each लूप होते हैं। लूप का मतलब होता है किसी कोड को बार-बार चलाना जब तक कंडीशन सही रहे।
// Loops example
public class LoopsDemo {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println();
// while loop
int j = 1;
while (j <= 5) {
System.out.print(j + " ");
j++;
}
System.out.println();
// for-each loop on array
int[] arr = {10, 20, 30, 40};
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
An array is a container that holds a fixed number of values of a single type. Index starts from 0.
Declaration: int[] arr = new int[5]; or int[] arr = {1,2,3};
ऐरे एक कंटेनर है जिसमें एक ही टाइप के कई वैल्यू (फिक्स्ड नंबर) स्टोर किए जा सकते हैं। इंडेक्स 0 से शुरू होता है।
डिक्लेरेशन: int[] arr = new int[5]; या int[] arr = {1,2,3};
// Arrays example
public class ArraysDemo {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// access and print
System.out.println("First element: " + numbers[0]);
// loop through
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println();
// 2D array
int[][] matrix = { {1,2}, {3,4} };
System.out.println("Element at [1][0]: " + matrix[1][0]);
}
}
Strings are objects in Java. They are immutable. Common methods: length(), charAt(), substring(), equals(), compareTo(), toUpperCase(), etc.
Java में Strings ऑब्जेक्ट होते हैं। ये अपरिवर्तनीय (immutable) होते हैं – एक बार बन गए तो बदले नहीं जा सकते। कुछ आम मेथड: length() (लंबाई), charAt() (किसी पोजीशन का कैरेक्टर), substring() (सबस्ट्रिंग निकालना), equals() (तुलना), toUpperCase() (बड़े अक्षर में बदलना) आदि।
// String example
public class StringDemo {
public static void main(String[] args) {
String str = "Hello Java";
System.out.println("Length: " + str.length());
System.out.println("Char at 0: " + str.charAt(0));
System.out.println("Substring (6): " + str.substring(6));
System.out.println("Uppercase: " + str.toUpperCase());
String str2 = "hello java";
System.out.println("Equals (ignore case): " + str.equalsIgnoreCase(str2));
}
}
Methods are blocks of code that perform a specific task. They can return a value or be void. Methods can be overloaded (same name, different parameters).
मेथड कोड का एक ब्लॉक होता है जो कोई खास काम करता है। वह कोई वैल्यू लौटा सकता है या void (कुछ नहीं लौटाता) हो सकता है। मेथड को ओवरलोड किया जा सकता है – मतलब एक ही नाम से अलग-अलग पैरामीटर वाले मेथड बना सकते हैं।
// Methods example
public class MethodsDemo {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) { // overloaded
return a + b;
}
static void greet(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
int sum = add(5, 3);
System.out.println("Sum (int): " + sum);
System.out.println("Sum (double): " + add(5.5, 2.3));
greet("Alice");
}
}
A class is a blueprint for creating objects. An object is an instance of a class. It has state (fields) and behavior (methods).
Concepts: encapsulation, inheritance, polymorphism, abstraction.
क्लास एक ब्लूप्रिंट की तरह होती है, जिससे ऑब्जेक्ट बनाए जाते हैं। ऑब्जेक्ट क्लास का एक उदाहरण (instance) होता है। उसमें स्टेट (फील्ड) और बिहेवियर (मेथड) होते हैं।
OOP के मुख्य कॉन्सेप्ट: एनकैप्सुलेशन (डेटा छुपाना), इनहेरिटेंस (विरासत), पॉलीमोर्फिज्म (बहुरूपता), एब्स्ट्रैक्शन (सिर्फ जरूरी चीजें दिखाना)।
// Class and object example
class Student {
String name;
int age;
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class OOPDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
s1.display();
}
}
Inheritance: One class acquires properties of another using extends. Supports single inheritance.
Polymorphism: Same method name behaves differently based on object (method overriding).
इनहेरिटेंस (विरासत): एक क्लास दूसरी क्लास के गुणों को extends कीवर्ड से प्राप्त कर सकती है। Java में सिर्फ एक ही क्लास से इनहेरिट कर सकते हैं (single inheritance)।
पॉलीमोर्फिज्म (बहुरूपता): एक ही मेथड का नाम, लेकिन अलग-अलग ऑब्जेक्ट के साथ अलग व्यवहार। यह मेथड ओवरराइडिंग से होता है।
// Inheritance and polymorphism
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Dog barks (polymorphism)
}
}
Exceptions are runtime errors. Java provides try-catch-finally blocks to handle them. Use throw to explicitly throw an exception, and throws to declare them.
एक्सेप्शन वो एरर हैं जो प्रोग्राम चलते समय आती हैं (runtime errors)। Java में इन्हें हैंडल करने के लिए try-catch-finally ब्लॉक होते हैं। throw से खुद एक्सेप्शन फेंक सकते हैं, और throws से मेथड के बारे में बता सकते हैं कि वह एक्सेप्शन फेंक सकता है।
// Exception handling
public class ExceptionDemo {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 100; // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught: " + e);
} finally {
System.out.println("Finally block always executes");
}
}
}
Java provides the java.io and java.nio packages for file operations. Common classes: File, FileReader, FileWriter, BufferedReader, BufferedWriter.
Java में फ़ाइल ऑपरेशन के लिए java.io और java.nio पैकेज हैं। आम इस्तेमाल होने वाली क्लास: File, FileReader, FileWriter, BufferedReader, BufferedWriter।
// File writing and reading
import java.io.*;
public class FileDemo {
public static void main(String[] args) {
// Write to file
try (FileWriter fw = new FileWriter("test.txt")) {
fw.write("Hello, Java File!\n");
fw.write("This is a test.");
} catch (IOException e) {
e.printStackTrace();
}
// Read from file
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}