Thinking in Java notes

Source: Internet
Author: User
Tags bitwise operators

Sophomore bought this book, and now read this book, see some of the previously not seen details, but also have a different experience. This article uses the 4th edition to organize the notes of each chapter. The foreigner's book has a characteristic, he will spend a lot of words to elaborate a concept, this is much stronger than the heap code.

Chapter 1th Introduction to Objects

1.1 Abstraction

Abstraction is one of the most important concepts of computers. C in solving problems, it is mainly based on the structure of the computer to abstract, rather than based on the structure of the problem to be solved. While Java is modeling the problem and describing the problem according to the problem, the program can apply a new type of object to make itself suitable for a particular problem, and programmers will not be limited to any particular type of problem.

Objects have state, behavior, and identity. The basic features of pure object-oriented programming:

    • Everything is object: objects can store data (state), and can perform certain actions (behaviors). Theoretically, you can extract any conceptualization from the problem you are solving to build as an object.
    • A program is a collection of objects that tell each other what they want to do by sending a message: it is actually a method of invoking a particular object.
    • Each object has its own and other objects that make up the storage: new objects, inheritance, and composition are generated from existing objects.
    • Each object has its type: Each object is an instance of a class.
    • A class of objects can receive the same message: polymorphic, subclasses can override the parent class to complete some calls.

1.2 Classes and objects

Each object is an instance of a class, and a class is actually an abstract data type that describes a collection of objects with the same attributes (members) and behaviors (functions). Instead of being forced to use existing data types that represent the storage units in a machine, programmers can define classes to accommodate problems. Add new types of extension programming languages as needed, and the system validates and manages them as if they were built-in types.

Consider the object as a service provider benefit: to decompose the problem into a collection of objects, to improve the cohesion of the object, each object is focused on its own work, that is, cohesion, there is a low coupling, decoupling is generally used in the queue to achieve, easy to understand code and reuse.

Object-oriented features: encapsulation, inheritance, and polymorphism. What is encapsulation? That is, the properties and details of the object are hidden, only the interface is exposed externally, and the read and modify properties are controlled. The reason for the presence of access control is to isolate parts unrelated to the provided service, and to isolate and secure interfaces and implementations. There are four kinds of access rights in Java, namely:

    • Public: accessible to anyone
    • Private: Only class creators and internal methods can access
    • Protected: Equivalent to private, the difference is that subclasses can access
    • Default access: Classes within the same package can access each other, and packages are like private

1.3 Code Reuse

Two ways, combination and inheritance, the combination flexibility is relatively high, compared to the inheritance coupling degree is lower. If you want to use a service feature provided by a class, it is generally used in combination when using inheritance if the interface provided by the class is used.

Inheritance, create a new type using an existing class. Subclasses have members of the parent class (public, protected) and replicate the interface of the parent class, that is, the child class has the same type as the parent class. Subclasses have two ways of changing their behavior: adding new methods and overriding methods of the parent class. When you add a new method, you can extract it into the parent class if all of the subclasses need it.

In Java, object is the direct or indirect parent class for all classes. The benefits of a single inheritance are: All objects have a common interface that facilitates backwards compatibility; All objects have some functionality, are easy to create and pass through, and are easy to recycle.

1.6 polymorphic

Since the parent and child classes are of the same type, the subclasses can replace the parent class (up-conversion) at run time to accomplish different functions, which is polymorphic. The manifestation of polymorphism is: overloading and overwriting of methods. The compiler (static distribution, overloading) and the running system (JVM dynamic distribution, overwrite) will handle the details and ensure the correct behavior of the program.

1.7 Containers and generics

containers, which are data structures in Java, provide different interfaces and behaviors for different containers and have different efficiencies for some operations. Before JDK 5, the object stored by the container is OBEJCT, the storage object must be transformed upward, loss of its identity, and when it is removed, it is possible to make an error, because you do not know what type of object was put in before, so JDK5 added generics to explicitly indicate what type of object the container can receive.

1.8 Creation and lifecycle of objects, exceptions and concurrency

Java uses dynamic memory allocation techniques to create objects on the heap using the keyword new. Use the garbage collector to free the memory that the object occupies.

Java has built-in exception handling and is forced to use. Exceptions provide a way to reliably recover from errors.

Concurrency Controls access to shared resources, Java provides a concurrent programming library, and there are readily available concurrency data structures.

1.9 Java and the Internet

Network programming will involve a lot of knowledge, TCP/IP, multi-threading, IO model, to write high-performance Java programs, or to make a big effort, although the big problem by the JVM was done.

2nd Chapter Everything is the object

2.1 Objects

Java uses references to manipulate objects and create objects using new. So where does the object be placed? There are 5 places in the calculation where data can be stored, namely:

    • Register: Located in the CPU, the fastest storage area, according to the needs of distribution, can not directly control
    • Stack: In RAM, use stack pointer to move up and down to allocate memory, speed is second only to register, Java object is not here, but reference here. The Java system knows the exact life cycle of the elements in the stack
    • Heap: In RAM, storing all Java objects, allocating memory is more flexible, but at the cost of slow
    • Constant storage: Constant values are usually placed inside the program code or in ROM read-only memory
    • Non-RAM storage: such as serializing an object to disk, or exists in a database

2.2 Basic Types

The base type stores the value and is placed on the stack, efficiently. The size of the base type in Java is fixed and does not change with the hardware architecture. The basic types are as follows:

    • char:16-bit,2 bytes, Unicode characters, 0 ~ 2^16-1,character
    • Byte:8 bits,1 bytes, -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 are signed and JDK5, and the automatic packaging function automatically converts the base type to the package type.

High-precision numbers: BigInteger: integers that support arbitrary precision; BigDecimal: fixed-point numbers that support arbitrary precision.

Arrays are also objects that can store basic and reference types, and Java ensures that arrays are initialized.

2.3 Scope Scope

Java uses curly braces to define scopes, and when a local variable ends at the end of the curly brace, the life cycle ends, and the object is not so, it can be persisted, and Java manages the memory of the object through the garbage collector. Memory leaks are generally not present, but they are not absolute.

2.4 Classes, fields, methods

Use the class keyword to define a class that has fields (member variables) and methods, and for member variables, even if not initialized, Java guarantees that it has a default value, the reference type defaults to NULL, the number defaults to 0, and the boolean default False,char default ' \u0000 ' ( NULL). Initialization is enforced for the local variable compiler.

Methods, method names and parameters are called method signatures, and for parameters, only values are passed in Java. Java eliminates the problem of forward referencing, that is, the sequence of member variables and methods can be arbitrary in the same class.

The static keyword can be used to modify fields, methods, and classes. Modified Field Method: Represents a class that can be used without creating a new object. Typically, the inner class is decorated, and this class has no difference from the general class.

2.5 notes

Common tags and HTML are as follows:

    • @see: Referencing other classes, @see classname, @see fully-qualified-classnam#method-name
    • @author: Author Information
    • @version: Version Information
    • <pre>code</pre>: Code
    • <ol><li>1<li>2</ol>: List
3rd Chapter Operator

3.1 Precedence & Assignment operators

Multiplication and subtract from left to right, when unsure, use parentheses to clearly identify.

The assignment operator (=), which assigns a value to the base type, is to copy the contents of one place to another, such as int a=b, which is to copy the contents of B to a; Assigning a value to an object only causes the variable to point to that object, such as String s = A,s and A to the same object. When you pass an object to a method, it simply passes a referenced value, or passes an alias for an object.

3.2 Arithmetic, relations, logical operators, direct constants

Subtraction, modulo, unary plus, minus operator, self-increment, self-reduction.

= = is applied to the base type, the comparison value is equal, if the object comparison is the same reference, the comparison object uses equals, the default equals comparison reference, needs to be overridden.

With (&&), or (| | ), non (!) generates a Boolean value that has a short-circuit function, that is, if the first expression can determine the result of the entire expression, then the subsequent expression is not evaluated.

Direct constants, you must explicitly tell the compiler constant type, such as 10f,10d,10l,0xff. For Char, BYTE, short more than its maximum range is automatically converted to int.

Exponential notation: Float a = 1.39e-43f, or 1.39xe^-43, which, if not added to the F compiler, is treated as double, prompting for type conversion.

3.3-bit operators and shift operators

Bitwise operators:

    • With (&): Same as 1, output is 1
    • or (|): 1, Output is 1
    • XOR (^): Not all 1, output is 1
    • Non (~): Take counter

Shift operators, which can only be used to process integers, char, byte, and short shift automatically to int:

    • Shift Left (<<): Low 0, equivalent to multiply 2^n,n to move the number of digits
    • Shift Right (>>): Use sign bit extension, 0 for positive high, 1 for negative high, equivalent to divide by 2^n,n to move number of bits
    • Unsigned right Shift (>>>): High position with 0 expansion

In the shift, such as int only the lower right side of the low 5-bit is useful, such as 16>>2 and 16>>34 equal, because 2^5=32, equivalent to 32 modulo. A long type is a low 6-digit number valid.

Here to say two more sentences, the source code or the shift will often see (&0XFF), why?

In general, we will operate on byte bytes, first 0xFF represents a low 8 bits (1 bytes), when the byte shift operation, will automatically turn to int, and in Java, the INT type has 32 bits, and the number in the computer using signed binary complement representation. As a result, a byte becomes an int with a symbol extension and a high position filled with a sign bit. If a byte is a positive number then its complement is the same as the original code, at which point and operation does not matter, but for a negative number, such as byte a =-4, and its conversion to int in the internal representation is 11111111111111111111111111111100, This operation is obviously not correct, the effective bit is only low eight bits, so with 0xFF bit and operation, the high 24 position 0, the result is correct.

Thinking in Java notes

Related Article

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.