Class

  1. Class is a blueprint (or) model for creating the objects and it doesn’t exist physically.
  2. A class can contains properties and actions.
  3. Properties are represented by variables and actions are represented by methods.

Student

Object

  1. Object is an instance of a class.An object is any thing that exists physically.
  2. An object also contains properties and actions.

StudentObject

Example:

class Student{
 //properties-instance variables
 String name;
 int age;
 // actions-methods
 void talk(){
   System.out.println("Hello i am "+name);
   System.out.println("My age is "+age);
 }
}

Creating Objects for a class

Syntax:
classname objname=new classname();

Example:
Student s=new Student();

  1. Here s variable stores the references number of the object returned by JVM after creating the object.
  2. We know that class code along with method a code is stored in “method area” of the JVM ,When an object is created ,the memory is allocated on “heap”.
  3. After creation of an object ,JVM produces a unique reference number for the object from the memory address of the object,This reference number is also called “hashCode” number.
  4. To know the hashCode no(reference) of an object ,we can use hashCode() method of object class.

what is hashCode?

  1. hashCode is a unique identification number allocated to the objects by the JVM.This hashCode number is also called reference number Which is created based on the location of the object in the memory.
  2. How can you find the hashCode of an object?
    the hashCode() method of ‘Object’ class in java.lang package is useful to find the hashCode of an object

    class Demo{
    public static void main(String args[]){
      //create Student  class object:p(in the above)
      Student s=new Student ();
      System.out.println(s.hashCode());
      s.talk();
     }
    }
    /*
    Output:
    
    33219526
    Hello i am null
    My age is 0
    */
    

Note:

  1. By using hashCode() we can get hashCode number
  2. By using reference variable(s),we can refer instance variables and methods like s. name,s.age,s.talk()
  3. Since we didn’t initialize instance variables,Java compiler adds some additional code to Student class
    name=null
    age=0
    When JVM executes ,preceding code .it stores null into name and 0 into age

The default values of the instance variable are used by java compiler

Datatype Default value
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
char a space
String null
any class type null
boolean false