Important Points to Note:
String is a Final class; i.e once created the value cannot be altered. Thus String
objects are called immutable(not changeable).
The Java Virtual Machine(JVM) creates a memory location especially for Strings
called String Constant Pool. That’s why String can be initialized without ‘new’
keyword.
String class falls under java.lang.String hierarchy. But there is no need to import this
class. Java platform provides them automatically.
String reference can be overridden but that does not delete the content; i.e., if
String h1 = "hello";
h1 = "hello"+"world";
then "hello" String does not get deleted. It just loses its handle.
Multiple references can be used for same String but it will occur in the same place;
i.e., if
String s1 = "hello";
String s2 = "hello";
String s3 = "hello";
then only one pool for String “hello” is created in the memory with 3 references
- s1,s2,s3
If a number is quoted in “ ” then it becomes a string, not a number anymore. That
means if
String S1 ="The number is: "+ "123"+"456";
System.out.println(S1);
then it will print: The number is: 123456
If the initialization is like this:
String S1 = "The number is: "+(123+456);
System.out.println(S1);
then it will print: The number is:579
That's all to Strings!
QUESTIONS
Q1 .Predict the output of the following Java program
snippets:
1. String x = new String("Computer");
String y = new String("Computer");
System.out.println(x.equals(y));
2. String x = "Computer";
String y = "Applications";
System.out.println(x.substring(1,5));
3. String S1 = "Computer";
String S2 = "COMPUTER WORLD";
String S3 = "Computer world";
String S4 = "computer world";
System.out.println(S1 + " equals "+ S2 + "
" + S1.equals(S2));
System.out.println(S1 + " equals "+ S3 + "
" + S1.equals(S3));
System.out.println(S1 + " equals "+ S4 + "
" + S1.equals(S4));
4. String str = "Information Technology";
int p;
p = str.indexOf('n');
System.out.println(p);
5. System.out.println ("DEDICATE".compareTo("DEVOTE"));
6. boolean p;
p = ("BLUEJ".length() > "bluej".length()) ? true: false;
7. String x = "Computer";
String y = "Applications";
System.out.println(y + x.substring(5,y.length()));
8. String str1 = "Information Technology";
String str2 = "information technology";
boolean p = str1.equalsIgnoreCase(str2);
System.out.println(“The result is “ + p);
9. String x = “Vision”;
String y = “2021”;
System.out.println(x + y);
10. String n1 = "46", n2 = "64";
int total = Integer.parseInt(n1) + Integer.parseInt(n2);
System.out.println("The sum of " + "46 "+ "and "+" 64" + "
is " + total);
11. String x="Computer";
System.out.println(x.indexOf(x.charAt(4)));
12. String n = "Computer Knowledge";
String m = "Computer Applications";
System.out.println(n.substring(0,8).concat(m.substring(9)));
System.out.println(n.endsWith("e"));
Q2. Write a program to input a sentence. Find and display the
following:
(i) Number of words present in the sentence
(ii) Number of letters present in the sentence.
Q3. Write a program in Java to accept a word/a String and
display the new string after removing all the vowels present in
it.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CMPTR PPLCTNS
Q4. Write a program in Java to accept a name (Containing three
words) and Display only the initials (i.e., first letter of each
word).
Sample Input: LAL KRISHNA ADVANI
Sample Output: L K A
Q5. Write a program in Java to accept a name containing three
words and display the surname first, followed by the first and
middle names.
Sample Input: MOHANDAS KARAMCHAND GANDHI
Sample Output: GANDHI MOHANDAS KARAMCHAND
Q6. Write a program in Java to enter a String/Sentence and
display the longest word and the length of the longest word
present in the String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN
BAGAN”
Sample Output: The longest word: FOOTBALL: The length of the
word: 8