2.JAVA Programming Idea--everything is an object

Source: Internet
Author: User

Everything is an object.

Welcome reprint, Reprint please indicate source: http://blog.csdn.net/notbaron/article/details/51040221

Although based on C + +, Java is a more purely object-oriented programming language

Before formally using it to design, you must first transfer your thoughts into an object-oriented world

1 manipulating objects with handles

In Java, anything can be considered an object. Can adopt a unified grammar, anywhere can be copied not mistaken. Note that although everything is "treated as" an object, the manipulated identifier is actually a "handle" (Handle) that points to an object.

2 All objects must be created

When you create a handle, you want to connect to the same new object. This is usually achieved with the new keyword.

When the program runs, it is important to note that the memory is allocated.

There are six places where you can save data.

2.1 Registers

This is the fastest save area because it is located in a different place than all other ways of saving: inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, nor can we find any trace of the register in our own program.

2.2 Stacks

resides in the general RAM (random access memory) area, but can be directly supported by its "stack pointer" for processing. If the stack pointer moves down, the new memory is created, and if you move up, the memory is freed. This is a particularly fast, particularly effective way to save data, second only to registers. When you create a program, the Java compiler must know exactly the "length" and "time of existence" of all data saved in the stack. This is because it must generate the appropriate code to move the pointer up and down. This restriction undoubtedly affects the flexibility of the program, so although some Java data is stored in the stack-especially the object handle-the Java object is not placed in it.

2.3 Stacks

A general-purpose memory pool (also in the Ram area) in which Java objects are saved. Unlike stacks, the most appealing aspect of the heap is that the compiler does not have to know how much storage to allocate from the heap, or how long the stored data will stay in the heap. As a result, there is greater flexibility in storing data with heaps. When you require an object to be created, you only need to compile the relevant code with the new command. When the code is executed, the data is saved automatically in the heap. Must pay a price: it takes longer to allocate storage space in the heap!

2.4 Static Storage

"Static" means "in a fixed position" (also in RAM). During the run of the program, the data that is stored statically is always waiting to be called. The static keyword can be used to indicate that a particular element of an object is static. However, the Java object itself will never be placed in static storage space.

2.5 constant Storage

The constant value is usually placed directly inside the program code. They will never change. Some constants need to be strictly protected, so consider placing them in read-only memory (ROM).

2.6 non-RAM storage

If the data is completely independent of a program, the program can still exist when it is not running and outside the control of the program.

The two most important examples are "streaming objects" and "fixed objects". For streaming objects, the object becomes a byte stream, which is usually sent to another machine. For fixed objects, objects are saved on disk. Even if the program is aborted, they can still keep their state intact. A particularly useful technique for these types of data stores is that they can exist in other media. Once needed, they can even be restored to normal, ram-based objects.

2.6.1 Main Types

There are a number of classes that need to be treated in a special way, and they can be imagined as "basic", "primary" or "primary" (Primitive) types, which are frequently used when programming.

2.6.2 An array of Java

Arrays are supported in almost all programming languages. It is very dangerous to use arrays in C and C + +, because those arrays are just blocks of memory. If a program accesses an array other than its own block of memory, or uses memory (which is a general programming error) before initialization, it can have unpredictable consequences. In C + +, you should try not to use arrays, in exchange for a more secure container in the Standard Template Library (templatelibrary).

One of the main design goals of Java is security. So many of the problems that plague programmers in C and C + + are not duplicated in Java. A Java can be guaranteed to be initialized and not accessible outside of its scope. Because the system automatically checks the scope, there is a certain price to pay: a small amount of memory overhead is incurred for each array and for checking the indexes during run time. The return is higher security, and higher productivity. It is worthwhile to pay a little price for it.

2.7 Do not clear objects

In most programming languages, the "time of Existence" (Lifetime) of variables has always been a problem that programmers need to focus on.

Most programming languages provide the concept of a scope. For a name defined in the scope, the scope also determines its "visibility" and "time of existence".

Java objects do not have the same time of existence as the primary type. When you create a Java object with the New keyword, it goes beyond scope.

In C + +, once the work is done, you must ensure that the object is cleared.

Java has a special "garbage collector" that looks up all the objects created with new and identifies which ones are no longer referenced. It then automatically frees the memory occupied by those idle objects so that it can be used by new objects. This means that we don't have to worry about the memory recycling problem at all. Simply create objects, and once they are no longer needed, they will automatically leave. Doing so prevents a programming problem that is common in C + +: "Memory Overflow" caused by programmers forgetting to release memory.

2.8 Data Type: Class

But historically, most object-oriented languages use the keyword "class" to express the meaning: "I'm going to tell you about the appearance of a new type of object." After this keyword, you should follow the name of the new data type. For example:

Class atypename{/* body is placed here}

This introduces a new type, which you can then use to create a new object of this type:

Atypename a =new atypename ();

In Atypename, a class body is made up of only one comment (the asterisk and slash and the contents of it, which are described later in this chapter), so you can't do too much of it. In fact, unless some method is defined for it, it cannot be instructed to do anything at all.

2.8.1 fields and Methods

You can set two types of elements in your own class: data members (sometimes called "fields") and member functions (usually called "methods")

A data member is an object (through its handle communicates with it) and can be of any type. It can also be one of the main types (not handles). If you are pointing to a handle to an object, you must initialize that handle, using a special function called "Builder" to connect it to an actual object (as with the new keyword).

But if you have a primary type, you can initialize it directly at the class definition location (as you'll see later, the handle can also be initialized at the defined location).

Each object retains storage space for its own data members, and data members are not shared between objects.

Once the variables are used as class members, pay special attention to the default values assigned by Java. This ensures that the member variables of the main type are definitely initialized (C + + does not have this capability) and can effectively suppress a variety of related programming errors.

2.9 methods, arguments, and return values

Java uses methods to replace the names of functions.

The return type refers to the type of numeric value returned after the method is called. Obviously, the role of a method name is to identify and reference a specific method. The argument list lists the type and name of the information you want to pass to the method.

Java's methods can only be created as part of a class. Only one method can be called on an object, and that object must be able to execute that method call. If you attempt to invoke the wrong method for an object, you will get an error message at compile time. When you invoke a method for an object, you need to list the name of the object, followed by a period after it, followed by the method name and its argument list. That is, the object name. Method Name (argument 1, Argument 2, Argument 3 ...).

Suppose we have a method called F (), which has no arguments and returns a value of type int. So, suppose you have an object named a, and you can call method F () for it, the code is as follows:

int x = A.F ();

The type of the return value must be compatible with the type of x.

The act of invoking a method like this is often called "sending a message to an object." In the above example, the message is F () and the object is a. Object-oriented programming is generally simply summed up as "sending messages to objects". In the above example, the message is F () and the object is a. Object-oriented programming is generally simply summed up as "sending messages to objects".

2.9.1 List of independent variables

The argument list specifies what information we transmit to the method. The object is used in the form of objects. We must specify the type of object to be passed and the name of each object in the argument list. Just as we are dealing with objects elsewhere in Java, we are actually passing "handles". If you want the argument to be a string, you must pass a string.

A program is simply a collection of objects whose methods use other objects as their own, and send messages to those objects.

2.10 Building Java Programs2.10.1 Names visible

In all programming languages, an unavoidable problem is the control of names or names.

How do you distinguish between two names and prevent two names from clashing with one another? C + + introduces the concept of a "namespace" with additional keywords. Java can avoid these problems altogether in order to generate explicit names for a library, using names similar to those of the Internet domain name.

2.10.2 using other components

Once a pre-defined class is used in its own program, the compiler must know how to find it.

The role of import is to instruct the compiler to import a "package"--or a "class library" (all Java code must be written to a class).

Most of the time, the components (parts) from the standard Java library are directly used, and they are provided with the compiler. There is no need to care about lengthy reserved domain names when using these components

2.10.3 Static keyword

Unless you create an object of that class with new, you don't actually get anything. The data storage space is not formally generated until new is executed, and the appropriate method can be used.

In two special cases, the above method is not used. One scenario is to use only one storage area to hold a specific data-no matter how many objects are created, or even if you do not create them at all. Another scenario is that we need a special method that is not associated with any object of this class. That is, even if you do not create an object, you need a method that can be called. To meet these two requirements, you can use the static (static) keyword. Once you set something to Static , the data or method will not be associated with any object instances of that class. so although an object of that class has never been created, you can still invoke a static method, or access some static data.

For non-static data and methods, we must create an object and use that object to access the data or method. This is because non-static data and methods must be aware of the specific objects they operate on. Of course, before formal use, because the static method does not need to create any objects, they cannot simply invoke other members without referencing a named object, thus directly accessing the non- Static member or method (because non-static members and methods must be associated with a particular object).

Some object-oriented languages use the terms "class data" and "class method". They mean that the data and methods exist only for the class as a whole, not for any particular object of that class.

One of the important uses of static is to help us invoke that method without having to create an object. As you'll see later, this is critical-especially when defining a program to run the Entry method main ().

As with any other method, the static method can also create a named object of its own type. Therefore, the static method is often used as a "leader" to generate a series of "instances" of its own type.

2.JAVA Programming Idea--everything is an object

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.