Java basics-object-oriented!

Source: Internet
Author: User

Object-oriented:

1. Three major features

Encapsulation is invisible to the outside. Can protect some programs in the program
Extended Functions
The overloaded object of the polymorphism method is polymorphism.
2. objects are stored in the stack memory, and properties are stored in the heap memory.

All methods in the program are saved in the global code area, and the content in this area is shared by all objects.
When using an object, the object must be instantiated before it can be used. (Instantiating an object is not just about
It is implemented through the New Keyword. As long as there is heap memory space pointing to it, it means the instantiation is successful .)

In the reference operation, if an object does not have a reference to the heap memory and the class's attributes or methods are called
Error. Java. Lang. nullpointerexception.

3. The so-called reference data type actually transfers the right to use the heap memory. You can define multiple stack memory reference operations for a heap memory space at the same time.

4. Do not call the constructor when declaring an object. The constructor is called only when an object is instantiated.

5 The so-called anonymous object is less of a reference relationship of stack memory than the previous object,

6 string comparison

public static void main(String[[ args){String str1 = "hello";String str2 = new String("hello");String str3 = str2;System.out.println("str1 == str2 ---->" + (str1 == str2));  //falseSystem.out.println("str1 == str3 ---->" + (str1 == str3));  //falseSystem.out.println("str2 == str3 ---->" + (str2 == str3));  //trues}

 

Now the ====determines whether the address space is equal. The address is determined.

7. Two string instantiation Methods

 String name = "hello"; String name = new String("hellow");

A string is actually an anonymous object of the string class.

public static void main(String[] args){System.out.println("hello".equals("hello"));}

In fact, string name = "hello ";
It indicates that the point of initializing a String object in a constant pool is given to the stack memory space.
Only after understanding this can we analyze the more appropriate instantiation method. Example:

Public static void main (string [[ARGs) {string str1 = "hello"; // value string str2 = "hello"; // value string str3 = "hello "; // directly assign the value system. out. println ("str1 = str2 ---->" + (str1 = str2); // truesystem. out. println ("str1 = str3 ---->" + (str1 = str3); // truesystem. out. println ("str2 = str3 ---->" + (str2 = str3); // true}

The memory addresses of the above three objects are the same.
Cause: directly assign values. Before assigning values. The program will first search for any character in the constant pool in the memory. If yes, it is the direct memory space to assign another object.

If not, it is to open up space. Therefore, you can save memory by directly assigning values. When new string () is used, two Memory Spaces are opened each time, and then the first one is released.

The two Memory Spaces should be "ABC" and an anonymous object, so a new object will be created in the constant pool. Then assign a value in the memory, and delete the value in the constant pool.
For example:

String str = new String("hello");

 

Analysis: first, the string "hello" is an anonymous String object. It will first open up a space in the constant pool. Then, we use new to open up a new memory space. The memory space of the previously Anonymous object will be reclaimed by the garbage collection mechanism. This is why new string is used to open up two spaces. Directly assigning values only requires one instantiated object. Using new string () means to open up two Memory Spaces. In development, it is best to use the direct Assignment Method for development.
8. The content of a string cannot be changed.

Public static void main (string [] ARGs) {string STR = "hello"; // open space STR = STR + "world "; // The "world" anonymous object opens up the space system. out. println ("str =" + Str); // "Hello World" to open up new space. disconnect STR to hello, // then point STR to Hello world .} // Finally, space for hello and world is reclaimed. Public static void main (string [] ARGs) {string str1 = "lixinghua"; for (INT I = 0; I <100; I ++) {str1 + = I ;} system. out. println (str1 );}

 

This operation can be implemented only when str1 is disconnected and the link is referenced for 100 times. Low performance.
If you must use it. Use the stringbuffer class. Specifically to complete such a function.

9. This keyword
1) indicates the attributes in the class.

Public Person (string name, int age) {// assign this value through the constructor. name = Name; // assign this to the name attribute in the class. age = age; // assign values to the age attribute in the class}

2) use this to call the constructor: if there are multiple constructor methods in a class, you can use this to call each other.

Private string name; // name private int age; // age public person () {// The system is constructed without parameters. out. println ("New Object Instantiation");} public person (string name) {This. name = Name; this (); // call the non-argument constructor in this class} public person (string name, int age) {// assign this (name) by the constructor ); // call the constructor of a parameter. This. age = age; // assign a value to the age attribute in the class} Public String getinfo () {// method for obtaining information \ this (); // call the construction method without parameters in this class return "name:" + name + ", age:" + age ;}

Notes
1) this () statements that call the constructor can only appear in the first line of the constructor.
2) When using this to call other constructor methods in this class. At least one constructor does not use this call.

3) use this to represent the current object.

Current object: the object that is currently calling the method.

Class person {// defines the person class public string getinfo () {// The Information Retrieval Method System. out. println ("person class -->" + this); // print thisreturn null directly; // return NULL to ensure correct syntax }}; public class thisdemo06 {public static void main (string ARGs []) {person Per1 = new person (); // call to construct the instantiated object person per2 = new person (); // call to construct the instantiated object system. out. println ("main method -->" + Per1); // print the object per1.getinfo () directly; // The object currently calling the getinfo () method is per1system. out. println ("main method -->" + per2); // print the object per2.getinfo () directly; // The object currently calling the getinfo () method is per2 }};

 

4 static keywords: If you want an attribute to be owned by all objects, you can declare it as static.
Attributes or methods declared as static are called class attributes or class methods. It can be called directly by class name.
1) use static to declare attributes
Each object has its own stack space. The heap memory stores the attributes of each object. But static
The attribute is saved in the global data area. All objects point to one content in the global data zone. So when I
After the object is modified, the content of all objects changes.

Memory area in Java
1) stack memory: the name of the object that can be saved (the address of the accessed heap memory)
2) heap memory: stores the specific attributes of each object.
3) Global Code area: Save static attributes.
4) Global Code area: Save the definition of all methods:
Generally, it is best to use the class name to call the static attribute directly. Use the class name. Attribute form:
2) use the static declaration method
If a method uses static Declaration, this method can be called directly by class name.

Note: The static method cannot be used to call non-static attributes or methods.

5. Object Array
Each element of the object array needs to be instantiated.

Stuend[] stu = new Student[100];for(int i = 0 ; i < stu.length ; i ++){stt[i] = new Student();}

 

6. Internal class
1) there is also a class within a class as the same internal class.

2) Access internal classes externally. Example:

Method 1:

Class outer {// defines the external class private string info = "Hello World"; // defines the Private Attribute Class inner of the external class {// defines the internal class public void print () {// define the internal class method system. out. println (Info); // directly access the private attributes of the external class}; Public void fun () {// define the method of the external class new inner (). print (); // call the method through the instantiation object of the internal class}; public class innerclassdemo04 {public static void main (string ARGs []) {outer out = new outer (); // external class instantiation object outer. inner in = out. new inner (); // instantiate the internal Class Object in. print (); // call the internal class method }};

 

Method 2: declare the internal class as a static class

Class outer {// defines the external class Private Static string info = "Hello World "; // define the private attribute of the external class static class inner {// use static to define the internal class as the external class public void print () {// define the internal class method system. out. println (Info); // directly access the private attributes of the external class}; Public void fun () {// define the method of the external class new inner (). print (); // call the method through the instantiation object of the internal class}; public class innerclassdemo03 {public static void main (string ARGs []) {New outer. inner (). print (); // call the fun () method of the external class }};

 

The internal class using static will be an external class.
3. Define the internal class in the method:
An internal class can be defined anywhere.

 

 

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.