Thinking in Java notes, thinkingjava

Source: Internet
Author: User

Thinking in Java notes, thinkingjava

I bought this book in my sophomore year. Now I have read this book again, and I have seen some details that I have not read before. This article uses version 4th to organize the notes of each chapter. A foreigner's book has a special feature. He will spend a lot of text to describe a concept, which is much better than heap code.

Chapter 2 object Introduction

1.1 Abstraction

Abstract is one of the most important concepts of computers. C is mainly to abstract the structure of the computer, rather than the structure of the problem to be solved. Java models the problem and describes the problem based on the problem. The program can apply itself to a specific problem by adding a new type of object, programmers are not limited by any specific type of issues.

The object has the status, behavior, and identifier. Basic Features of pure object-oriented programming:

  • All objects are objects: objects can store data (states) and perform certain operations (actions ). In theory, we can extract any conceptual building objects to be resolved.
  • Programs are a collection of objects. What they need to do by sending messages to each other is to call methods of specific objects.
  • Each object has its own storage with other objects: new objects are generated, inherited, and combined with existing objects.
  • Each object has its type: each object is an instance of a class.
  • A class of objects can receive the same message: polymorphism. Child classes can complete some calls instead of the parent class.

1.2 Categories and objects

Each object is an instance of a class. A class is actually an abstract data type, which describes a set of objects with the same features (members) and behavior (functions. Programmers can adapt to problems by defining classes, rather than being forced to use existing data types to represent storage units in machines. Add new types of extended programming languages as needed, and the system verifies and manages them just as it treats built-in types.

Consider an object as a benefit of a service provider: the problem is broken down into a collection of objects; the object cohesion is improved, and each object focuses on its own work, that is, high cohesion, and low coupling, decoupling is generally implemented using queues to facilitate understanding of code and reuse.

Object-oriented features: encapsulation, inheritance, and polymorphism. What is encapsulation? This function hides the attributes and details of an object and only exposes the interface to control the reading and modification of attributes. Causes of access control: isolate the components unrelated to the provided services; Separate and protect interfaces and implementations. Java has four access permissions:

  • Public: accessible to anyone
  • Private: only the class creator and internal method can access
  • Protected: similar to private. The difference is that sub-classes can be accessed.
  • Default access permission: classes in the same package can access each other. Different packages are the same as private.

1.3 code reuse

The combination is more flexible than the inheritance mode. If you want to use the service functions provided by a class, it is generally combined, and inheritance is used when you use the interface provided by the class.

Inheritance: Creates a new type using an existing class. The subclass has the public and protected members of the parent class and copies the interfaces of the parent class. That is, the subclass has the same type as the parent class. Subclass has two ways to change its behavior: Add a new method and override the parent class. When a new method is added, if all subclasses need it, you can extract it to the parent class.

In Java, objects are the direct or indirect parent classes of all classes. Benefits of single inheritance: all objects share interfaces, facilitating backward compatibility; all objects have certain functions, which are easy to create and pass parameters; and easy to recycle garbage.

1.6 Polymorphism

Since the parent class and subclass are of the same type, the Child class can replace the parent class (transition up) at runtime to complete different functions. This is polymorphism. Polymorphism is embodied in: Method overload and coverage. The compiler (static distribution, reload) and running system (JVM dynamic distribution, overwrite) process relevant details to ensure proper program behavior.

1.7 containers and generics

Containers are the data structures in Java. Different containers provide different interfaces and actions, which have different efficiency for some operations. Before JDK 5, the object stored in the container is Obejct. The storage object must be transformed upwards and its identity will be lost. If it is taken out, it needs to be transformed downward, which may cause errors, because I don't know what type of objects are put in the previous step, JDK 5 adds a generic type that explicitly specifies the type of objects that the container can receive.

1.8 object creation and lifecycle, exceptions, and concurrency

Java uses the dynamic memory allocation technology and the keyword new to create objects on the stack. Use the Garbage Collector to release the memory occupied by objects.

Java has built-in Exception Handling and is mandatory. Exceptions provide a reliable way to recover from errors.

Concurrency controls access to shared resources. Java provides a concurrent programming library with ready-to-use concurrent data structures.

1.9 Java and Internet

Network Programming involves a lot of knowledge, such as TCP/IP, multithreading, and I/O models. To write high-performance Java programs, it still takes a lot of time, although the JVM has done a big problem.

Chapter 1 everything is an object

2.1 objects

Java uses references to operate on objects and uses new to create objects. Where is the object placed? In computing, data can be stored in five places:

  • Register: It is located inside the CPU and the fastest storage zone. It is allocated as needed and cannot be directly controlled.
  • STACK: located in RAM, memory allocation is moved up and down using the stack pointer. The speed is second only to registers. Java objects are not here, but referenced here. The Java System knows the exact lifecycle of the elements in the stack.
  • Heap: It is located in RAM and stores all Java objects. The allocated memory is flexible, but the cost is slow.
  • Constant storage: the constant value is usually placed inside the program code or in the ROM read-only memory.
  • Non-RAM storage: for example, serializing an object to a disk or in a database

2.2 Basic Types

Store the value of the basic type in the stack for high efficiency. The size of basic types in Java is fixed and does not change with the hardware architecture. The basic types are as follows:

  • Char: 16-bit, 2 bytes, Unicode character, 0 ~ 2 ^ 16-1, Character
  • Byte: 8 bits, 1 byte,-128 ~ 127, Byte
  • Short: 16 bits, 2 bytes,-2 ^ 15 ~ 2 ^ 15-1, Short
  • Int: 32 bits, 4 bytes,-2 ^ 31 ~ 2 ^ 31-1, Integer
  • Long: 64 bits, 8 bytes,-2 ^ 63 ~ 2 ^ 63-1, Long
  • Float: 32 bits, 4 bytes, IEEE754, Float
  • Double: 64 bits, 4 bytes, IEEE754, Double
  • Boolean: true/false, Boolean
  • Void: Void

All values have positive and negative signs. The automatic packaging function of JDK 5 automatically converts the basic type to the packaging type.

High-Precision numeric: BigInteger: integer with any precision supported; BigDecimal: number of fixed points with any precision supported.

An array is also an object that can store basic and reference types. Java will ensure that the array is initialized.

2.3 scope

Java uses curly brackets to define the scope. When the curly brackets end, the lifecycle of a local variable ends. if the object is not the same, it can be saved all the time, java manages the object memory through the garbage collector. Generally, there is no memory leakage, but it is not absolute.

2.4 categories, fields, and methods

Use the class keyword to define a class. The class has fields (member variables) and methods. For member variables, Java ensures that they have a default value even if they are not initialized, the default reference type is null, the numbers are both 0 by default, the default Boolean value is false, and the default char value is '\ u000000' (null ). The local variable compiler enforces initialization.

Methods, method names, and parameters are collectively called method signatures. For parameters, only values are transferred in Java. Java eliminates the issue of forward reference, that is, the order of member variables and methods in the same class can be random.

Static keywords can be used to modify fields, methods, and classes. Modifier field method: indicates that it belongs to the class and can be used without creating a new object. It is generally used to modify the internal class, which is no different from the general class.

2.5 annotations

Common tags and html are as follows:

  • @ See: reference other classes, @ see classname, @ see fully-qualified-classnm # method-name
  • @ Author: author Information
  • @ Version: version Information
  • <Pre> code </pre>: code
  • <Ol> <li> 1 <li> 2 </ol>: List
Chapter 1 Operators

3.1 Priority & Value assignment operator

Multiplication and addition and subtraction from left to right. When you are not sure, use brackets to clearly identify them.

The value assignment operator (=) is used to copy the content of a place to another place. For example, if int a = B, the content of B is copied to; assigning values to an object only directs this variable to the object. For example, String s = a, s and a point to the same object. When an object is passed to a method, it only transmits a reference value or an object alias.

3.2 arithmetic, relational, and logical operators, direct Constants

Addition, subtraction, multiplication, division, modulo, unary addition, subtraction operator, auto-increment, and auto-subtraction.

= Act on the basic type, and check whether the comparison value is equal; Act on the object to compare whether it is the same reference, the comparison object uses equals, the default equals comparison reference, need to override.

And (&), or (|), non (!) Generate a Boolean value with a short circuit function, that is, if the first expression can determine the result of the entire expression, it will not calculate the following expression.

A direct constant must explicitly inform the compiler of the constant type, such as 10F, 10D, 10L, 0xFF. Char, byte, and short are automatically converted to int if they exceed the maximum range.

Exponential Notation: float a = 1.39E-43F; indicates 1.39 × e ^-43. If F is not added, the compiler will handle it as a double by default, and a type conversion will be prompted.

3.3-bit operators and Shift Operators

Bitwise OPERATOR:

  • And (&): The same as 1, and the output is 1
  • Or (|): There is 1, and the output is 1
  • Exclusive or (^): Not all is 1, and the output is 1
  • Non (~) : Reverse

The shift operator can only be used to process integers. It is automatically converted to int when char, byte, and short are shifted:

  • Left shift (<): Fill 0 in the low position, which is equal to multiplying by 2 ^ n, and n is the number of digits to move
  • Shift to the right (>): use the symbol bit extension to add 0 to the positive high position and 1 to the negative high position, which is equal to dividing by 2 ^ n and n to the mobile digits
  • Unsigned right shift (>>>): 0 extension for high positions

For example, int can be used only when the lower five bits on the right side of the value, for example, 16> 2 and 16> 34 are equal because 2 ^ 5 = 32, it is equivalent to modulo 32. The long type is valid when the number is 6 digits lower.

I would like to say a few more words here. I often see (& 0xFF) in the source code or during shift. Why?

Generally, we perform operations on byte bytes. First, 0xFF indicates a low 8-bit (1 byte). When the byte shift operation is performed, it is automatically converted to int, in Java, the int type is 32 bits, and the numbers in the computer are represented by signed binary complement codes. Therefore, when byte is converted to int, the symbol is extended, and the high position is filled with the symbol bit. If a byte is a positive number, its complement code is the same as the original code. bitwise AND operation are not required at this time, but it is different when it is a negative number, such as byte a =-4; it is converted into an int value in the internal representation, which is 11111111111111111111111111111100. This operation is obviously incorrect, and the valid bit is only eight low bits. Therefore, with the 0xFF bit and the operation, the 24 high position is 0, the calculation result is correct.

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.