Tuesday, 29 September 2015

Program to concatenate two strings and prove staring object is immutable (it also displays Hashcode)

/**
 *
 */

/**
 * @author U0123154
 *
 */
public class Stringcmp3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Data";
System.out.println("The hashcode of s1 is " +s1.hashCode());
String s2 = "Base";
System.out.println("The hashcode of s2 is " +s2.hashCode());
s1 = s1 + s2;
System.out.println("The hashcode of s1 is "+s1.hashCode());

System.out.println("The value of s1 is " +s1);

}

}


Output:-
The hashcode of s1 is 2122698
The hashcode of s2 is 2063089
The hashcode of s1 is 1853155771
The value of s1 is DataBase

No comments:

Post a Comment