You are on page 1of 5

Array : Done

Strings :

                String  str = "NAME";

                It will search for an string object with data NAME is string pool.

                If available, str refers to that object otherwise, it creates a new object with data
NAME and refers to it.

                String str1 = "SIKANDER";

        String str2 = "SIKANDER";

                      

        System.out.println(str1.hashCode());       

        System.out.println(str2.hashCode());

                //Same  hash code.

------------------

                String str1 = new String("SIKANDER");

        String str2 = new String("SIKANDER");

                      

        System.out.println(str1.hashCode());       

        System.out.println(str2.hashCode());

                //Same  hash code.       

--------------------

                To compare string always use equals method

                String str1 = new String("SIKANDER");


        String str2 = new String("SIKANDER");

        if(str1.equals(str2) == true)

        {

                }

------------------

  Rectangle  r1 = new Rectangle();

            Rectangle  r2 = new Rectangle();

           

            r1.length = 4;

            r1.breadth = 4;

           

            r2.length = 4;

            r2.breadth = 4;

           

            if(r1.equals(r2) == true)

            {

                System.out.println("Equal");

            }

            else

                System.out.println("Not Equal");

Output : Not Equal

                    if(r1 == r2)


            {

                System.out.println("Equal");

            }

            else

                System.out.println("Not Equal");

Output : Not Equal

---------------

Object class provides equals method. This method only checks for references.

If you want to check for data, we can override the equals method and provide our own
implementation.

 boolean equals(Object r)

return (this == rhs) ? true : false;

overriden equals method in Rectangle class

boolean equals(Rectangle r)

        if(r.length == length && r.breadth == breadth)

                return true;

                return false;

}
 

Strings are immutable objects. Once a string object is creates, it cannot be modified.

                str.toLowerCase();

                It takes the string object (str), creates a new string with lowercase data of str and
returns a new string (does not modify str).

---------------------

  String str1 = new String("SIKANDER");

        String str2 = new String("CRANES");

       

        str1.concat(str2);

       

        System.out.println(str1);       

        System.out.println(str2);

Does not modify str1 OR str2. It creates a new string with data of str1 and str2 and returns
that string.

                str1 = str1.concat(str2);

str1 refers to newly created object.

str1.indexOf(String );
 

str2 = str1.substring(start , end);

                                                0 , 3

It includes characters from index 1 - 2(0,1,2)

You might also like