Introduction to programming (Java) & #183; 4.1 meaning of data abstraction, Introduction to programming pdf

Source: Internet
Author: User

Introduction to programming (Java)-4. 1. Meaning of data abstraction, Introduction to programming pdf

You have no choice about the necessity to integrateyour observations,

Your experiences, your knowledge into export actideas, I. e., into principles.

-- Ayn Rand, Philosophy: Who Needs It, 1974

Data abstraction action clearly separates the abstract features of Data types from the specific details of their implementations. The "Abstract Feature" of the data type refers to the interface that is visible to the user code and how to use the data type. Data abstraction isPromotion of the principle of separation between interfaces and implementations at the type level and Object Technology.

This chapter describes the basic data types, abstract classes, and Java interfaces of Java as data abstraction. It focuses on the roles of abstract classes and interfaces. Next chapterContinue to introduce data abstraction-a linear table as an abstract data type and its implementation. 4.1 meaning of data abstraction

The value stored in the memory is called data or operations.Data ClassificationIt is to facilitate programmers to identify and operate on them. Several concepts are introduced in [2.2.2 Java data type:Data Type-- Defines a set of values and a set of operations for processing the value set. Java language types include basic types and programmer-defined data types (reference types ).

In [3.2.2 operator], various operations of the basic type are introduced, and seven operations of the reference type are listed in [2.4.1 reference meaning. Note that all of the above descriptions are inJava language[Not the JVM layer]. In other words, we have always discussed the Abstract Feature-interface of Java data types.


4.1.1 implementation of basic types

Basic Types of interfaces/abstract features includeValue range and applicable operators of this type. The most common int and boolean are used as examples to illustrate the meaning of data abstraction.

1. int type interfaces and Implementations
[Here we will introduce some precautions for int.] int is a mathematical integer simulation. The difference between int and a mathematical integer is that int is not infinite. The int type logically has 32-bit memory space. The maximum value of the int type is (231-1), that is, 2147483647 or 0x7FFF_FFFF, or the binary 0b01111111111111111_111111111111 according to [0.1.2 binary complement. When it is added with 1, it becomes 0x8000_0000, that is, the minimum value (-231, that is,-2147483648 ). Generally, integer computing is described as running on the Clock. When the calculation time exceeds the maximum or minimum value, it is called overflow (O verflow and underflow). When using large numbers, be careful to overflow, because no one or abnormal mechanism helps programmers prevent such program errors, it is a silent killer. The following code is available for verification:
Void dosomething (){
Int x = 0x7FFFFFFF;
System. out. println (x + 1 );
}
In addition, what value in int can I =-I? In addition to zero, there is a minimum value of 0x8000_0000. Therefore, the semantics of if (I =-I) is different from that of if (I = 0 ).
Int operations include most operators, such as arithmetic, comparison, bitwise operations, value assignment, and positive and negative numbers, ++, and -- Of The unary operations.


The preceding content is an int interface/Abstract Feature. As an int userWhen the int type (logically) has 32-bit memory space, it means that the user knows the value range of the int. In fact, the JVM specification specifies the intValue Range,No memory space defined for int type. Memory space used to store int data is freely set by different implementers of JVM.

Generally, word is used as the basic size unit of data values in JVM. Requires that the word size be large enough to save byte, short, int, char, float, returnValue (a JVM data type unavailable to Java programmers, used to implement the finally clause in Java programs) and reference. The two characters can be long or double. Generally, the local pointer size of the host platform is used as the size standard. If the machine and system platform are 32-bit, the int value and all references are allocated 32-Bit Memory. If the machine and system platform are 64-bit,The int value may be allocated 64-bit space..

Generally, int users do not care about the implementation of int. The int memory space, whether 32-bit or 64-bit, must not affect the int interface. Even if the system uses 64-bit to save the int value, 2147483647 + 1 will (should also be) Overflow (to the minimum value ).

Exercise 4-1.: Explain the separation of int interfaces and implementations.

Exercise 4-2.: Program typeSystem. primitive. IntDemo, verify overflow and I =-I.

 

2. boolean interfaces and Implementations

What computers do is only arithmetic and logical operations. Therefore, the Boolean expression is in the if-else statement ,? : Operation and loop statements are widely used. Relational operators such as 5> 2, x! If the value is equal to y, a boolean value is obtained. A boolean operator such as p & q performs logical operations. Any non-zero integer x, expression x! The value of = 0 is true. Any non-null reference ref, expression ref! = The value of null is true.

When we talk about Java boolean expressions and boolean types, we actually talk about the boolean interface. HoweverSuch an important and basic boolean Type in Java does not exist substantially.Well, this joke Is a little big. In the JVM Specification Version 2nd, there Is a section <3.3.4There Is No boolean Type>. In fact, JVM defines the boolean Type, but does not provide commands to operate the boolean Type. The title of Java virtual machine specification (Java SE 7) <2.3.4 boolean type> is more rigorous.

In JVM, the boolean Type operation of source code is implemented as the int type operation of JVM. The boolean [] type of the source code. Access and modification of its elements use the baload and bastore commands of the byte array of JVM. To put it simply, JVM defines the boolean data type, for example, using "Z" or "[Z" to represent boolean and boolean [], you can also use the newarray command to directly create a boolean [].However, for a boolean operation to an int operation, 1 indicates true, 0 indicates false, and boolean [] indicates a byte array operation. The JVM Implementation of Sun regards the boolean [] element as the 8-bit value, other JVM implementations may adopt compression, for example, one.

The int and boolean examples reflect the differences between the abstract features of the data type and the specific implementation details. This is the meaning of the data abstraction that separates the data interface from the implementation.

Exercise 4-3: explain the differences between the boolean type at the Java language level, JVM specification level, and JVM implementation level, and explain the abstract value.

4.1.2 class Interface

The reference type does not need to involve JVM as the basic type.There are no objects in the source code. Only variables and reference values are referenced.For object implementation, refer to [objects on the 7.4.3 Stack]. Class interfaces and implementations are described in [Chapter 1 encapsulation], which is briefly described here.

1. Class APIs

Parnas principles/separation of interfaces and ImplementationsA user programmer consciously ignores the implementation of a method at the module or method level. Each method has its own interface. For users of A class A, the (method) interface defined by A is worth noting only when it can be accessed. Many methods, such as private methods, are used only by A. It is not important for the outside world to determine whether these methods exist. ThereforeIn object-oriented technology, Parnas principles are promoted to the class layer.

Class InterfaceA set of interfaces defined by the class that can be accessed by external objects. Generally, the public and protected fields declared in the class are also part of the class interface.

According to the above definition, as the relationship between the client program and the class is different, for example, in or out of a package, is or is not a subclass of the classThe set of class interfaces varies to a certain extent for the customer class. The interface that uses the access modifier to restrict classes. This mechanism is called encapsulation., Which is detailed in [Chapter 1 encapsulation.

A class interface is often called an API of this class to be different from the interface type in Java. Generally, the private and package-private methods are not class interfaces.

 

2. classes and types

In most cases, the class can be regarded as a type. For example, in [2.2.2Java data type], the concept of the (data) type takes the class as the class type.

The difference between subclass and subtype is introduced in [2.1.1's replacement principle]. What are the differences between classes and types?

The difference lies in the separation of interfaces and implementations.Type is a name that identifies the class interface. If an object can accept all operation requests (method calls) of Class X interfaces, the object has the X type. It is precisely because the Java subclass can satisfy the interface of the parent class (although it can be rewritten), soSubclass objects can have multiple types in the class hierarchy at the same time..

Class is a combination of interfaces and implementations. The class not only defines a type, but also defines the implementation of the internal state and method of the object, as well as methods that are not interfaces (such as private methods ). In short, the type is the class interface, and the class is the type + implementation.

Exercise 4-4.: an object can have multiple types. These types constitute a ____, like biological classification.

Exercise 4-5: Objects of different classes can have the same type. This common type is called ____.

Exercise 4-6: subclass inherits the interface of the parent class. Discuss this proposition.

Exercise 4-7.: explain the differences between class and type.




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.