Code Examples
package com.javaninja.stringexamples;
import java.util.HashMap;
public class StringExample3 {
//What are immutable objects and Why is String Immutable
public static void main (String args[])
{
String str1 = new String("Java");
System.out.println("str1 : "+str1);// Prints Java
str1.toUpperCase();
System.out.println("str1 : "+str1);// Prints Java
String str2= str1.toUpperCase();
System.out.println("str2 : "+str2);// Prints JAVA
Employee emp1 = new Employee(101, "Vatsal");
System.out.println("emp1 : "+emp1.getEmployeeCode()); // Prints 101
emp1.setEmployeeCode(102);
System.out.println("emp1 : "+emp1.getEmployeeCode()); // Prints 102
HashMap hm = new HashMap();
hm.put(str2, 1);
str2=str2.concat(" Ninja");
System.out.println(hm.get(str2));
}
}
package com.javaninja.stringexamples;
public class Employee {
private int employeeCode;
private String name;
Employee(int employeeCode,String name)
{
this.employeeCode = employeeCode;
this.name = name;
}
public int getEmployeeCode() {
return employeeCode;
}
public void setEmployeeCode(int employeeCode) {
this.employeeCode = employeeCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}