Questions for: Java Lang Class
What will be the output of the program?
System.out.println(Math.sqrt(-4D));
It is not possible in regular mathematics to get a value for the square-root of a negative number therefore a NaN will be returned because the code is valid.
public class NFE
{
public static void main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5"); /* Line 8 */
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("bad number");
}
}
}
All of this code is legal, and line 8 creates a new String with a value of "42.5". Lines 9 and 10 convert the String to a double and then back again. Line 11 is funรขโฌโ Math.ceil()'s argument expression is evaluated first. We invoke the valueOf() method that returns an anonymous Double object (with a value of 42.5). Then the doubleValue() method is called (invoked on the newly created Double object), and returns a double primitive (there and back again), with a value of (you guessed it) 42.5. The ceil() method converts this to 43.0, which is cast to an int and assigned to x.
Discuss About this Question.
String s = "ABC";
s.toLowerCase();
s += "def";
System.out.println(s);
String objects are immutable. The object s above is set to "ABC". Now ask yourself if this object is changed and if so where - remember strings are immutable.
Line 2 returns a string object but does not change the originag string object s, so after line 2 s is still "ABC".
So what's happening on line 3? Java will treat line 3 like the following:
s = new StringBuffer().append(s).append("def").toString();
This effectively creates a new String object and stores its reference in the variable s, the old String object containing "ABC" is no longer referenced by a live thread and becomes available for garbage collection.
Discuss About this Question.
public class SqrtExample
{
public static void main(String [] args)
{
double value = -9.0;
System.out.println( Math.sqrt(value));
}
}
The sqrt() method returns NaN (not a number) when it's argument is less than zero.
Discuss About this Question.
class Q207
{
public static void main(String[] args)
{
int i1 = 5;
int i2 = 6;
String s1 = "7";
System.out.println(i1 + i2 + s1); /* Line 8 */
}
}
This question is about the + (plus) operator and the overriden + (string cocatanation) operator. The rules that apply when you have a mixed expression of numbers and strings are:
If either operand is a String, the + operator concatenates the operands.
If both operands are numeric, the + operator adds the operands.
The expression on line 6 above can be read as "Add the values i1 and i2 together, then take the sum and convert it to a string and concatenate it with the String from the variable s1". In code, the compiler probably interprets the expression on line 8 above as:
System.out.println( new StringBuffer()
.append(new Integer(i1 + i2).toString())
.append(s1)
.toString() );
Discuss About this Question.
Discuss About this Question.