20155335 Eucun "Java Programming" Fifth week summary

Source: Internet
Author: User
Tags assert stack trace throwable try catch

# 20155335 Java Programming Fifth week of study summary

# # Textbook Learning Content Summary

# # Problems and solutions in textbook learning

For exception handling, there are always unexpected errors in the program, and errors in Java are presented as objects to various subclass instances of Java.lang.Throwable.

First, try and catch can be used to catch errors in the program, If a Java.util.InputMismatchExpection error message appears, indicating that the Scaneer object is not expected, because the next string itself represents a number, expected by the Scaneer object, and using the try and catch syntax, the JVM tries to execute the program code in the try block. If an error occurs, the execution process jumps off the point of the error, then compares the type declared in the catch parenthesis, conforms to the object type of the thrown error, and, if so, executes the program code of the Catch block. All errors in Java will be packaged as objects.

Exceptions refer to Java's non-compile-time anomalies or errors Java uses an object-oriented approach to handling exceptions, which encapsulate each exception that occurs in the program into an object to represent; Java classifies exceptions, different classes represent different types of exceptions, The two subclass of the exception, error and Expection, which represents a serious system error, such as a hardware-level error, JVM error, or insufficient memory, the Java application itself is unable to respond when a system critical error occurs. Expection said that the program can also overcome and restore the problem, which is divided into system anomalies and common anomalies, system anomalies when the software itself caused by defects, in this problem can also let the software continue to run or let the software die, ordinary abnormal users can overcome.

Java system Exceptions and common exceptions provide different solutions, and the compiler enforces that ordinary exceptions must be handled by a try catch or by throwing a throw declaration to the upper layer. But it doesn't matter whether the system exception can be handled or not.

Inheritance schema exception, from the inheritance schema diagram of the exception class can be seen that the Expection class extends a number of subclasses, where IOException runntimeexpection is the more common two, Runntimeexpection even if you do not write exception-handling program code, you can still compile successfully, and this exception must be run in the program can only occur, for example: The index value of the array is out of range, unlike Runntimeexpection, Ioeexpection be sure to write exception handling program code, it is usually used to handle input//output related operations, such as file access, network links, when the exception occurs, the statement code of the exception will throw an exception class instantiation object, This object is then matched to the type of the class in the catch statement and then processed in the corresponding catch.

An exception occurs when we call multiple methods, and we can use the stack to trace the origin of the exception, which is collected automatically using the exception object. The simplest way to view the stack trace is to call the exception object Printstacktrace () method, the type of the exception is shown in the stack information, the root of the known source is at the top level, the order of the calling method is as follows, and the individual stack trace elements can be processed using Getstacktrace (). The stackttaceelement array is returned, and the information about the source of the exception is indexed by the array 0, which can then be called by each method. such as Stacktraceelement GetClassName () and other methods to obtain the appropriate information.

When using a throw-toss exception, the origin of the exception's trace stack is still the source of the exception, not the place where the exception was thrown. If you want to let the exception stack start to throw the exception, you can use the Fillstacktrace () method, the method will reload the exception stack, the starting point as the place to re-throw the exception and return the Throwable object, the example is as follows

public class stacktracedemo{

public static void Main (string[] args) {

try{

C ();

}catch (NullPointerException ex) {

Ex.printstacktrace ();

}

}

static void C () {

try{

b ();

}catch (NullPointerException ex) {

Ex.printstacktrace ();

Throwable t = ex.fillinstacktrace ();

Throw (NullPointerException ex) T;

}

}

static void B () {

A ();

}

Static String A () {

String text = NULL;

return Text.touppercase ();

}

About assert

With respect to assertions (assert), we know that when a program is required or designed, in which state it is only in and not in two states during program execution, the Assert keyword is used, and the two methods use the Assert,assert

Boolean_expression and assert boolean_expression:detail_expression; If the former is ture, nothing happens, otherwise java.lang.Assertionerror occurs, If the second syntax is taken, the result of the detali_expression will be displayed, and if the call to ToString () displays the result, then there is an object.

?

Collection Framework, collection is an interface, his two branches are list and set,list and set are all interfaces, they inherit with collection. The list is an ordered queue, and the list can have duplicate objects, but the set has no duplicate elements, and both list and set have their own implementation classes.

Abstract Abstractcollection Abstract class can achieve most of the functions, through the inheritance of abstractcollection omit the repeated encoding, Java. Util. Abstractcollection operations The basic behavior of list, if necessary, can inherit abstractcollection operation own collection, inherit abstractlist operation own list, is much more convenient than manipulating collection or list interfaces directly.

As to when to use ArrayList, when to use the LinkedList, consider the use of the former, is to consider whether to use the array characteristics, adjust the index order is inconvenient, with the linklist when manipulating the list interface, using the link structure to collect objects, Refer to the next object one by one and count to get the object directly indexed.

Using generics

Generic definition: Its nature is parameterized type, that is, the data type of all operations is specified as a used when specifying a type, which can be used in the creation of the class interface method. Generics in Java exist only in the source code of the program, and are replaced with the original primitive type after compilation. , bin and insert the forced transformation code in the appropriate place.

A more general raw storage method is provided in map, which is used to store collection class elements. , map defines four types of interfaces and methods.

-1 Questions

Operation Array Object P102 page run result first three null

-Xx1 Solutions

The initial value of the known class type is null.

Integer [] scores=new integer[3]; The number of objects created is 0.

-XX2 problem

How do I make the p135 page overloaded correctly?

-XX2 Solutions

Change the public double somemethod (int i) in line sixth to

Public double SomeMethod (float i)

-  ...

-  ...

# # [Code Hosting] (link)

-Code Submission Process:

-Run git log--pretty=format: "%h-%an,%CD:%s" and

-Code Volume:

-Run Find Src-name "*.java" |  Xargs Cat |  Grep-v ^$ | Wc-l and

# # last week quiz summary of the wrong title

-Error 1 and cause 3.10&9 operation result is 8, known 10 is 00001100,9 00001011, the same or the result is 0000100, that is 8.

-Wrong question 2 and reason 15. The method declared as protected can only be accessed after the subclass of the inheritance, is actually wrong,

The class is qualified as a member of the protected, and can be accessed by the class itself, his subclasses and the subclasses of the same package as well as all other classes in the same package, and the access rights of the inherited member variables and member methods do not change.

-Wrong question 3 and why

Blog title "Study Number 2016-2017-2" Java programming "the first week of learning summary"

20155335 Eucun The fifth week of Java programming

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.