2.JAVA programming Idea--everything is an object

Source: Internet
Author: User
Tags class definition

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 be used in a unified syntax. No matter where you can copy the Error. Note that while 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.

Often use Newkeyword to achieve This.

When the program executes, it is particularly important to note the allocation of memory.

There are six places where data can be saved

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 register is based on the need to be allocated by the Compiler. 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. however, the direct support for processing can be obtained through its "stack pointer".

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 the data saved in the STACK. This is because it must generate the corresponding 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 put IN.

2.3 Stacks

A general-purpose memory pool (also in the Ram area). Java objects are Saved. is different from the STACK. 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 ask for an object to be created, you can simply compile the relevant code with the new Command. When you run the code, you take the initiative to save the data 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 program Execution. Data that is stored statically is always waiting to be called. You can use static keyword 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 a 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 executed and outside the control of the Program.

Two of the most basic examples are "streaming objects" and "fixed objects." For streaming objects, the object becomes a stream of bytes and is typically sent to a machine. and for fixed Objects. The object is saved on Disk. Even if the program aborts Execution. They can still keep their state intact. A particularly practical technique for these types of data storage 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

Almost all programming languages support Arrays.

Using arrays in C and C + + is critical, because those arrays are just blocks of memory. If a program visits an array other than a block of memory, or uses memory (which is a general programming error) prior to initialization, it can have unpredictable consequences. In C + +. You should try not to use arrays for safer containers in standard template libraries (templatelibrary).

One of the main design goals of Java is Security. So many of the problems that plague the program apes in C and C + + are not repeated in Java. A Java is guaranteed to be initialized. and cannot be interviewed outside of its scope.

Because the system proactively carries out scope checks. So there must be some price to pay: for each array. And the checksum of the index during execution can result in a small amount of memory overhead.

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" of variables (Lifetime) has always been a problem that the program apes need to focus On.

Most programming languages provide the concept of a scope.

For the name defined in the Scope. The scope at the same time 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 new keyword, it goes beyond Scope.

In C + +. Once the work is complete, 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.

Then. It will voluntarily release the memory occupied by those idle Objects. So that it can be used by new Objects. This means that we do not have to worry about the memory recycling issue at All. Simply create objects, and once they are no longer needed, they will leave on their own initiative. Doing so prevents a common programming problem in C + +: because the program ape forgets to release memory for "memory overflow."

2.8 Data Type: class

But historically, most object-oriented languages use keyword "class" to express the idea that "i'm going to tell you what a new type of object looks like". In the back of this Keyword. The name of the new data type should be Followed.

Like what:

Class atypename{/* body is placed here}

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

Atypename a =new atypename ();

In atypename, the body of a class is made up of only one gaze (asterisks and slashes, and the contents of Them. This chapter will be described later), so there is no way to do too much of it. In fact. Unless some methods have been defined for it. Otherwise it simply cannot be instructed to do whatever it is.

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), capable of whatever type. It can also be one of the main types (not handles).

Assuming that 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 if using newkeyword).

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 variable is used as a class Member. Pay special attention to the default values assigned by Java.

Doing so ensures that the member variables of the main type are definitely initialized (c + + does not have this capability). 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 the method name is to identify and reference the detailed Method. The argument list lists the type and name of the information you want to pass to the Method.

Java's approach 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 run that method Call. If you try to invoke the wrong method for an Object. An error message is received at compile Time.

When you invoke a method for an object, you need to list the name of the object First. Follow a period followed by the method name and its list of References. That is, the object Name. method name (argument 1, argument 2, argument 3 ...).

If we have a method called F (), it has no arguments and returns a value of type int. so, If you have an object named a, you can call method F () for it, The code is for example the Following:

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 example above, the message is F () and the object is A.

Object-oriented programming is generally simply summed up as "sending messages to objects".

In the example above, 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.

It 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 you do with objects elsewhere in Java. What we actually pass is the Handle. If you want the argument to be a string, you must pass a string.

A program is simply a collection of objects, and their methods use other objects as their own arguments. 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 extra Keyword. Java in order to generate a clear name for a library. Uses a name similar to the Internet domain Name. So you can avoid these problems altogether.

2.10.2 using other components

Once you want to use a predefined class in your 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. Direct use of components (parts) from standard Java libraries is available, and they are provided with Compilers.

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. Only after you have run new will you formally generate the data storage space and use the corresponding method.

In two special cases, the above method is not Used. one scenario is simply to use a storage area to hold a specific data-no matter how many objects are Created. Do not even create objects at All.

Another situation is that we need a special method that does not relate to 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 the requirements of these two aspects. You can use static (static) Keyword.

Once you set something to Static , the data or method will not be associated with whatever object instance of that class. So while you never create an object of that class, 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 data or METHODS. This is because non-static data and methods must know the detailed object of their operation. of course, before formal use, because the static method does not need to create whatever object. So they cannot simply invoke other members, not referencing a named object at the same time, and thus direct access to non-static members or methods (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 data and methods exist only for the class as a whole, not for whatever particular object that class is.

One of the important uses of static is to help us invoke that method without having to create an object.

As you will see later, this is critical-especially when defining the procedure to execute the entry method main ().

As with any other method, the static method can also create its own type of named Object.

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.