IT Level-04 CBSE-Part01-Java

Java Notes

Subject :- 802 (IT)

1.     What is Java ?

 

  • जावा एक हाई लेवल की प्रोग्रामिंग भाषा है जिसका इस्तेमाल सॉफ्टवेयर और एप्लीकेशन को बनाने के लिए किया जाता है.
  • दुसरे शब्दों में कहें तो, जावा एक object-oriented प्रोग्रामिंग भाषा है जिसका इस्तेमाल मोबाइल एप्लीकेशन और वेब एप्लीकेशन को बनाने के लिए किया जाता है.
  • जावा को James Gosling (जेम्स गोसलिंग) ने 1995 में विकसित किया था, इसलिए जेम्स गोसलिंग को जावा का पिता भी कहा जाता है.

 

2.     जावा की प्रमुख विशेषताएँ क्या हैं?

 

1- Simple (सरल)

जावा एक सरल प्रोग्रामिंग लैंग्वेज है जिसे कोई भी व्यक्ति बड़ी आसानी से सीख सकता है। इस भाषा का सिंटेक्स काफी सरल होता है जिसकी वजह से इसे समझना काफी आसान होता है।

2- Secure (सुरक्षित)

जावा एक सुरक्षित भाषा है क्योंकि जावा के प्रोग्राम JRE (जावा रनटाइम एनवायरनमेंट) में रन होते है. इसके साथ साथ इसमें encryption का इस्तेमाल किया जाता है जिसके कारण कोई भी हैकर इसे hack नहीं कर पाता.

जावा में कोई भी वायरस आसानी से प्रवेश नहीं कर सकता जिसकी वजह से यूजर का डेटा पूरी तरह से सुरक्षित रहता है।

3- Object-Oriented (ऑब्जेक्ट ओरिएंटेड)

जावा एक ऑब्जेक्ट ओरिएंटेड भाषा है इसलिए इसमें class और objects का उपयोग करके प्रोग्राम को लिखा जाता है.

4- Portable (पोर्टेबल)

जावा एक portable लैंग्वेज है क्योंकि हम इसके bytecode को किसी भी कंप्यूटर और डिवाइस में run कर सकते हैं.

5- High-performance (उच्च प्रदर्शन)

अन्य प्रोग्रामिंग भाषाओँ की तुलना में जावा की परफॉरमेंस काफी अच्छी होती है। यह एक तेज भाषा है जो तेज गति से सभी कार्यो को करती है। हालांकि यह अभी भी C और C ++ की तुलना में थोड़ी धीमी है।

6- Robust (मज़बूत)

जावा में garbage collection अपने आप हो जाता है और इसमें मेमोरी एलोकेशन बहुत बढ़िया है। जावा में जो भी errors (त्रुटियाँ) आती है उन्हें आसानी से हल किया जा सकता है। इन्हीं सभी कारणों से जावा Robust लैंग्वेज है।

7- Multi-Threading (मल्टी-थ्रेडिंग)

जावा में multi threading का इस्तेमाल किया जाता है जिसके कारण जावा के बड़े program को छोटे programs में विभाजित किया जा सकता है और फिर इन छोटे programs को एक एक करके execute किया जाता है.

8- Dynamic (डायनामिक)

जावा एक डायनामिक लैंग्वेज है क्योंकि इसमें हम variable को run time में allocate कर सकते हैं.

Interpreted / Compiled

Java language को compiled और interpreted दोनों तरह की programming language consider किया जाता है , क्योंकि किसी भी Java program को run करने के लिए आपको उस program को compile करके binary byte-code में convert करना पड़ेगा। फिर इस byte code को JVM (Java Virtual Machine ) द्वारा run किया जाता है जो कि एक software-based interpreter है।

Java Program Structure

Java program के लिए हमेशा class name के जैसे ही file name भी रखते हैं। means जो class का name होगा वही file का name भी होगा। और file extension .java के साथ save करते हैं।

 programming language C , C++ की तरह ही java program का entry point main() method होता है। जैसा कि नीचे example में आप देख सकते हैं।

public class Test {

  public static void main(String[] args) {

    System.out.println("Hello World");

  }

}

 

 

Explain :

·        तो जैसा कि आपने ऊपर पढ़ा कि java program का entry point main() method होता है , हम जो implementation चाहते हैं उसके लिए पूरा code main() method में ही लिखते हैं।

 

·        class name के first letter को हमेशा uppercase में लिखते हैं। और एक से ज्यादा words होने पर उसे camel case में लिखते हैं जैसे : UserAddress

 

·        ध्यान रहे कि Java case-sensitive language है , तो Test और test दोनों अलग अलग हैं।

 

·        Program की class name और file name same होना चाहिए।

Java main Method

main() method को आप हर जावा program में देखेंगे। main() method के अंदर जो भी code होगा वो execute हो जयगा जब भी program run होगा।

public static void main(String[] args)

method को आगे जो keywords use किये गए हैं , उन्हें आगे deeply अच्छे से पढ़ेंगे।

Java System.out.println ()

System एक predefined class है जिसमे कई useful members और properties हैं जैसे out . इसकी full form output है। और println () एक method है जिसका use किसी भी text को print करने के लिए किया जाता है। इसकी full form print line है।

Important

Normally हम file name और class name same रखते हैं ,लेकिन ऐसा नहीं कि आप file का नाम class name से different रख सकते हैं।

Actually java program को  compile करने के लिए file name का use किया जाता है , जबकि complied code को execute कराने के लिए file में present main class का name use किया जाता है।


Example -

class A

{

   public static void main(String args[])

   {

      System.out.println("Class A running");

   }

}

 

Output

javac Test.java

java A

Class A running

Example में आप देख सकते हैं कि , code को compile करने के लिए filename use किया गया है और compiled code execute करने के लिए class name use किया गया है।

ध्यान रहे कि अगर file का नाम class के name से different है तो वह class public नहीं होनी चाहिए नहीं तो compiler error देगा।

Java Comments

Comments program में line of code होता है जो कि execute नहीं होता है। Comments को code को explain करने के लिए use किया जाता है , जिससे कि जब दुबारा हम उस program पर काम करे तो हर step हमें समझ सके कि हमने इसमें किया क्या था।

Types of comments in Java

Java में Comments दो तरह के होते है -

  1. Single Line Comments
  2. Multi Line Comments

Java Single Line Comment

Java में single line comment को double slashes // की help से लिखा जाता है , जैसा कि आप नीचे example में देख सकते हैं

class HelloWorld

{

   // this is single line comment.

   public static void main(String[] args)

   {

      // print hellow world.

      System.out.println("Hello World!");

   }

}

Java Multi Line Comment

multi line comment लिखने के लिए slash asterisk /* से start करते हैं और asterisk slash */ से end करते है।

File : HelloWorld.java

class HelloWorld

{

   /**

   * this is multi line

   * comment

   */

   public static void main(String[] args)

   {

      System.out.println("Hello World!");

   }

}

 

 

 

Output

javac HelloWorld

java HelloWorld

Hello World!


Java Advantages of Using Comments

  • किसी भी project पर काम करने के बाद हमें जरूरत के हिसाब से समय के साथ उसमे changes करने पढ़ ही जाते हैं तो जब हम update करें तो हमें proper पता चले कि हमने किया क्या था।
  • या जब कोई दूसरा programmer हमारे द्वारा develop किये गए project को देखे या update करे तो उसे भी पता चले।
  • Comments लिखना आपकी  Coding Standardization को भी बताता है।
  • Comments लिखने से Coding Readability भी बढ़ती है।

Variables In Java

variable किसी memory को दिया गया नाम है जो कि किसी value को Hold करती है या store करती है। मतलब जब हम Variable define कर रहें है तो हम memory में value को store करके उसे नाम दे रहे होते हैं। इसलिए variables को named storage भी कहते हैं।

Java define a variable

Java में variables को उनके data types के साथ define किया जाता है , मतलब आपको initialize करते समय यह बताना पड़ता है , कि variables किस type की value store करेगा। Java में variables को उनके data types के साथ define किया जाता है , जैसा कि आप example में देख सकते हैं।

class Variables {    public static void main(String[] args)  

   {   

    // define integer variable.   

    int age = 25;   

    // defien string variable.

    String name = "Rahul Kumar";

    System.out.println("Name : "+ name);

    System.out.println("Age : "+ age);

  }

}

तो कुछ इस तरह से Java में variables को define किया जाता है। हालाँकि data types और भी कई तरह के होते हैं जिन्हे आप आगे detail में पढ़ेंगे।

normally define किये गए सभी variables की value को अपनी need के according change / update कर सकते हैं।

int age=90;age = 65;//you can change variable value any timeage = 89;

v Java में variables case sensitive होते हैं , मतलब same name के साथ lower और upper case में define किये गए variables अलग होंगे।

 

class Variables {      public static void main(String[] args)  

    {   

       String name = "variable : name";   

       // now define same vairable with different case.   

        String Name = "variable : Name";

       System.out.println(name);

       System.out.println(Name);

  }

}

Java declare multiple variables

आप same data type के variables को अलग अलग initialize करने के बजाय उन्हें एक साथ भी define कर सकते हैं।

class Variables
{ 

  public static void main(String[] args)  

  {   

    // you can define multiple variables like this.   

    int x = 51, y = 16, z = 50;   

    System.out.println(x + y + z);

  }

}

Java One Value to Multiple Variables

आप equals = operator का use करके किसी value को same type के variable में भी assign कर सकते हैं।

class Variables { 
    public static void main(String[] args)   {   

      // define multiple variables.   

      int x, y, z;   

      // assign same value to all variables.

      x = y = z = 10;

      System.out.println(x + y + z);

  }

}

Important about variable

1. आप same variable को एक से ज्यादा बार define नहीं कर सकते हैं

Type

Keyword

Size

Description

Boolean

boolean

1 bit

Boolean में सिर्फ दो values true , false होती हैं।

Character

char

2 bytes

यह single character store करता है।

Integer

int

4 bytes

यह बिना decimal points वाले whole numbers (-2,147,483,648 to 2,147,483,647) को store करता है।

Floating point

float

4 bytes

यह fractional numbers को point के बाद 7 digits तक store करता है।

Double floating point

double

8 bytes

यह भी fractional numbers को point के बाद 15 digits तक store करता है।

Long Integer

long

8 bytes

यह भी integer की तरह बिना decimal points वाले whole numbers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) को store करता है।

Short Integer

short

2 bytes

short integer whole numbers को -32,768 से 32,767 तक store करता है।

Byte

byte

1 byte

byte Integer , whole numbers को -128 से तक store करता है।

2. ध्यान रहे कि data type के according ही variable में value store होनी चाहिए , ऐसा नहीं हो सकता है कि int variable में string value assign करें। और char type के variable में boolean . हालाँकि ऐसा करने पर syntax error मिलेगी, या हो सकता है कि variable में assign की value proper नहीं मिलेगी।

Java Rules to define variables

  1. variable name सिर्फ letters, digits, underscores, और dollar signs के साथ define कर सकते हैं।
  2. variable name किसी letter से ही start होना चाहिए।
  3. variable name को हमेशा lowercase letter से start करना चाहिए और white space नहीं होने चाहिए।
  4. जैसा कि आपने ऊपर example देखा कि variable name case sensitive होते हैं , तो इस बात का ध्यान रखें।
  5. Java language में define कोई भी reserved keyword (int or boolean), variable name नहीं हो सकते हैं।

Data Types of Variables

Data Type का simply मतलब है किसी variable का type, जिससे हमें पता चल सके कि कोई variable किस type की value hold किये हुए है , या in future किस type की value hold करेगा।

पिछले topic में आपने Variables और final Variables के बारे में पढ़ा और समझा , आपको याद होगा कि कोई भी variable define करने से पहले हमें उसका data type define करते थे जिससे पता लगाया जा सके कि वो variable किस type की value hold करेगा।

 

Java Primitive Data Types

Java में 8 basic data types define किये गए हैं जो कि इस प्रकार हैं

 

public class DataType

{

  public static void main(String[] args)

  {

     int int_var = 23;

     boolean bool_var = true;

     char char_var = 'A';

     float float_var = 34.45f;

     byte byte_var = 125;

    

     // now print them.

     System.out.println("Integer value : "+ int_var);

     System.out.println("Boolean value : "+ bool_var);

     System.out.println("Character value : "+ char_var);

     System.out.println("Float value : "+ float_var);

     System.out.println("Byte value : "+ byte_var);

  }

}

Output

 

javac DataType.java

java DataType

Integer value : 23

Boolean value : true

Character value : A

Float value : 34.45

Byte value : 125

 

 

 

 

Java character

char data type का use किसी single character को store करने के लिए use किया जाता है। जैसे 'A' or 'b'

char grade = 'A';

ध्यान रहे variable में store की जाने वाली value हमेशा single quotes '' में ही होनी चाहिए double quotes में नहीं।

Java Boolean

boolean data type को boolean keyword के साथ define किया जाता है , boolean data type की सिर्फ दो values ही होती हैं true , false .

 

public class BooleanType

{

  public static void main(String[] args)

  {

    boolean isJavaEasy = true;

    boolean isJavaHard = false;

    System.out.println(isJavaEasy);

    System.out.println(isJavaHard);

  }

}

Output

javac BooleanType.java

java BooleanType

true

false

ध्यान रहे boolean values सिर्फ true & false ही हैं , उन्हें आप True / False या trUe / falsE नहीं लिख सकते हैं।

String In Java

 

String series of characters या group of characters होती है , Java में string define करने के लिए double quotes " " का use किया जाता है।

Java में string define करने के लिए String class का use किया जाता है।

 

class StringTest

{

  public static void main(String[] args)

  {

    String name = "SachinSaini";

    System.out.println(name);

  }

}

Output

javac StringTest.java

java StringTest

SachinSaini

ध्यान रहे string हमेशा double quotes में ही define करें , Java में string को single quotes के साथ define नहीं कर सकते हैं।

 

Java String length

तो जैसे कि आपने ऊपर पढ़ा कि Java में String एक class है जिसका use string manipulation के लिए जाता है। इनमे से length() method का use string length पता करने के लिए किया जाता है।

 

 

 

class StringTest

{

  public static void main(String[] args)

  {

    String name = "SachinSaini";

    System.out.println("SachinSaini have " + name.length() + " letters");

  }

}

Output

javac StringTest.java

java StringTest

SachinSaini have 14 letters

 

Operators In java

Operator एक symbol होते हैं , जो process / operation represent करते हैं करते हैं , या हम कह सकते हैं की Operators को कोई process / या operation को perform करने के लिए use करते हैं।

class Operators

{

  public static void main(String[] args)

  {

    int a = 7;

    int b = 89;

    // here = is a assignment operator.

    System.out.println(a+b);

    // here + is arithmetic operator.

  }

}

Output   96

 

Java Operators Types

इसी तरह से Java में different - different action perform करने के लिए different - different Operators है Java में normally use होने वाले Operators कुछ इस प्रकार हैं

 

  1. Arithmetic Operators

 

  1. Assignment Operators

 

  1. Comparison / Relational Operators

 

  1. Logical Operators

 

  1. Increment / Decrement Operator

 

 

Operator

Name

Example

Explanation

+

Addition

x+y

( + ) plus operator uses to add two or more numbers / values

  -

Subtraction

x-y

 ( -  ) minus  operator uses to substract one  numbers / values from another numbers / values or finds the difference 

   /

Division

x / y

quotient of x and y with decimal point.

  *

multiplication

x * y

product of x and y

%

Modulus

x % y

remainder of operands

 

Java Assignment Operators

 

Assignment Operator को equals to = से represent करते हैं , जो कि value को किसी variable में assign करने के लिए use किया जाता है।

हालाँकि इसे Arithmetic Operators के साथ भी use करते हैं। नीचे दिए गए Example में आप देख सकते हैं।

 

 

 

 

 

 

Operator

Name

Example

Explanation

=

Assign

x = y

value of y assigned to x

!=

Assign

x = y

value of y assigned to x

+=

Addition then Assign

x += y

First add and assign it to x. (It is same as x = x+y )

- =

Subtract then Assign

x -= y

get the difference and assign it to x. (It is same as x = x-y )

/ =

Divide then assign quotient

x /= y

quotient of x and y then assign it to x . (It is same as x = x/y )

* =

Multiplication then assign the product

x * =y

product of x and y then assign it to x. (It is same as x = x*y )

% =

Divide then assign remainder

x %= y

remainder of operands then assign it to x. (It is same as x = x+%y )

 

class AssignmentOperator

{

  public static void main(String[] args)

  {

    // add then assign.

    int a = 90;

    a += 10;

    System.out.println(a);

   

    // devide 100 by 2 and assign.

    a /= 2;

    System.out.println("Quotient : "+  a);

   

    // get module deviding 50 by 11 then assign.

    a %= 11;

    System.out.println("Modulas : "+  a);

  }

}

Output

100

quotient : 50

Modulas : 6

 

Java Incrementing-Decrementing Operators

 

Incrementing-Decrementing Operators को किसी variable को 1 से
increase या decrease करने के लिए use किया जाता है। हालाँकि इसमें Addition और Subtraction operation ही होते हैं , इसलिए इसमें ( ++ ) या ( -- ) sign ही use होते हैं।  नीचे table में आप देख सकते हैं किसा तरह से इन्हे use करते हैं और क्या Output generate हैं।

Operator

Name

Explanation

++a

Pre Increment

first increment by 1 then return the value of a

a++

post Increment

first return the value of a then increment by 1.

--a

Pre Decrement

first decrement by 1 then return the value of a

a--

Post Decrement

first return the value of a then decrement by 1.

class IncDecOperator

{

  public static void main(String[] args)

  {

    int a = 90;

    System.out.println("Initial value : "+a);

    // increment by 1

    a++;

    System.out.println("After increment : "+a);

   

    // decrement by 1

    a--;

    System.out.println("After decrement : "+a);

  }

}

Output

Initial value : 90

After increment : 91

After decrement : 90

 

 

Post a Comment

0 Comments