0% found this document useful (0 votes)
34 views

Java Notes

The document provides examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditions, loops, strings and more. It shows how to declare and use variables of different types, print output, use arithmetic, relational and logical operators, write if/else conditional statements, for, while and do-while loops, break and continue keywords in loops, and string functions.

Uploaded by

Aman gangwar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Java Notes

The document provides examples of Java code demonstrating basic programming concepts like variables, data types, operators, conditions, loops, strings and more. It shows how to declare and use variables of different types, print output, use arithmetic, relational and logical operators, write if/else conditional statements, for, while and do-while loops, break and continue keywords in loops, and string functions.

Uploaded by

Aman gangwar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 24

public class Main {

public static void main(String[] args) {


System.out.println("Hello World!");
System.out.println("I am learning Java.");
System.out.println("It is awesome!");
}
}
-----------------------------------------------------------------------------------
-------------

Note that we don't use double quotes ("") inside println() to output numbers.
-----------------------------------------------------------------------------------
----------

public class Main {


public static void main(String[] args) {
System.out.println("Hello World! ");
System.out.print("I will print on the same line.");
}
}
----------------------------------------------------------------------------------
Variable

String - stores text, such as "Hello". String values are surrounded by double
quotes
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
boolean - stores values with two states: true or false

public class Main {


public static void main(String[] args) {
String name = "John";
System.out.println(name);
}
}

public class Main {


public static void main(String[] args) {
int myNum = 15;
System.out.println(myNum);
}
}

public class Main {


public static void main(String[] args) {
final int myNum = 15;
myNum = 20; // will generate an error
System.out.println(myNum);
}
}

-----------------------------------------------------------------------------------
-----------
type casting

public static void main(String[] args) {


int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt);
System.out.println(myDouble);
}
}
-----------------------------------------------------------------------------------
-

java operator
Java divides the operators into the following groups:

Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators

public class Main {


public static void main(String[] args) {
int sum1 = 100 + 50;
int sum2 = 899 + 250;
int sum3 = 9090 + sum2;
System.out.println(sum1);
System.out.println(sum2);
System.out.println(sum3);
}
}

public class Main {


public static void main(String[] args) {
int x = 5;
x *= 3;
System.out.println(x);
}
}

public class Main {


public static void main(String[] args) {
int x = 5;
int y = 3;
System.out.println(x != y); // returns true because 5 is not equal to 3
}
}

-----------------------------------------------------------------------------------
----------------------------------------

Java String

Strings are used for storing text.


A String variable contains a collection of characters surrounded by double quotes:

create a variable-
public class Main {
public static void main(String[] args) {
String a= "Hello";
System.out.println(a);
}
}

length of string
public class Main {
public static void main(String[] args) {
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println(txt.length());
}
}

public class Main {


public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}

***String Concatenation
The + operator can be used between strings to combine them. This is called
concatenation:
public class Main {
public static void main(String args[]) {
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
}
}

Note that we have added an empty text (" ") to create a space between firstName and
lastName on print.

-----------------------------------------------------------------------------------
-------------------------------------

Maths
public class Main {
public static void main(String[] args) {
System.out.println(Math.sqrt(121));
}
}
-----------------------------------------------------------------------------------
--------------------------------------

Boolean
public class Main {
public static void main(String[] args) {
boolean a= true;
boolean b= false;
System.out.println(a);
System.out.println(b);
}
}
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9
}
}

public class Main {


public static void main(String[] args) {
int x = 10;
System.out.println(x == 10); // returns true, because the value of x is equal
to 10
}
}

___________________________________________________________________________________
________________________________

class JavaExample
{
public static void main(String args[])
{
//creating string using string literal
String s1 = "BeginnersBook";
String s2 = "BeginnersBook";

//creating strings using new keyword


String s3 = new String("BeginnersBook");
String s4 = new String("BeginnersBook");

if(s1 == s2){
System.out.println("String s1 and s2 are equal");
}else{
System.out.println("String s1 and s2 are NOT equal");
}

if(s3 == s4){
System.out.println("String s3 and s4 are equal");
}else{
System.out.println("String s3 and s4 are NOT equal");
}

}
}

____________________________________________________________________________
public class Main{
public static void main(String[] args){
String str1 = "Aman";
String str2 = new String("Aman");
if(str1.equals(str2)){
System.out.println("Str1 and Str2 are equal");
}
else
{
System.out.println("not equal");
}
}
}_____________________________________________________________

Java Conditions and If Statements

Java supports the usual logical conditions from mathematics:

Less than: a < b


Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b

if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.

public class Main {


public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}

public class Main {


public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}

if-else-if
public class Main {
public static void main(String[] args) {
int time = 12;
if (time < 10)
{
System.out.println("Good morning.");
}
else if (time < 20)
{
System.out.println("Good day.");
}
else
{
System.out.println("Good evening.");
}
}
}
-----------------------------------------------------------------------------------
--------------------------------
public class Main {
public static void main(String[] args) {
int marks=82;
if(marks >90 && marks <100){
System.out.println("grade A");

}
else if(marks >80 && marks <90)
{
System.out.println("grade B");
}
else if(marks >70 && marks <80)
{
System.out.println("grade C");
}
else if(marks >60 && marks <70)
{
System.out.println("grade D");
}
else if(marks >50 && marks <60)
{
System.out.println("grade E");
}
else if(marks >40 && marks <50)
{
System.out.println("grade F");
}
else
{
System.out.println("Fail");
}
}
}
___________________________________________________________________________________
_______OP--B
Switch

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
public class Main {
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}

-----------------------------------------------------------------------------------
--------------------------------------

While Loop
while (condition) {
// code block to be executed
}

public class Main {


public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}

public class Main {


public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
}
}
-----------------------------------------------------------------------------------
-------------------------------------------
for loop
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}

public class Main {


public static void main(String[] args) {
for (int i = 0; i <= 10; i = i + 2) {
System.out.println(i);
}
}
}

-----------------------------------------------------------------------------------
--------------------------------------
Java Brake and continue
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
}
}

****************** break and cont in while Loop

public class Main {


public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 4) {
break;
}
}
}
}

-----------------------------------------------------------------------------------
---------------------------------------------------

Array

public class Main {


public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
}
}

* change in aaray element

public class Main {


public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
}
}

****Array length
public class Main {
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
}
}

*****For each
Syntax:
for (type variable : arrayname) {
...
}

________________________________________________-
public class Main
{
public static void main(String[] args) {

int nums[]={12,13,14,15};
for(int x:nums)
System.out.println(x);
}
}

public class Main {


public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
}
}
Method program
public class Main{
public static void Add(){
int x = 5;
int y = 6;
int z = x + y;
System.out.println(z);
}
public static void main(String[] args){
Add();
}
}
------------------------------------------------------------------------------

public class Main {


static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}

public static void main(String[] args) {


myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}

----------------------------------------------------------------------------------
Overloading multiple methods can have the same name with different parameters:

Overloading eg:

public class Main {


static int plusMethod(int x, int y) {
return x + y;
}

static double plusMethod(double x, double y) {


return x + y;
}

public static void main(String[] args) {


int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
}

_-----------------------------------------------------------------------------
Recursion eg
public class Main {
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
public static void main(String[] args) {
int a = sum(10);
System.out.println(a);
}

}
-----------------------------------------------------------------------------------
-
Method overloading

public class Test {


public static int myMethod(int x, int y ){
return x+y;
}
public static double myMethod(double x, double y ){
return x+y;
}

/**
* @param args
*/
public static void main(String[] args) {
int myNum1=myMethod(2,9); // isme hm variable bna rhe or method ki value
de rhe h
double myNum2 =myMethod(2.0,9.8); /// same
System.out.println(myNum1);
System.out.println(myNum2);
}

-----------------------------------------------------------------------------------
-

public class Test {


static int x=23330; // static
static double y=9.0; // static

/**
* @param args
*/
public static void main(String[] args) {

System.out.println(x); so no use of object.


System.out.println(y);
}
}

___________________________________________________________________________________
______
public class Test {
int x=90;
int y=9;

/**
* @param args
*/
public static void main(String[] args) {
Test myObj= new Test();
Test myObj1= new Test();
myObj.x=30;
System.out.println(myObj.x);
System.out.println(myObj1.y);
}
}__________________________________________________________________________________
______________---

OOP stands for Object-Oriented Programming.


*******Java Classes and Objects

public class Main {


int x = 5;

public static void main(String[] args) {


Main myObj = new Main();
System.out.println(myObj.x);
}
}
* Two object create in one class

public class Main {


int x = 5;

public static void main(String[] args) {


Main myObj1 = new Main();
Main myObj2 = new Main();
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}

-----------------------------------------------------------------------------------
-----------------------------
Class Attributes
Another term for class attributes is fields.
public class Main {
int x;

public static void main(String[] args) {


Main myObj = new Main();
myObj.x = 40;
System.out.println(myObj.x);
}
}

change the value


public class Main {
int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}

public class Main {


final int x = 10; //final

public static void main(String[] args) {


Main myObj = new Main();
myObj.x = 25; // will generate an error
System.out.println(myObj.x);
}
}

** Class Method
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}

public static void main(String[] args) {


myMethod();
}
}

-----------------------------------------------------------------------------------
--------
static method, which means that it can be accessed without creating an object of
the class,
unlike public, which can only be accessed by objects:

public class Main {


// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}

// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}

// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method

Main= new Main(); // Create an object of MyClass


myObj.myPublicMethod(); // Call the public method
}
}

-----------------------------------------------------------------------------------
--------------------------------------
** Constructer
A constructor in Java is a special method that is used to initialize objects. The
constructor is called when an object of
a class is created. It can be used to set initial values for object attributes:
// Create a Main class
public class Main {
int x;

// Create a class constructor for the Main class


public Main() {
x = 5;
}

public static void main(String[] args) {


Main myObj = new Main();
System.out.println(myObj.x);
}
}

-----------------------------------------------------------------------------------
---------------------------------------------------------------
Package And API
import java.util.*; // import the java.util package

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
String userName;

// Enter username and press Enter


System.out.println("Enter username");
userName = myObj.nextLine();

System.out.println("Username is: " + userName);


}
}
-----------------------------------------------------------------------------------
---------------------------------------
******** Inheritance
In Java, it is possible to inherit attributes and methods from one class to
another. We group the "inheritance concept" into two categories:

subclass (child) - the class that inherits from another class


superclass (parent) - the class being inherited from

-----------------------------------------------------------------------------------
---------------------------------------------
**
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Pig myPig = new Pig();
Dog myDog = new Dog();

myAnimal.animalSound(); // create a object


myPig.animalSound(); // create a object
myDog.animalSound(); // create a object
}
}

-----------------------------------------------------------------------------------
-----------------------------------------
User Input

nextBoolean() ->boolean value from the user


nextByte() -> byte value from the user
nextDouble() -> double value from the user
nextFloat() -> float value from the user
nextInt() ->int value from the user
nextLine() -> String value from the user
nextLong() -> long value from the user
nextShort() -> short value from the user

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");

// String input
String name = myObj.nextLine();

// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();

// Output input by user


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}

-----------------------------------------------------------------------------------
----------------------------------
* Java Date
(1)- Local Date
import java.time.LocalDate; // import the LocalDate class
public class Main {
public static void main(String[] args) {
LocalDate myObj = LocalDate.now(); // Create a date object
System.out.println(myObj); // Display the current date
}
}

(2)- Local time


import java.time.LocalTime; // import the LocalTime class
public class Main {
public static void main(String[] args) {
LocalTime myObj = LocalTime.now(); // now() is method
System.out.println(myObj);
}
}

(3) Localdatetime
import java.time.LocalDateTime; // import the LocalDateTime class
public class Main {
public static void main(String[] args) {
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
}
}

-----------------------------------------------------------------------------------
------------------------------------
* ArrayList
(1) Add item

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}

(2)-Access item

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.get(0));
}
}
(3) Change the item then use set();

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
cars.set(0, "Opel");
System.out.println(cars);
}
}

(4).Remove an item
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
cars.remove(2);
System.out.println(cars);
}
}

(5) To remove all the elements in the ArrayList, use the clear() method:
import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
cars.clear();
System.out.println(cars);
}
}

(6) Size ();


import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.size());
}
}

*
ArrayList for-each loop
import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (String p : cars) {
System.out.println(p);
}
}
}

-------------
import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
}
}
}
--------------ArrayList sorting in integer type

import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(33);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(34);
myNumbers.add(8);
myNumbers.add(12);

Collections.sort(myNumbers);

for (int i : myNumbers) {


System.out.println(i);
}
}
}
-----------------------------------------------------------------------------------
-------------------------------------

Java LinkedList

1. Add first
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");

// Use addFirst() to add the item to the beginning


cars.addFirst("Mazda");
System.out.println(cars);
}
}

2. Add last
cars.addLast("Mazda");

3. remove first and last


import java.util.LinkedList;

public class Main {


public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

// Use removeFirst() remove the first item from the list


cars.removeFirst();
//cars.removeLast();
System.out.println(cars);
//System.out.println(cars);
}
}

5. getFirst and last


import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

// Use getFirst() to display the first item in the list


System.out.println(cars.getFirst());
System.out.println(cars.getLast());
}
}

-----------------------------------------------------------------------------------
------------------------------------------------
* Java Exception

Syntax
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
*

public class Main {


public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
___________________________________________________

public class Main {


public static void main(String[] args) {
try {
int[] myNumbers = {10, 20, 30};
System.out.println(myNumbers[10]); here 10 is index num
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}

o/p-Something went wrong.


_____________________________________________________

* finally
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}

Java में, multithreading एक प्रक्रिया है जिसके द्वारा हम बहुत सारें threads को एक साथ execute कर सकते हैं. इससे
CPU का अधिकतम utilization (उपयोग) होता है.
एक thread, प्रोसेसिंग का सबसे छोटा और lightweight sub-process होता है. Thread एक दूसरे से independent
(स्वतंत्र) होते है क्योंकि उनके execution का path
अलग होता है. अगर कभी किसी thread में कोई exception आ भी गया तो, उससे दूसरे threads को कोई प्रभाव नहीं पड़ता.

Multithreading का उपयोग multitasking को प्राप्त (achieve) करने के लिए किया जाता है. इसका प्रयोग ज्यादातर
games और animation को create करने में किया जाता है.
Advantage of Multithreading – इसके लाभ
यह user को block नहीं करता है क्योंकि threads एक दूसरे से independent होते है.
आप इसके द्वारा एक समय में बहुत सारें कार्य कर सकते है. जिससे समय की बचत होती है.
एक process के सभी threads इसके resources (जैसे कि – memory, data और files आदि) को share करते
है. जिससे हमें threads को अलग से resources को allocate करने की जरुरत नहीं पड़ती.
मल्टीथ्रेडिंग CPU के idle time को कम करता है जिससे system की performance बेहतर होती है.
What is Thread in Hindi – जावा थ्रेड क्या है?
Thread एक lightweight process होती है. जावा multithreaded programming को सपोर्ट करती है. एक
multithreaded program दो या दो से अधिक ऐसे parts को contain किये रहता है जो एक साथ run होते हैं. ऐसे
program का प्रत्येक part एक thread कहलाता है.

प्रत्येक thread के execution का path अलग होता है. और ये independent होते हैं जिससे अगर कभी एक thread में
कोई exception आ भी जाये तो उसका प्रभाव दूसरे threads पर नहीं पड़ता.
___________________________________________________________________________________
_________________________________________________________
इसमें दो प्रकार के thread होते है. पहला user thread और दूसरा daemon thread. (daemon threads का use
तब होता है जब हम application को clean करना चाहते है और इनको background में use किया जाता है.)

जावा में threads का प्रयोग करने के लिए ‘Thread Class’ का use किया जाता है.
Life Cycle of a Thread – थ्रेड का लाइफ साइकिल
थ्रेड की लाइफ साइकिल में पांच stages होती हैं जो कि निम्नलिखित हैं:-

multithreading in hindi
image source
Life Cycle of a Thread – थ्रेड का लाइफ साइकिल
थ्रेड की लाइफ साइकिल में पांच stages होती हैं जो कि निम्नलिखित हैं:-

New – एक थ्रेड अपना लाइफ साइकिल new state में शुरू करता है. यह इस state में तब तक रहता है जब तक start()
method को call नहीं किया जाता.
Runnable – जब थ्रेड को start() method के द्वारा start कर दिया जाता है तो यह runnable हो जाता है अर्थात् यह
runnable state में चला जाता है.
Running – एक थ्रेड running state में होता है यदि scheduler उसे select करता है.
Waiting – एक थ्रेड तब waiting state में होता है जब वह एक task को परफॉर्म करने के लिए दूसरे task के लिए wait
करता है.
Terminated – जब thread अपने task पूरा कर देता है तो वह terminated state में चला जाता है.
___________________________________________________________________________________
___________________________
Thread को Create कै से करते है?
इसे दो तरीकों से create किया जा सकता है:-

Thread class को extend करके


Runnable interface को implement करके

Thread class को extend करके


class MultiDemo extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
MultiDemo t1=new MultiDemo();
t1.start();
}
}
----------------------------------------------

class A extends Thread {


public void run(){
for(int i=1;i<=5;i++)
{
System.out.println("greeting");
}
}
}
class B {
public static void main(String[] args) {
A t=new A();
t.start();
for(int i=1;i<=5;i++)
{
System.out.println("Great");
}
}
}

___________________________________________________________________________________
_______________________________________
Runnable interface को implement करके
class MultiDemo implements Runnable{
public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


MultiDemo m1=new MultiDemo();
Thread t1 =new Thread(m1);
t1.start();
}
}
Thread class और runnable interface के बीच अंतर
यदि हम thread class को extend करते है तो हमारी class दूसरी classes को extend नहीं कर सकती. क्योंकि जावा
multiple inheritance को सपोर्ट नहीं करती है. परन्तु यदि हम runnable interface को implement करते है तो
हमारी class दूसरी base classes को extend कर सकती है.
हम thread class को extend करके एक thread की बेसिक कार्यविधि (functionality) को प्राप्त कर सकते हैं. क्योंकि
यह in-built methods जैसे:- yield(), interrupt() आदि प्रदान करता है. जबकि ये मेथड runnable
interface में उपलब्ध नहीं होती है

___________________________________________________________________________________
____________________________________________________
Thread methods in Java in Hindi
नीचे आपको thread class में मौजूद कु छ महत्वपूर्ण thread methods दी गयी हैं:-

start() इसका प्रयोग थ्रेड को start करने के लिए किया जाता है.
run() इसका प्रयोग थ्रेड को run करने के लिए किया जाता है.
sleep() यह thread को किसी एक time period के लिए suspend कर देता है.
join() यह थ्रेड के खत्म होने का इन्तजार करता है.
getPriority() यह थ्रेड की priority को return करता है.
setPriority() यह थ्रेड की priority को बदल देता है.
getName() यह थ्रेड के name को return करता है.
setName() यह थ्रेड के name को change कर देता है.
isAlive() यह check करता है कि थ्रेड alive (जीवित) है या नहीं.
yield() यह वर्तमान में execute हो रहे thread object को pause करता है और
दूसरे threads को execute करता है.
suspend() इसका प्रयोग thread को suspend करने के लिए किया जाता है.
resume() इसका प्रयोग suspended thread को resume करने के लिए किया जाता है.
stop() इसका उपयोग thread को stop करने के लिए किया जाता है.
destroy() इसका प्रयोग thread group को destroy करने के लिए किया जाता है.
interrupt() इसका प्रयोग थ्रेड को interrupt करने के लिए किया जाता है.

___________________________________________________________________________________
______________________________________

OOP का पूरा नाम object-oriented programming (ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग) है। OOPS concepts निम्नलिखित होते
है:-

Object
Class
Encapsulation
Abstraction
Inheritance
Polymorphism
Message Passing
Object
ऑब्जेक्ट, class का instance होता है जो कि variable के स्थान पर वास्तविक value को contain किये रहता है।
ऑब्जेक्ट एक बेसिक run-time entity होती है।
सामन्यतया, Object वह प्रत्येक वस्तु होती है जिसको कि पहचाना जा सकें । हमारी आसपास की सारी वस्तुएँ जैसे:-पेन, किताब, कु र्सी, गाडी,
टीवी आदि सभी ऑब्जेक्ट्स है।
___________________________________________________________________________________
_________________________________________

Class
क्लास एक ही तरह के objects का समूह होता है। जैसे:- आम, अमरुद तथा सेब आदि ये सभी फल है, और ये सभी class fruit
के सदस्य हुए।
क्लास यूजर-डिफाइंड डेटा टाइप होता है तथा क्लास data तथा functions का समूह होता है।

___________________________________________________________________________________
__________________________________________

Encapsulation
डेटा तथा फं क्शन को एक ही यूनिट में सम्मिलित करना (जोड़ना) Encapsulation कहलाता है। इसमें class के variables प्राइवेट
होते हैं और इन्हें class के बाहर direct access नहीं किया जा सकता. Encapsulation को class के रूप में use किया
जाता है. एक class में हम data और methods को एक यूनिट के रूप में एक साथ रख सकते हैं.

Java bean पूरी तरह से एक encapsulated class होती है क्योंकि इसमें सभी data members प्राइवेट होते हैं.

Abstraction
Abstraction का अर्थ है कि object के के वल आवश्यक जानकारी को प्रदर्शित करना तथा background की जानकारी को छु पाये
रखना।
उदाहरण के लिए– जब हम कोई car चलाते है तो हमें यह पता होता है कि जब accelarator को दबायेंगे तो speed बढ़ेगी और जब
break दबायेंगे तो Car रुक जायेगी. लेकिन हमें यह नहीं पता होता कि break दबाने से car क्यों रुक जाती है.

इसी प्रकार OOPS में complex (कठिन) चीजों को छु पा दिया जाता है और के वल आवश्यक तथा simple चीजों को show किया जाता
है. Java में, abstraction को प्राप्त करने के लिए abstract class और interface का प्रयोग किया जाता है.
___________________________________________________________________________________
__________________________________________________-
Inheritance
inheritance का अर्थ है ‘विरासत’।
जावा में एक क्लास के द्वारा दूसरी क्लास के properties(गुणों) तथा methods को inherit कर लेना inheritance कहलाता है।

वह क्लास जो दूसरी क्लास से derived होती है वह subclass कहलाती है तथा वह क्लास जिससे subclass derived हुई होती है
वह super class कहलाती है।
Superclass को हम base class भी कहते है तथा subclass को हम derived class भी कहते है।
___________________________________________________________________________________
____________________________________________

Polymorphism
polymorphism ग्रीक भाषा से लिया गया शब्द है जिसमें poly का अर्थ है many और morphism का अर्थ है forms. तो
polymorphism का अर्थ हुआ many forms.

Polymorphism एक ऐसा concept है जिसमें हम एक ही काम को दो भिन्न तरीके से कर सकते है।

जावा में Polymorphism दो तरह की होती है जो निम्न है:-


1:- Compile-time polymorphism (static polymorphism)
2:- Run-time polymorphism (Dynamic polymorphism)

1:- Compile time polymorphism:- Compile time polymorphism को हम method overloading या


early binding भी कहते है।

इस polymorphism का अर्थ है कि हम समान नाम के methods को different signatures के साथ declare करते है
क्योंकि हम अलग-अलग task को एक ही method name के साथ perform क्र सकते है।

2:- Run-time polymorphism:-इस प्रकार के polymorphism को late binding या dynamic binding या


method overriding कहते है।
इस polymorphism का अर्थ है कि हम समान नाम के methods को समान signature के साथ declare करते है।

oops concepts in hindi - polymorphism

Message Passing
Objects एक दूसरे को information (सूचना) send तथा receive करके आपस में communicate करते हैं.
objects उसी प्रकार message को send तथा receive करते है जिस प्रकार हम लोग करते हैं.
एक ऑब्जेक्ट के लिए message एक procedure (प्रक्रिया) के लिए request होता है और इसलिए receiving object में
एक method को invoke किया जाता है.

इसे पूरा पढ़ें:- message passing क्या है?

Advantage of OOP in Hindi


इसके लाभ निम्नलिखित होते हैं:-

इसमें program का structure बहुत ही simple होता है जिससे complexity कम होती है.
हमें इसमें सिर्फ एक बार code को लिखने की जरूरत होती है और उसे हम बार-बार use कर सकते हैं.
यह data redundancy प्रदान करता है.
इसमें हम आसानी से code को maintain किया जा सकता है जिससे time की बचत होती है.
object-oriented programming में data hiding और abstarction का प्रयोग किया जाता है जिससे इसमें
security बेहतर बन जाती है.
इसमें debugging करनी हो तो इसे आसानी से किया जा सकता है.

You might also like