Java String Functions Q&A: equals(), compareTo(), and Exception Handling, Schemes and Mind Maps of Computer science

A concise overview of essential string functions and concepts in java. It covers methods like equals(), compareto(), tolowercase(), touppercase(), charat(), and substring(), explaining their usage with examples. Additionally, it discusses exception handling and the differences between string and stringbuffer, offering insights into when to use each for efficient string manipulation. This resource is ideal for students learning java programming and seeking a quick reference guide to string operations and exception handling. (404 characters)

Typology: Schemes and Mind Maps

2024/2025

Available from 08/10/2025

priyanshu-saha-1
priyanshu-saha-1 🇮🇳

5 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Question 1
equals()
and
==
equals()
==
It
is
a
method.
It
is
used to
check
if
the contents of two strings are same or not.
It
is
a
relational
operator.
It
is
used to
check
if
two variables refer to the same object
in
memory
Question 2
compareTo()
and
equals()
compareTo()
It
compares
two
strings.The
result
is
a
negative,
positive
or
zero
integer
value
depending
on
whether
the
String
object
precedes,
follows
or
is
equal
to the
String
argument
equals()
It
checks
if
contents of two strings are same or not.The result
is
true
if
the contents are same otherwise
it
is
false.
Question 3
toLowerCase()
and
toUpperCase()
tolowerCase()
Converts
all
characters
of
the
String
object to
lower
case
Example:
String str =
"HELLO":
STRING FUNCTION
System.out.printin
(str.toLowerCase();
toUpperCase()
Converts
all
characters
of
the
String
object to
upper
case
Example:
String str = "hello":
System.out.println(str.toUpper
Case());
Output
of
this code snippet
will
be
HELLO.
pf3
pf4

Partial preview of the text

Download Java String Functions Q&A: equals(), compareTo(), and Exception Handling and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Question 1 equals() and ==

equals()

==

It is a method. It is used to check if the contents of two strings are same or not.

It is a relational operator. It is used to check if two variables refer to the same object in memory

Question 2 compareTo() and equals() compareTo()

It compares two strings.The result is a negative, positive or zero integer value depending on whether

the String object precedes, follows or is equal to the String argument equals() It checks if contents of two strings are same or not.The result is true if the contentsare same otherwise it is false.

Question 3

toLowerCase()and toUpperCase()

tolowerCase()

Converts all characters of the String object to lower case Example: String str = "HELLO":

STRING FUNCTION

System.out.printin (str.toLowerCase();

toUpperCase() Converts all characters of the String object to upper case Example: String str = "hello":

System.out.println(str.toUpper Case());

Output of this code snippet will be HELLO.

Question 4 charAt() and substring()

charAt()

It returns a character from the string at the index specified as its argument Itsreturn type is char

A function can only return a single value.

substring() It extracts a part of the string as specified by its arguments and returns the extracted part as a new

string.

Its return type is String.

Question 5 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 6

Useful String Functions: Function

length()

indexOf()

lastIndexOf()

Return Type int

int

int

Use

Tocount number of characters in aString

To calculate index/position of a character in a String

To calculate last occurrence of a character in a String

Example

String a="Hello"; int b=a.length();

Output: 5

String a="Hello"; int b=a.indexOf('e'); Output: 1

String a="Hello"; int b=a.lastindexOf(); Output: 3

compareTolgnoreCase() int

concat()

replace()

Question 7

String

String

Question 8

String

To return difference of ASCIl value of unmatched characters of two Strings if cases are not same.

Ans: java.lang

Tojoin two Strings without any space

To replace a character or String from a String with new one

Difference Between String and StringBuffer

String a="ABC"; String b="abc"

Which package is used for String in Java?

int c=a.compareTo(b);

Output:-

String a="ABC"; String b="abc" String c=a.concat(b) Output: ABCabc String a=" India is my country";

String is immutable, its content cannot StringBuffer is mutable, its content can be changed through its

be changed once created. append(), insert()etc methods.

String b=a.replace("my","our"); Output: India is our country

String operations like concat() create a StringBuffer operations are mutable and don't create new objects on

new String object. each^ operation.

StringBuffer

String is used when you don't need to StringBuffer is preferred when you need to modify the content

modify content frequently. frequently like building a String through multiple additions.