Module-6B
Playing with String (An special class) class
- Example of creating String class instance
String website = “HadoopExam.com”; //No new keyword here
- Now check below declaration’s (Yes, both below are different)
String website = “HadoopExam.com”;
String website = new String(“HadoopExam.com”);
String Concatenation: use numeric addition if two numbers are involved, use concatenation otherwise, and evaluate from left to right.
public class Welcome {
public static void main(String[] args) {
System.out.println(1 + 2); // 3
System.out.println("Hadoop" + "Exam"); // HadoopExam
System.out.println("Hadoop" + "Exam" + 100); // HadoopExam100
System.out.println(100 + 200 + "HadoopExam"); // 300HadoopExam
}
}
Immutable String: Once a String object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change one of the characters inside it.
- immutable classes in Java are final, and subclasses can’t add mutable behavior
public class Welcome {
public static void main(String[] args) {
String s1 = "Hadoop";
String s2 = s1.concat("Exam");
s2.concat(".com"); //Trying to add .com with website name
System.out.println(s2);
}
}
String Pool: Strings use a lot of memory. In some applications, they can use up 25–40 percent of the memory in the entire program. Java realizes that many strings repeat in the program and solves this issue by reusing common ones. The string pool, also known as the intern pool, is a location in the Java virtual machine (JVM) that collects all these strings.
- The string pool contains literal values that appear in your program.
- Now check below declaration’s (Yes, both below are different)
String website = “HadoopExam.com”; //Will go in pool, even literal website also go in pool.
String website1 = new String(“HadoopExam.com”); //It will not go in pool, it is similar to regular object. GC will happen on that.
String methods:
public class Welcome {
public static void main(String[] args) {
String website = "HadoopExam.com";
System.out.println(website.length());
System.out.println(website.charAt(0));
System.out.println(website.charAt(5));
//System.out.println(website.charAt(14)); //Exception
System.out.println(website.indexOf('o'));
System.out.println(website.indexOf('o', 5)); //next index position
System.out.println(website.indexOf('z')); //Not in string
}
}
public class Welcome {
public static void main(String[] args) {
String website = "HadoopExam.com";
System.out.println("Print 1 : " + website.substring(0, 3));
System.out.println("Print 2 : " + website.substring(3));
System.out.println("Print 3 : " + website.substring(3,3));
//System.out.println("Print 4 : " + website.substring(3,100));
System.out.println("Print 5 : " + website.toUpperCase());
System.out.println("Print 6 : " + website.toLowerCase());
}
}
public class Welcome {
public static void main(String[] args) {
String website = "HadoopExam.com";
System.out.println("Print 1 " + website.equals("HadoopExam.com"));
System.out.println("Print 2 " + "HadoopExam.com".equals(website));
System.out.println("Print 3 " + "HADOOPEXAM.com".equals(website));
System.out.println("Print 4 " + "HADOOPEXAM.com".equalsIgnoreCase(website));
System.out.println("Print 5 " + "HADOOPEXAM.com".startsWith("H"));
System.out.println("Print 6 " + "HADOOPEXAM.com".endsWith("com"));
System.out.println("Print 7 " + "HADOOPEXAM.com".endsWith("m"));
System.out.println("Print 8 " + "HADOOPEXAM.com".endsWith("M"));
System.out.println("Print 9 " + "HADOOPEXAM.com".contains("com"));
System.out.println("Print 10 " + "HADOOPEXAM.com".contains("HADOOP"));
System.out.println("Print 11 " + "HADOOPEXAM.com".replace("com" , "COM"));
System.out.println("Print 12 " + "HADOOPEXAM.com".replace("HADOOP", "TRAINING4"));
System.out.println("Print 13 " + "\t HADOOPEXAM.com \n");
System.out.println("Print 14 " + "HADOOPEXAM.com ".trim());
System.out.println("Print 15 " + "\t HADOOPEXAM.com \n".trim());
}
}
Method Chaining:
public class Welcome {
public static void main(String[] args) {
System.out.println("HADOOPEXAM.com".replace("com" , "COM").concat(" Hadoop Learning \n ".trim().concat("Resources")));
System.out.println("HADOOPEXAM.com".replace("com" , "COM").concat(" Hadoop Learning \n ".concat("Resources")));
}
}