Professional Documents
Culture Documents
Differentiate Between The Following: Equals and
Differentiate Between The Following: Equals and
Question 1
equals() and ==
equals() ==
Example: Example:
String s1 = new String s1 = new
String("hello"); String("hello");
String s2 = new String s2 = new
String("hello"); String("hello");
boolean res = boolean res = s1 == s2;
s1.equals(s2); System.out.println(res);
System.out.println(res);
The output of this code
The output of this code snippet is false as s1 and
snippet is true as contents s2 point to different String
of s1 and s2 are the same. objects.
Question 2
compareTo() equals()
It checks if
It compares two strings contents of two
lexicographically. strings are same
or not.
Question 3
toLowerCase() toUpperCase()
Example: Example:
String str = "HELLO"; String str = "hello";
System.out.println(str.toL System.out.println(str.toU
owerCase()); pperCase());
Question 4
charAt() substring()
Example:
Example:
String str = "Hello";
String str = "Hello";
String subStr =
char ch = str.charAt(1);
str.substring(1);
System.out.println(ch);
System.out.println(subStr);
Output of this code
Output of this code snippet
snippet is e.
is ello.
Question 1
An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions. Two exception handling blocks are try and
catch.
Question 2
State the purpose and return data type of the following String functions:
(a) indexOf()
It returns the index within the string of the first occurrence of the specified character or -1
if the character is not present. Its return type is int.
(b) compareTo()
Question 3
(i) Extract the second last character of a word stored in the variable wd.