My thinking in Java Study Notes (1)

Source: Internet
Author: User
Tags float double
In fact, I have long wanted to write a learning note. Today, I finally made up my mind to write something that is not easy to write. Let me know! It is actually a simplified version of tij.

Chapter 1 Introduction to objects
In fact, this chapter introduces the idea of OOP and knows what abstract, interface, and inheritance are. In fact, I don't think it is necessary to write a specific chapter.
In future studies, these knowledge will be integrated.

Chapter 2 object of everything
In Java programs, everything is an object. Even the Java program itself is an object.
1. Reference is the key to manipulating objects
The C language uses pointers to manipulate objects, but what about Java? I use reference to manipulate objects. I often call it a handle. The relationship between a handle and an object is the same as that between a TV remote control and a TV, the remote control and the TV can appear independently.
Example: string name;
This Java statement will only generate a pie (Remote Control) without generating actual objects (TV sets). You just get the same name before your wife has given birth, the handle is not connected to the actual object. It is not recommended to write code in this way. When calling a handle that does not point to an object, the compiler will encounter an error and should use string name = "baby ";, or use string name = new string ("baby"); here new means to generate a string type object, and this string is called Baby. Both methods can implement object creation.
2. All objects need to be created by you
1. stored there
A. Registers are the core storage space, which we cannot manipulate.
B. STACK: the access speed and efficiency are high, because the stored data must have a specific size and survival time limit, and the elasticity is small, here, we usually store our object handle, and the object does not exist here.
C. heap is a general storage space. It is better than stack. The Compiler does not need to know the size of the data stored in heap, nor how long the space needs to be allocated, it is flexible, so it is used to store objects, but the speed is much slower than that of stack.
D. static storage: stores specific members declared as static. Java objects are not allocated here.
E. The constant storage space stores constants in the program. The constant value will not change and is the safest.
F. Non-ram stream or Persistent Object
2. Basic Data Types
Boolean char byte short int long float double void basic data type data is not created by new, int I = 0 directly, without using heap space, it is placed in the stack, fast! However, if you want to use heap to store basic data types, you need to use this type of overwrite class to implement integer I = new INTEGER ("0 ");
Note! String is not a basic data type. It is an object! You can see from his definition method!
3. Array)
The use and definition of arrays in Java are more secure than the arrays in C. When you define arrays, you actually generate an array that stores object handles, the value pointed to by each handle is set to null, that is, it does not point to any object.
3. You do not need to destroy objects
1. Survival scope of basic types
In Java, the survival scope of the basic types is determined by a pair of braces. variables defined in the living space can only be used before the end of the living space.
{
Int I = 1;
{
Int I = 100; // error! This definition is always not allowed in Java! The compiler will think that I has been defined
}
}
2. Survival scope of Objects
The lifetime of an object is different from the basic type. When you use new to generate an object, even if you leave the braces, the object still exists.
{
String name = new Sting ("baby ");
}
The handle name disappears out of the living space of the braces, but the string object to which it points is still occupying the memory, but you will think that a large number of useless objects will occupy a large amount of memory. How can this problem be solved in Java? It uses the garbage collection mechanism. The Garbage Collector checks the objects created with new at a specific time. If these objects have no handles pointing to them, then, it clears useless objects.
4. Create a new data type class
In Java, since everything is an object, what limits the attributes of an object? Use a class. Use the class keyword in Java to define a class.
For example, class women {// class body} defines a women class. Of course, this class does not have any attributes.
1. Data members and Methods
There are two members in a class, one is a data member and the other is a method. A data member can be a basic data type or an object, and the system automatically assigns an initial value to the member when the basic data type is declared.
5. Methods, parameters, and returned values
A valid method includes the name, parameter, return type, and method body, for example
Void name (int I)
{
Return;
} Among them, the name () parameter int I returns the void method body {}. For a class, the name + parameter combination must be unique and the parameter can be empty, if an object named test allows you to call the gettest () method whose return value is string, string name = test. the gettest (); Name type must be the same as the return value type.
When an object is passed to a method, the handle of the object is actually passed (except for the basic data type ), the passed object type must be the same as the parameter type accepted by the method. When you do not need a method to return something to you, you can set the return type of this method to void. At this time, the return in the method is used to leave the method, you do not need to wait until the execution is complete. If the return type of the method is not void, you can use return to return a value of the same type as the return type.

Not complete .......

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.