Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Differentiate between the following

Question 1

equals() and ==

equals() ==

It is a method It is a relational operator

It is used to check if the It is used to check if two


contents of two strings variables refer to the same
are same or not object in memory

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() and equals()

compareTo() equals()

It checks if
It compares two strings contents of two
lexicographically. strings are same
or not.

The result is a negative, positive or The result is true


compareTo() equals()

zero integer value depending on if the contents


whether the String object precedes, are same
follows or is equal to the String otherwise it is
argument false.

Question 3

toLowerCase() and toUpperCase()

toLowerCase() toUpperCase()

Converts all characters of Converts all characters of


the String object to lower the String object to upper
case case

Example: Example:
String str = "HELLO"; String str = "hello";
System.out.println(str.toL System.out.println(str.toU
owerCase()); pperCase());

Output of this code Output of this code


snippet will be hello. snippet will be HELLO.

Question 4

charAt() and substring()

charAt() substring()

It extracts a part of the


It returns a character
string as specified by its
from the string at the
arguments and returns the
index specified as its
extracted part as a new
argument
string.

Its return type is char Its return type is String


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.

Write short answers

Question 1

What is exception? Name two exception handling blocks.

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()

It compares two strings lexicographically. Its return type is int.

Question 3

Write a statement for each to perform the following task on a string:

(i) Extract the second last character of a word stored in the variable wd.

char ch = wd.charAt(wd.length() - 2);


(ii) Check if the second character of a string str is in upper case.
boolean res = Character.isUpperCase(str.charAt(1));

You might also like