Java Classroom Answer

Source: Internet
Author: User
Tags comparable getmessage throwable java throws

Which of the following statements will cause a compilation error? Which one will cause a run-time error? Why?
M=d;
D=m;
D= (Dog) m;
D=c;
C= (Cat) m;
First make a self-judgment, draw a conclusion, run the Testcast.java instance code to see if your judgment is correct

Compile error

D=m;d=c;

An incorrect subclass object can be directly assigned to a base class variable.
To assign a base class object to a subclass object variable, you must perform a type conversion,
Its syntax is: Sub-class object variable = (subclass name) base class object name;

Run error c= (Cat) m

Incorrect conversion confusion. If the type conversion fails, Java throws the following exception: ClassCastException

2 You can use the instanceof operator to determine whether an object can be converted to the specified type:
Object obj= "Hello";
if (obj instanceof String)
System.out.println ("Obj object can be converted to a string");

public static void Main (string[] args)
{
When you declare hello using the object class, the compilation type of Hello is Object,object is the parent class of all classes
But the actual type of the Hello variable is string
Object Hello = "Hello";
String is a subclass of the object class, so it returns true.
System.out.println ("Whether the string is an instance of the Object class:" + (hello instanceof Object));
Returns True.
System.out.println ("String is an instance of String class:" + (hello instanceof string));
returns FALSE.
System.out.println ("String is an instance of the Math class:" + (hello instanceof Math));
String implements the comparable interface, so returns TRUE.
System.out.println ("String is an instance of the comparable interface:" + (hello instanceof comparable));
String a = "Hello";
The string class is neither the math class nor the parent class of the math class, so the following code compilation does not pass
System.out.println ("String is an instance of the Math class:" + (a instanceof math));
}
}

3

1. What is the result of the program running below? 2. How do you explain that you will get such output? 3. The computer is not wrong, the reason why you get such a running result is also a reason, then from these running results, you can summarize the Java syntax features?

Make sure to summarize the brain, then modify or write some code to test, verify your own ideas, and finally look at the following PPT to give the conclusion.

public class Parentchildtest {public static void main (string[] args) {parent parent=new parent ();p Arent.printvalue (); Child child=new Child (); Child.printvalue ();p arent=child;parent.printvalue ();p arent.myvalue++;p arent.printvalue () ;((child) parent). myvalue++;p Arent.printvalue ();}} class Parent{public int myvalue=100;public void Printvalue () {System.out.println ("Parent.printvalue (), myvalue=" + myvalue);}} Class Child extends Parent{public int myvalue=200;public void Printvalue () {System.out.println ("Child.printvalue (), Myvalue= "+myvalue);}}

1)

Parent.printvalue (), myvalue=100

Child.printvalue (), myvalue=200

Child.printvalue (), myvalue=200

Child.printvalue (), myvalue=200

3)

when a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type. If the child class has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword.
If the subclass is used as a parent class, the field accessed through the subclass is the parent class!

4 Hands-on brain: Read and run the Aboutexception.java sample, then use the next few pages of PPT to learn about exception handling in Java

5

Read the following code (Catchwho.java) to write out the results of the program run:

ArrayIndexOutOfBoundsException "+"/inner layer Try-catch\

occur ArithmeticException

6 Write the results of the Catchwho2.java program run

arrayindexoutofboundsexception/outer Try-catch

7 Read the Embedfinally.java example before you run it, observe its output, and summarize it.

When there are multiple layers of nested Finally, exceptions are thrown at different levels and thrown at different locations, which can result in a different order of the finally statement block execution.

8

Analysis: The finally statement block will be executed?
Please answer these questions through the Systemexitandfinally.java sample program

Throwable There are two direct subclasses of the class:

1 , Exception : Problems that arise can be captured;

2 , Error : System error, usually by JVM processing.

When an exception occurs in the program, the JVM looks for the error handler in order of the method invocation.
You can use the Printstacktrace and GetMessage methods to understand what happens when an exception occurs:
Printstacktrace: Prints the method call stack.
Each object of the Throwable class has a GetMessage method that returns a string that is passed in the exception constructor and typically contains information about a particular exception.

9 Please use the Printexpressionstack.java sample to learn the above content

10

A method can declare multiple exceptions to be thrown
int g (float h) throws Oneexception,twoexception
{ ...... }
The Throwmultiexceptionsdemo.java example shows the relevant features.

11

The throws clause of a subclass throws an exception that cannot be the parent of an exception object that is thrown by a method with the same name as its base class.
The Overridethrows.java example shows this syntax feature of Java.

12

The Exceptionlinkinrealworld.java example shows a typical exception handling code template

13

Java Classroom Answer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.