F x = new F; x.f( );how does the compiler decide which f is called?
Ans: The compiler looks first in the class F. If an f method is defined, use it. Otherwise, search, in order, E, D, C, B, A, Object. Use the first f that is found. If no f is found, print an error message.
+---+ +---+
c | O------+ d | O------+
+---+ | +---+ |
V V
x y x y z
----+---- ----+---+----
C( ) 10 20 D(4, 5) 8 12 5
12 23 8 16 5
10 16 13
10 19 13
Output: 25
28
35
33
24
2913
Stack s = new Stack( );
s.push(new Person("Alice", 'F', 23));
s.push(new Integer(243));
s.push(new String("dog"));
s.push(new Double(3.14159265));
System.out.println(s);
Ans: Simply use the editor to replace all instances of the word
Person with the word Object.
Ans: If x is the object, use x.getClass( ).getName( ).
Ans: TestFinalize.java Person.java
A x = new B( );
public static void main(String[ ] args)
{
int[ ] a = {1, 2, 3, 4, 5};
System.out.println(a[5]);
}
Object
Throwable
Exception
RuntimeException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
try
{
// Code that might cause an exception.
}
catch(Exception e)
{
// Code to run if exception occurs.
}
Scanner s = new Scanner(System.in);
String fileName = s.next( );
FileReader fr;
try
{
fr = new FileReader(fileName);
}
catch
{
System.out.println("File " + fileName +
" not found.");
}
public static void main(String[ ] args)
throws FileNotFoundException
{
. . .
}
Object
Throwable
Exception
RuntimeException
ArithmeticException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
IOException
EOFException
FileNotFoundException
IllegalArgumentException
NumberFormatException
NoSuchElementException
InputMismatchException
NullPointerException
Error