Read the first half of the <thinking in java>

Source: Internet
Author: User
Tags integer division java web

Read the 1-14 chapters.
This book is really not suitable for beginners, and may be more suitable for people who have experience in Java for a year or two.
Learning is really a spiral progressive process. Just started to learn basic Java grammar, the book saw a lot of things feel too little detail, have not seen, use, resulting in the book can not be seen down. Later learn the Java Web, found that the actual application will really use a lot of things, have a book to time to hate less feeling, feel not to add basic knowledge to go on. So this book to see, the first 14 chapters is a serious reading, but also made notes, as a result, but also more and more feel that reading can not go on. Reading the 15th chapter of the generic type, it is not going to go down.
Next I intend to continue to learn the Java Web, and then the book to time to hate less when the feeling of return to "Core Java", the first scholar still have to read this.

Chapter II Everything is the object

All you do in Java is define classes, produce objects of those classes, and send messages to those objects.

Two types of elements in a class: Fields and methods.

Java eliminates the "forward reference" issue.


When declaring a thing to be static, it means that the field or method is not associated with any object instance of the class that contains it. So, even if you never create any objects of a class, you can call its static method or access its static domain.


A static field has only one copy of the storage space for each class, and not a static field that has one storage space for each object.

Chapter Three operator

Integer division, or when you convert a decimal number to an integer, it directly removes the decimal digits of the result, rather than rounding it.


relational operators = = and! = Apply to Objects. The reference to the object is compared.
The default behavior of equals () is to compare references. Most class libraries Implement the Equals () method, which is used to compare the contents of an object.


Direct constant: The suffix L represents long,f on behalf of Float,d for Double. Prefix 0x hexadecimal number, 8 binary number. (The above letters are case-sensitive)


Exponential notation: float f = 1e-43f;

The fourth chapter controls the execution process

foreach can be used for arrays and any other iterable object.

for (float x:f) {}


Label: Located just before the iteration statement. The only reason to use a label is because there is a loop nesting, and you want to break or continue from multiple layers of nesting.

Fifth chapter initialization and cleanup


The constructor name is exactly the same as the class name (capitalized in the first letter).

The constructor does not have a return value.

The compiler secretly passes the reference to the manipulated object as the first argument to the calling method.

The This keyword can only be used inside a method, representing a reference to the "object that called the method".

It is not necessary to invoke another method of the same class within a method.

In the constructor, you can call another constructor with this, and the call must be placed at the very beginning.
Flower (int a) {
THIS.A = A;
}
Flower (String s, int a) {
This (a);
THIS.S = s;
}

The static method is the method without this.

The non-static method cannot be called inside the static method.

The local variable of the method is not initialized, resulting in a compile-time error.
If the field of the class is a base type, uninitialized will get the default initial value.
class defines a reference to an object, and if not initialized, this reference gets a special value of NULL.

Explicit static initialization: static Block static{}, this code executes only once: When an object of this class is first generated, or when a static data member belonging to that class is first accessed.

Assigns an array to another, just copies the reference.

All arrays have an intrinsic member length.


The sixth Chapter access rights control


Java source code files are often referred to as compilation units, and must have a suffix name. java. There can be a public class within a compilation unit, and the name of the class must be the same as the file name. Each compilation unit can have only one public class.


Seventh re-use class


When an exported class object is created, the object contains a child object of a base class.

If there is no default base class constructor, or if you want to invoke a base class constructor with parameters, you must explicitly write the statement that calls the base class constructor with the keyword super, with the appropriate argument list.

You can add @override annotations when you want to override the method, and the compiler generates an error message if you don't pay attention to overloading rather than overriding the method.

The final base type keeps the values constant and is used for object references, and final makes the reference constant.

A typical way of defining constants: defined as public can be used outside the package; the definition of static emphasizes only one copy; the definition is final. The description is a constant.

Java allows you to specify the parameters as final in the parameter list declaratively.

Final method: Prevents any inherited classes from being modified and will not be overwritten.

All private methods are implicitly specified as final.

Final class: Cannot be inherited, no child class.


The eighth chapter Polymorphic


Late binding, meaning that it is bound at run time based on the type of the object. Also called dynamic binding or run-time binding.

All other methods are late-bound except for the static method and the final method, which belongs to the final method.

A dynamic binding method that is constructing an object is called inside the constructor, and the result may be unpredictable. So writing the constructor applies the simplest possible way to get the object into a normal state, and avoid calling other methods if you can.

All transformations in Java will be checked. The behavior of checking a type during run is called run-time type recognition (RTTI). Transformation failure will return classcastexception.


Nineth Chapter Interface


Abstract method, with only declarations and no method body.

Classes that contain abstract methods are called abstract classes.

To create an interface, replace the class keyword with the interface keyword.

An interface can contain fields, which are implicitly static and final.

Methods in an interface must be public, even if they are not explicitly declared.

The core reason for using interfaces is to be able to move up to multiple base types (and the resulting flexibility). The second reason is the same as using the abstract base class: Prevents the client programmer from creating objects of that class and ensures that only an interface is established.

Adapter interface: We can add new interfaces on any existing class, so this means that the method accepts the interface type, which is a way for any class to adapt the method. This is where the power of using interfaces rather than classes is.


Tenth chapter Inner class


A typical case is that the outer class will have a method that returns a reference to the inner class.

If you want to create an object of an inner class from anywhere other than the non-static method of the outer class, you must specifically indicate the type of the object: Outerclassname.innerclassname.

The inner class has access to all members of its external class.

You need to get a reference to an external class object, which you can use immediately after the name of the outer class.

Unless you are creating a nested class (a static inner class), it is not possible to create an inner class object before you have an external class object.

. New syntax:
Outer o = new Outer ();
Outer.Inner i = o.new Inner ();

Nested classes: When an inner class is static, the object of its enclosing class is not required to create a nested class object. Nested classes do not have this reference pointing to the perimeter class object.

It does not matter how many layers an inner class is nested, it transparently accesses all members of the enclosing class that it embeds.


The 11th chapter holds the object


Unlike arrays, Java container classes can automatically adjust their size.

Why internal classes are required:
In general, an inner class inherits from a class or implements an interface, and the inner class's code action creates an object of its outer class.

Each inner class can inherit itself from one (interface) implementation independently, so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class.

The inner class effectively implements "multiple inheritance." That is, the inner class allows the inheritance of multiple non-interface types (classes or abstract classes).

By using generics, you can prevent objects of the wrong type from being placed in the container at compile time.

List:
ArrayList, the advantage of random access elements.
LinkedList, advantages of INSERT and delete operations, more features.

Set: Does not save duplicate elements. Common HashSet implementations, which are specifically optimized for fast lookups.

Map: The ability to map objects to other objects. A map can return the set of its key, its value of collection, and its set of key-value pairs.

Queue: Queues are FIFO containers, and are often used as a reliable way to transfer objects from one area of a program to another.

Iterator iterator:

(1) Use method iterator () requires the container to return a iterator. Iterator will be ready to return the first element of a sequence.

(2) Use Next () to get the next element in the sequence.

(3) Use Hasnext () to check if there are elements in the sequence.

(4) use remove () to delete the newly returned element of the iterator.

Implementing collection means that you need to provide a iterator () method.

The Iterable interface contains a iterator () method that produces iterator, and the Iterater interface can be used by foreach to move through the sequence.

The container cannot hold the basic type, but has the automatic packing mechanism to help.


12th. Error handling by exception


The exception description, using the throws keyword, indicates the type of exception that the method might throw.

The exception is generated in the method code: either handle the exception or indicate in the exception description that the method will produce an exception.

You can declare a method to throw an exception, but not actually throw it.

Throwable class: Error indicates compile-time and system error; exception is usually the basic type of programmer's concern.

Exceptions that are forced to be checked at compile time are referred to as checked exceptions. Exceptions inherited from RuntimeException can be thrown without exception descriptions, which are automatically thrown by the virtual machine and are also known as unchecked exceptions.

When you want to restore resources other than memory to their initial state, you need to use the finally clause.

When overriding a method, only those exceptions listed in the base class method exception description can be thrown.


13th Chapter String


The string object is immutable and has a read-only attribute. Each method that appears to modify a string value actually creates a completely new string object.

The regular expression.


14th Chapter Type Information


In general, you want most of your code to know as little as possible about the exact type of the object, but only with one generic representation in the object family. So polymorphism is the basic goal of OOP.

All classes are dynamically loaded into the JVM when they are used for the first time. This class is loaded when the program creates the first static member reference to a class. So the Java program is not fully loaded before it starts to run, and its parts are loaded when it is necessary, unlike many traditional languages.

Once a class object is loaded into memory, it is used to create all objects of the class.

Class.forName ("Name"); This method is a static member of class and returns a reference to a class object.

If you already have an object of the type of interest, you can get the class reference by using the GetClass () method.

Class literal constant: classname.class; generates a reference to a class object.

Generic class Reference: Creates a class reference that is limited to a type, or any subtype of that type:
class<? Extends number> B = int.class;

Keyword instanceof, which returns a Boolean value that tells us that the object is not an instance of a type.

The Class.isinstance () method provides a way to dynamically test object types.

For Rtti, the compiler opens and checks the. class file at compile time. For the reflection mechanism, the. class file is not available at compile time and opens and checks the. class file at run time.

Read the first half of the <thinking in java>

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.