1.2-New_and_Dot_operator

[!INDEX]

  1. employee class obj e1 initialisation + this + ct() + new + display mt()
  2. data type of number(4 type), decimal (2 type ), char, boolean, and object with string.
  3. create dog array and put 3 dog in it.
  4. Dot operator
  5. employee class 2 obj. initilize name + no ct+ no mt
  6. employee class 2 obj. initilize name + with mt() + emp detail mt()

1. employee class obj e1 initialisation + this + ct() + new + display mt()

public class Employee {
    String name;
    Employee(String name){
        this.name= name;
    }
    void empTask(){
        System.out.println(name+" Has done his Task");
    }
    public static void main(String[] args) {
        Employee e1 = new Employee("Rajat");
        e1.empTask();
    }
}

[!NOTE]

  1. एक instance को बनाता है |
  2. फिर मेमोरी allocate करता है heap में |
  3. फिर ct की सहायता से object initialized करता है जिसमे instance variables की default वैल्यू आ जाती है या यु कहे की field भी initialized हो जाते है |
  4. name, ct aur mt fields hai class ke
  5. new ek memory location create kareyga jisko e1 point karega aur usme field ki value default assing kar deyga

2. data type of number(4 type), decimal (2 type ), char, boolean, and object with string.

Primitive Data Types:
        int, short, byte, long = 0
        float, double = 0.0
        char = '\u0000' (null character) //**Unicode representation** 
        boolean = false
Reference Types:
    Objects (including Strings) = null

3. create dog array and put 3 dog in it.

// Creating an array of Dog objects with the new keyword
Dog[] dogs = new Dog[3];
dogs[0] = new Dog("Rex");
dogs[1] = new Dog("Max");
dogs[2] = new Dog("Bella");

[!NOTE]

  1. dogs[0] = new Dog("Rex");
  2. The new keyword creates a new Dog object in memory which holds "Rex" (the name).
  3. The reference (memory address) of this newly created Dog object is stored at dogs[0]. So dogs[0] points to the memory location where the "Rex" object is stored.
  4. Similarly:
  5. dogs -> [null, null, null] (Initially)
  6. dogs -> [ref to Rex, ref to Max, ref to Bella] (After object creation)
  7. or
  8. dogs[0] -> Memory location A (Rex)
  9. dogs[1] -> Memory location B (Max)
  10. dogs[2] -> Memory location C (Bella)

4. Dot operator

(dot) operator is used to access the ==members== (fields and methods) of the object or class.


5. employee class 2 obj. initilize name + no ct+ no mt

public class Employee {
    String name;
    public static void main(String[] args) {
        // Creating first instance of D_014Employee
        Employee e1 = new Employee();
        e1.name = "Rajat";
        // Creating second instance of D_014Employee
        Employee e2 = new Employee();
        e2.name = "Deepak";
        // Printing details of first employee (e1)
        System.out.println(e1.name);
        // Printing details of second employee (e2)
        System.out.println(e2.name);
    }
}

6. employee class 2 obj. initilize name + with mt() + empdetail mt()

public class Employee {
    String name;
    void empDetails(String name1){
        name = name1;
    }
    public static void main(String[] args) {
        Employee e1 = new Employee();
        e1.empDetails("Rajat");
        
        Employee e2 = new Employee();
        e2.empDetails("Deepak");

        System.out.println(e1.name);
        System.out.println(e2.name);
    }
}