Thursday, 27 May 2021

Java

 Java program

1. local variable  program 

//local variable

class Ram

{

public void student_age()

{

int age=0; //local variable

age=age+5;

//defined in block can't use outside block

}

public static void main(String args[])

{

//using local variable outside scope


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

}

}

output : 

Program generate error

  • प्रत्येक  object के  पास  instance variable  की खुद  की  copy होती  है  जब  की  class या  static variable की  same  copy सभी  object  के  pass होती  है 
  • instance variable का use करके  एक  object के  variable मे बदलाव  अन्य  objects मे  reflect (प्रतिबिंबित) नहीं  होता 
  • 2.class variable program

    // class variable

    class Ram

    {

    static int count; // class variable

    public static void main(String args[])

    {

    Ram obj1=new Ram(); // obj1 is created for class Ram

    Ram obj2=new Ram();     // obj2 is created for class Ram

    obj1.count=15;

    obj2.count=15;

    obj1.count++; //change in count of object 1 

    System.out.println(obj1.count);

    System.out.println(obj2.count);

    }

    }

    output : 

    16

    16

    3. constructor program

    // constructor 

    class person

    {

    private 

    String name;

             person() // this is constructor

            {

        name="raj"; // initialize

             }

            void show()

             {

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

            }

    }

    class Ram

    {

    public static void main(String args[])

    {

    person obj=new person();// constructor ko call nhi krna automatic call when object is created 

    obj.show();

    }

    }

    Output : name is raj



    4. instance variable

    //instance variable

    class marks

    {

    int english,math;// instance variable inside class but outside function

    }

    class Ram

    {

    public static void main(String args[])

    {

    marks obj1=new marks();


    obj1.english=20; //object ke through value initli

    obj1.math=30; //object ke through value initli


    marks obj2=new marks();


    obj2.english=50; //object ke through value initli

    obj2.math=80; //object ke through value initli


    System.out.println("first object : "+obj1.english);

    System.out.println("first object : "+obj1.math);


    System.out.println("second  object : "+obj2.english);

    System.out.println("second object : "+obj2.math);

    }

    }

    output :

    first object : 20

    first object : 30

    second object : 50

    second object : 80


    5.java string 

    • string is  a sequence of character but in java string is an object that represent //sequence of charcters.
    • java.lang.string class का  उपयोग  string object बनाने  के लिए  करते  है  


    //Java string

    class Ram

    {

    public static void main(String args[])

    {

    String[] sub={"java","sql","c++","c"};

    System.out.println(sub[0]);

    System.out.println(sub.length);

             //sub is object , // length एक function है object के साथ function लगा कर use किया है 

    }

    }

    output :

    Java 

    4

    6.java string 

    class Ram

    {

    public static void main(String args[])

    {

    String str="hellow, this is sample program";

    char p[]=new char[20];

    str.getChars(7,20,p,0); //obj.fx(paramtr)

    //7- starting  position 

    //20- ending position 

    //p - destination - जहा  भेजना   है 

    //0 - destination starting point

    //getChars एक  method है  जो  string  मे  से  charcter copy कर  लेता  है 

    System.out.println(p);

    }

    }

    output:

    this is samp

    7. java string

    class Ram

    {

    public static void main(String args[])

    {

    String str="hellow, this is sample program";

    System.out.println(str.toUpperCase());

    System.out.println(str.toLowerCase());

    System.out.println(str.indexOf("this"));

    }

    }

    output:

    HELLOW, THIS IS SAMPLE PROGRAM

    hellow, this is sample program


    suppose string s1="welcome" or string s2="welcome" , इस  case मे  केवल  एक  object create  होगा 

    JVM एक  string constant pool   होता  है  उसके  अंदर  welcome को  रख  लेते  है , पहली  बार  जब  s1 बनाते  है   तो  उसमे  welcome है  जब  दोबारा  s2 create किया  जाता  है  तो   नया  object बनाने  की  जगह  पहले  वाले  का  refference return करते  है  .string जो  object होते  है  वो  special memory area मे  store होते  है  जिसे  string constant pool कहते  है  


    StringBuffer भी  string की  तरह  ही  होता  है  लेकिन  उसे  modify किया  जा  सकता  है  इसे  StringBuffer कहते  है  . जो  string होता  है  वो  immutable होता  है  can't be modified.जब  की  string buffer mutable होता  है।  

    INHERITENCE

    //extend keyword use in java for inheritence

    //multiple inheritence is not supported java through class. when one class inherit multiple classes it is known as multiple inheritence.

    //जब  किसी  class के  एक  से  ज्यादा  parent   हो  same level पर

    //for ex : there is an three classes A,B,C ---- C class inherit A&B Class. if A&B have the same method now you can call it from child class object 

    // there will be ambiguity to call the method of A&B. <<--why java not support multiple inheritence?

    8. Inheritence 

    // work in inheritence 

    class doctor

    {

    void ddetail()

    {

    System.out.println("this is doctor detail");

    }

    }

    class surgun extends doctor

    {

    void sdetail()

    {

    System.out.println("this is surgun detail");

    }

    }

    class Ram

    {

    public static void main(String args[])

    {

    surgun obj = new surgun();

    obj.ddetail();

    obj.sdetail();

    }

    }

    output : 

    this is doctor detail

    this is surgun detail

    9. multi Inheritence 

    class doctor

    {

    void ddetail()

    {

    System.out.println("this is doctor detail");

    }

    }

    class surgun extends doctor

    {

    void sdetail()

    {

    System.out.println("this is surgun detail");

    }

    }

    class phy extends surgun

    {

    void pdetail()

    {

    System.out.println("this is phy detail");

    }

    }

    class Ram

    {

    public static void main(String args[])

    {

    phy obj = new phy();

    obj.ddetail();

    obj.sdetail();

    obj.pdetail();

    }

    }

    output :

    this is doctor detail

    this is surgun detail

    this is phy detail

    OVERRIDDING METHOD

    • जब  same name का  function base class और  derived class दोनों  मे  available हो  और  function का  signature //(function type, parameter)same हो  तो  ऐसे  situation को  function overriding कहते  है 
    • class B मे  show function है  उसे  overridden हुआ  है  ,  class A का show function hidden रहा  है 

    //question - difference b/w overloading and overridding ?

    10. overridding program

    //method overriding

    class A

    {

    void show()

    {

    System.out.println("this is doctor detail");

    }

    }

    class B extends A

    {

    void show()

    {

    System.out.println("this is surgun detail");

    }

    }

    class Ram

    {

    public static void main(String args[])

    {

    B obj = new B();

    obj.show();

    }

    }

    output : 

    this is surgun detail

    VECTOR CLASS 

    • //vector class - vector class dynamic array की  तरह  होता  है  जो  अपने  size को  shrink/grow कर  सकता  है  .
    • //इसकी  default capicity 10 होती  है  इसका  मतलब  तब  vector resize होगा  जब  11 element vector मे  शामिल  होगा   फिर  इसकी  capicity 20 हो  जाएगी 

    11. vector class 

    // vector class

    import java.util.*;

    class Ram

    {

    public static void main(String args[])

    {

    Vector<String> v=new Vector<String>();

    v.add("Tiger");

    v.add("Lion");

    System.out.println(v);

    }

    }

    output:

    [Tiger,Lion]

    12. vector class - integer

    import java.util.*;

    class Ram

    {

    public static void main(String args[])

    {

    int n=5;

    Vector<Integer> v=new Vector<Integer>(n);

    for(int i=0;i<n;i++)

    v.add(i);

    System.out.println(v);

    v.remove(3);

    System.out.println(v);

    }

    }

    output :

    1 2 3 4 5

    1 2 4 5 


    WRAPPER CLASS

    •  wrapper class - java मे  जो  wrapper class होती  है  वो  primitive data type(int , float) को  object के  रूप  मे  काम  लेने  के  लिए  करते  है  इसके  throgh premitive type को  object मे  बदला  जा  सकता  है  और  vice versa भी ये  java.util package मे  available होती  है। 
    • primitive type object नहीं  होते  है इसलिए  वो  किसी  class से  सम्बन्ध  नहीं  रखते। 


    //primitive type -> wrapper classes       - example 

    // boolean ->  Boolean,char -> Character , int -> Integer , float-> Float, double -> Double


    13. Wrapper class 

    // program : primitive type convert into object type 

    import java.util.*;

    class Ram

    {

    public static void main(String args[])

    {

    int num=100; // ये  primitive type है  

    Integer p=Integer.valueOf(num); //num को  object मे  बदल  रहा  है  

                  // num int है = से इसे भेजा जा रहा है , obj मे , obj की type class है 

    //char p = 5  -- ये  लाइन  गलत  है  लेकिन  ये  देखने  के  लिए  की  int 5 को  char p मे  भेजा  जा  रहा  है | 

    System.out.println(p);

    }

    }

    output :

    100

    14. wrapper class 

    // program : object convert into  primitive type 

    import java.util.*;

    class Ram

    {

    public static void main(String args[])

    {

    Integer obj= new Integer(100); // class है  , class का  variable है [object भी ] , 

    int num=obj.intValue(); // object के  साथ  (.)dot लगा  के function की  call करी  है              //जो  आया  उसे  num को  दिया  

    System.out.println(num);

    }

    }

    output : 

    100

    No comments:

    Post a Comment