Print Page Print Page

CH9 – Strings Are Immutable

  1. Create a new class named StringsImmutable.
  2. Create a main method and add the following code.
public class StringsImmutable{
  public static void main(String[] args){
    String name = "Alan Turing";
    System.out.println(name + " :" + name.hashCode());
    name = name.toUpperCase();
    System.out.println(name + " : " + name.hashCode());
  }
}
  1. Compile and run the program.
$ javac StringsImmutable.java 
$ java StringsImmutable
Alan Turing :-528410695
ALAN TURING : -791838791