Let's look at this line of code
<PRE>
factor= Float.valueOf(s).floatValue();
</PRE>
Float is a wrapper class and it has a static method with the signature
public static Float valueOf(String s) throws NumberFormatException It "attempts to" convert the argument into a Float object. Remember this is the Float object, and NOT float primitive . So, Float.valueOf(s) would give you a Float object assuming the String s can be converted to a float number without any errors.
Because the code snippet given by you is incomplete, I am assuming 'factor' is declared to be of type float . As Float object cannot be directly assigned to a primitive, we will have to use another method defined on the Float wrapper class called floatValue() which returns the value contained in the wrapper class as a primitive.
In essence, the one line of code above is trying to convert the
value of String s to a floating point number with the help of the
Float wrapper class. Obviously this operation might throw some
exceptions especially when the string argument cannot be converted to a floating point number. That's why we have the try-catch safety net.
About your second question, your code is missing paranthesis in
the first line. Also the characters a and b should be enclosed
in single quotes, not the back-quotes that appears in your snippet. Perhaps when you copied the code from the example, some how the quotes got messed up. One way of avoiding such bugs is to use a
Java editor which has syntax highlighting, so that you will know what is wrong soon after you paste the code into a new file.
I fixed these two things and it worked like a magic!
<PRE>
class ValueOfTest
{
public static void main ( String s[])
{
String str3= String.valueOf(new char[]{'a','b'});
System.out.println( str3 ) ;
}
}
</PRE>
Hope this helps.
Ajith