Tuesday, July 30, 2024

Object Hashcode in Java

 

Object Hashcode in Java

Every object in Java has a unique address when it is created. This address can be retrieved using the hashCode() method.

 

int Hashcode():

·        This is a Predefined method in  java language.

·        It can be used to get address of object/ return address of object.

·        The return type of hashCode() is int.

Ex:

package OOPSBasics;     

public class Dog

{             

               // variables  --  Instance variables

               String name;

               float weight;

               String color;      

               public static void main(String[] args) {                   

                              //  create obj for Dog class and get address of obje d1

                              Dog d1 = new Dog();

                              int addressOfd1 =  d1.hashCode();

                              //                   123961122

                              // addressOfd1 =  123961122

                              System.out.println("addressOfd1="+addressOfd1);                                         

                              //  addressOfd1=123961122

                              //  create obj for Dog class and get address of obje d2

                              Dog d2 = new Dog();

                              int  addressOfd2 =  d2.hashCode();

                              //                     942731712

                              //

                              System.out.println("addressOfd2="+addressOfd2);

                             

                              //                                          addressOfd2= 942731712

               }

}

Final variable:

·        final is a predefined keyword in Java.

Declaring Final Variables:

Syntax:

      float pi =3.14f;

·        write final keyword  before data type -  final variable

                              //   can be used before data type

·        A final variable's value cannot be changed after it is assigned.

·        Must be initialized at the time of declaration or within a constructor

 

·        final k/w is applicable for variables -->   final float pi =3.14f;

·        for methods   -->  final M1()

                                                                                          {

                                                                                          }

                                              for classes   --> final class student

                                                                                          {

                                                                                         

                                                                                          }

                    

package OOPSBasics;

public class FinalVariable {

               // can we declare final instance variable ?

                              final float pi3= 3.14f;

              

//                                          final int pi4; //CE:  The blank final field pi2 may not have been initialized

//                                          pi4= 4.12f;

                                            

               // Declaration and Assignment must be in one line for final instance var

                             

               public static void main(String[] args)

               {

                              //Declare  'final' variable and  Pi = 3.14f

                              final float  pi = 3.14f;// final variable

                              // change final variable value pi =  5.34f

//                           pi = 5.34f;// CE:  dont change final variable value

                              // if we try to change final variable,   it throws CE - compile time error

                              // The final local variable pi cannot be assigned. It must be blank and not using a compound assignment

                              // display Pi variable value

                              System.out.println("pi="+ pi);

                              //                       pi=3.14

                              // FAQ how do u declare const variable ?

                              // FAQ What is final k/w ?                          

                              // can we declare final variable  and store value in 2 lines pi2 = 3.78f;?

                              final float  pi2 ;

                              pi2 = 3.74f;

                              System.out.println("pi2="+ pi2);

                              //                        

//                           pi2 = pi2 +1;// Error

                              //    3.74 + 1

                              //     4.74

                              //   pi2 = 4.74

                              //      if we try to change final variable value, it throws CE.

                              // //   CE :   as we are trying to changing final variable value

                              // display pi2 var                            

               }

}

o/p:

pi=3.14

pi2=3.74

 Declare final  method, final class --> Later

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...