Java - OOP 2
Java - OOP 2
For example, the following methods are used to get the value
associated with the corresponding wrapper object: intValue(),
byteValue(), shortValue(), longValue(), floatValue(), doubleValue(),
charValue(), booleanValue(). Exampl2
This example will output the same result as the example above:
Another useful method is the toString() method, which is used to
convert wrapper objects to strings. In the following example, we
convert an Integer to a String, and use the length() method of the
String class to output the length of the "string": Example3
Java Exceptions
When executing Java code, different errors can occur: coding
errors made by the programmer, errors due to wrong input, or
other unforeseeable things.
When an error occurs, Java will normally stop and generate an
error message. The technical term for this is: Java will throw
an exception (throw an error).
Java try and catch
The try statement allows you to define a block of code to be
tested for errors while it is being executed.
The catch statement allows you to define a block of code to be
executed, if an error occurs in the try block.
The try and catch keywords come in pairs:
Consider the following example:
This will generate an error, because myNumbers[10]
does not exist.
public class MyClass { public static void main(String[ ]
args) { int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error! } }