Answer:
The java.lang.RuntimeException is a sub–class of the
java.lang.Exception class. The RuntimeException and
its sub–classes are also known as unchecked exceptions. This is
because the compiler does
not force you to handle such
exceptions. So even if code that might potentially throw an
unchecked
exception is written, the compiler does not cause an
error. Some examples of RuntimeExceptions are
ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException.
131.
132.
CHAPTER
12
String Handling
What is a string literal? Give an example.
Answer:
A string literal is a set of characters enclosed in double quotes. It
can be assigned to a String variable with the help of the
assignment operator (=). The following
line of code shows that
the string literal “I’m a literal” is assigned to the string variable
called ‘name’.
String name = “I’m a literal”;
What will be the output of the following code? Explain
why.
class StringDemo {
public static void main (String strArgs[]) {
String strVar = “Welcome”;
strVar.concat(“ to Java”);
System.out.println(strVar);
}
}
Answer:
This code prints the following output:
Welcome
133.
134.
Java strings are immutable objects
so unless explicitly assigned,
the string’s value does not change. The method concat ()
simply appends the value
to Java to strVar but
does not assign
the appended String back to the variable strVar.
In order for the String Welcome to Java to be printed,
the code
needs to be modified as follows:
strVar = strVar.concat(“ to Java”);
Do'stlaringiz bilan baham: