Chp14: Java

1.finally keyword:

finally keyword is used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown. The finally block will be executed after the try and catch blocks, but before control transfers back to its origin.

2. Overloading vs Overriding:

overloading is a term used to describe when two methods have the same name but differ in the type or number of arguments.

overriding is when a method shares the same name and function signature as another method in its super class.

3. Java generics and C++ templates:

C++ templates can use primitive types, like int. Java cannot and must instead use Integer.

In Java, you can restrict the template's type parameters to be of a certain type. For instance, you might use generics to implement a CardDeck and specify that the type parameter must extend from CardGame.

In C++, the type parameter can be instantiated, whereas Java does not support this.

InJava, the type parameter cannot be used for static methods and variables, since these would be shared between MyClass<Foo> and MyClass<Bar>. In C++, these classes are different, so the type parameter can be used for static methods and variables.

In Java, all instances of MyClass, regardless of their type parameters, are the same type. The type parameters are erased at runtime. In C++, instances with different type parameters are different types.

你可能感兴趣的:(java)