Recently, I used the think in java I watched when I was off work. I felt that it was very different from the first time I watched it. Let’s talk about the relationship between objects and references in java, as well as the concept of internal classes.
1. Everything in java is an object
What is the operator object in Java? The answer is a quote, which is like a pointer in C or C++.
If you have a reference, you must associate it with an object at this time, otherwise the reference will not be left to your control as you think. For example, you create a String reference:
String s ;
It is not associated with any object at this time. If you do some operations at this time, such as calling some methods of String, there will definitely be problems (except some basic types, because when you define them, they will be assigned to the initial value), so you must associate money with the object when using it:
String s = new String();
or
String s = “my name is ajun”;
Just do it like this.
2. How to associate with objects
In Java, an object is usually created through new to associate with references, such as:
String s = new String("my name is ajun"); This not only creates an object to associate reference s, but also initializes it for colleagues, but also creates our own object type.
3. Storage location
(1) Stack: Generally, the stack stores references and basic type variables. The stack mainly allocates and releases memory by moving the stack pointer up and down.
Basic type variables are not suitable for new creation because they occupy less memory.
(2) Heap: used to store Java objects. When the program executes new, the heap will allocate a space to this object. Remember that the heap uses more time to allocate and release memory than the stack for storing and releasing memory. This is why basic type variables need to be stored on the stack, because basic type variables are used the most frequently, frequently store and release memory, and when consumed more, the performance can be imagined.
4. Internal class
(1) Basic knowledge of internal categories:
Generally, classes defined inside java classes become internal classes can be divided into: classes defined outside the method body, classes defined inside the method, and static internal classes (only defined outside the method), anonymous internal classes description:
Define the class outside the method:
The member variables of the class (static, non-static) can be accessed. In order to ensure that the member variables of the class can be referenced correctly, you must first instantiate the object of the external class before instantiating the object access permissions of the inner class. You can treat it as a member variable of the class, so you will understand it much more.
Classes defined in the body of the method;
The member variables of the class (static, non-static) can be accessed. In order to ensure that the member variables of the class can be referenced correctly, you must first instantiate the object of the external class before instantiating the object of the inner class cannot have access rights, and just treat it as a local variable of the method.
Static inner class:
Access to any anonymous inner class only by accessing static member variables of the class:
The member variables of the class (static, non-static) can be accessed. In order to ensure that the member variables of the class can be referenced correctly, it is necessary to instantiate the object of the external class before instantiation of the object of the inner class cannot have (2). The role of the inner class can hide the class very well. Generally, the class does not allow private protect default access.
Internal classes can implement multiple inheritance, making up for the characteristics that Java cannot inherit more (3), examples
package com.ajun.test.innerclass.example; /** * Fruit content* @author Administrator * */ public interface Contents { String value(); } package com.ajun.test.innerclass.example; /** * Fruit destination* @author Administrator * */ public interface Destination { // Destination String readLabel(); } package com.ajun.test.innerclass.example; public class Goods { private String des="is ruit!!"; // Method external private class Content implements Contents{ private String name = "apple "+des; @Override public String value() { return name; } } //External private class GDestination implements Destination{ private String label ; private GDestination(String label){ this.label= label; } @Override public String readLabel() { return label; } } //Anonymous internal class public Destination getdestination(final String label){ return new Destination(){ @Override public String readLabel() { return label; } }; } public Destination dest(String s){ return new GDestination(s); } public Contents content(){ return new Content(); } public Destination dest2(String s){ class GDestination implements Destination{ private String label; private GDestination(String label){ this.label= label; } @Override public String readLabel() { return label; } } return new GDestination(s); } } package com.ajun.test.innerclass.example; public class Test { public static void main(String [] a){ Goods gs = new Goods(); Contents c = gs.content(); Destination d = gs.dest("Beijing"); System.out.println(c.value()); System.out.println(d.readLabel()); Destination d1 = gs.getdestination("Shanghai"); System.out.println(d1.readLabel()); System.out.println(gs.dest2("Tianjin").readLabel()); } }Content and Gdestination are well hidden. When calling outside, you don’t know which class is called, so that this class has multiple inheritance features.
Output;
apple is ruit!! Beijing Shanghai Tianjin