Wednesday, 11 May 2016

Java Tutorial : Java StringBuilder - String vs StringBuilder(HashCode)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=BhehDFtT6NQ&list=UUhwKlOVR041tngjerWxVccw

HashCodeTest.java
public class HashCodeTest
{

    public static void main(String[] args)
    {

        System.out.println("Hashcode test of String:");
        
        /*
         * String is immutable(once created, can not be
         * modified).
         */
        String str = "Welcome";
        System.out.println("Before Concat,HashCode = "
                + str.hashCode());
        str = str + "Peter";
        System.out.println("After Concat,HashCode  = "
                + str.hashCode());

        System.out.println("---------------------------------");

        System.out.println("Hashcode test of StringBuilder:");

        /*
         * StringBuilder is mutable (once created, can be
         * modified.
         */
        StringBuilder sb = new StringBuilder("Welcome");
        System.out.println("Before Concat,HashCode = "
                + sb.hashCode());
        sb.append("Peter");
        System.out.println("After Concat,HashCode  = "
                + sb.hashCode());

    }
}
Output
Hashcode test of String:
Before Concat,HashCode = -1397214398
After Concat,HashCode  = 44468202
---------------------------------
Hashcode test of StringBuilder:
Before Concat,HashCode = 2114664380
After Concat,HashCode  = 2114664380
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/StringBuilderDemo_Hashcode_diff_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StringBuilderDemo_Hashcode_diff_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/c539eeaf4c6ff331d8e2aa2333826ea70c999093/BasicJava/StringBuilderDemo_Hashcode_diff_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment