Java Notes
Java Notes
Note that we don't use double quotes ("") inside println() to output numbers.
-----------------------------------------------------------------------------------
----------
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
-----------------------------------------------------------------------------------
-----------
type casting
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
-----------------------------------------------------------------------------------
----------------------------------------
Java String
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());
}
}
***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
}
}
___________________________________________________________________________________
________________________________
class JavaExample
{
public static void main(String args[])
{
//creating string using string literal
String s1 = "BeginnersBook";
String s2 = "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");
}
}
}_____________________________________________________________
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.
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
}
-----------------------------------------------------------------------------------
--------------------------------------
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);
}
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------
Array
****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);
}
}
----------------------------------------------------------------------------------
Overloading multiple methods can have the same name with different parameters:
Overloading eg:
_-----------------------------------------------------------------------------
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
/**
* @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);
}
-----------------------------------------------------------------------------------
-
/**
* @param args
*/
public static void main(String[] args) {
___________________________________________________________________________________
______
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);
}
}__________________________________________________________________________________
______________---
-----------------------------------------------------------------------------------
-----------------------------
Class Attributes
Another term for class attributes is fields.
public class Main {
int x;
** Class Method
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
-----------------------------------------------------------------------------------
--------
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 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
-----------------------------------------------------------------------------------
--------------------------------------
** 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;
-----------------------------------------------------------------------------------
---------------------------------------------------------------
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;
-----------------------------------------------------------------------------------
---------------------------------------------
**
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 Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Pig myPig = new Pig();
Dog myDog = new Dog();
-----------------------------------------------------------------------------------
-----------------------------------------
User Input
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
-----------------------------------------------------------------------------------
----------------------------------
* 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
}
}
(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
(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;
*
ArrayList for-each loop
import java.util.ArrayList;
-------------
import java.util.ArrayList;
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);
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");
2. Add last
cars.addLast("Mazda");
-----------------------------------------------------------------------------------
------------------------------------------------
* Java Exception
Syntax
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
*
* 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 किया जा सकता है:-
___________________________________________________________________________________
_______________________________________
Runnable interface को implement करके
class MultiDemo implements Runnable{
public void run(){
System.out.println("thread is running...");
}
___________________________________________________________________________________
____________________________________________________
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 का अर्थ है कि हम समान नाम के methods को different signatures के साथ declare करते है
क्योंकि हम अलग-अलग task को एक ही method name के साथ perform क्र सकते है।
Message Passing
Objects एक दूसरे को information (सूचना) send तथा receive करके आपस में communicate करते हैं.
objects उसी प्रकार message को send तथा receive करते है जिस प्रकार हम लोग करते हैं.
एक ऑब्जेक्ट के लिए message एक procedure (प्रक्रिया) के लिए request होता है और इसलिए receiving object में
एक method को invoke किया जाता है.
इसमें program का structure बहुत ही simple होता है जिससे complexity कम होती है.
हमें इसमें सिर्फ एक बार code को लिखने की जरूरत होती है और उसे हम बार-बार use कर सकते हैं.
यह data redundancy प्रदान करता है.
इसमें हम आसानी से code को maintain किया जा सकता है जिससे time की बचत होती है.
object-oriented programming में data hiding और abstarction का प्रयोग किया जाता है जिससे इसमें
security बेहतर बन जाती है.
इसमें debugging करनी हो तो इसे आसानी से किया जा सकता है.