I recently watched think in java when I was off work, and it feels very different from the first time I watched it again
Next, let’s talk about the relationship between objects and references in Java, and the concept of internal classes.
1. Everything in java is an object
What is the object that operates in java? The answer is a reference, 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 under your control as you imagine. For example, if 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, problems will definitely occur (except for some basic types, Because they will be assigned initial values when you define them), the money must be associated with the object when using it:
String s = new String();
or
String s = “my name is ajun”;
Just like this.
2. How to associate with an object
In java, an object is usually created through new to associate with a reference, such as:
String s = new String("my name is ajun")
This not only creates An object is associated with a reference s and initialized simultaneously. At the same time, we can also create our own object type.
3. Storage location
(1) Stack: Generally 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 creation with new because they occupy a small amount of memory.
(2) Heap: used to store java objects. When the program executes new, the heap will allocate a space to the object. Remember that the allocation and release of memory by the heap is more expensive than the storage and release of memory by the stack. This means that basic type variables need to be stored on the stack, because basic type variables are used most frequently, and memory is stored and released frequently. When more is consumed, the performance can be imagined.
4. Internal classes
(1) Basic knowledge of internal classes:
Generally, classes defined inside the java class become internal classes
Internal classes can be divided into: classes defined outside the method body, Define classes inside the method, static inner classes (can only be defined outside the method), anonymous inner classes
Note:
Classes defined outside the method:
Member variables of the class (static, non-static) can Access, in order to ensure that the member variables of the class can be correctly referenced, the object of the outer class must be instantiated first before the object of the inner class can be instantiated.
Access permissions can be any, and can be regarded as member variables of the class. This makes it much easier to understand.
Class defined in the method body;
The member variables (static and non-static) of the class can be accessed. In order to ensure that the member variables of the class can be correctly referenced, the object of the external class must be instantiated first. You cannot have access permission to the object of the instantiated inner class, just treat it as a local variable of the method.
Static inner class:
Only static member variables of the class can be accessed
Access permissions Any
Anonymous inner class:
Member variables (static, non-static) of the class can be accessed, in order to ensure that Correctly reference the member variables of the class, so the object of the external class must be instantiated first before the object of the internal class can be instantiated
Access permissions cannot be
(2), the role of the internal class
Internal Classes can hide classes very well. Generally, classes are not allowed to have private protect default access rights.
Inner classes can achieve multiple inheritance, which makes up for the fact that java cannot have multiple inheritance
(3), example
package com.ajun.test.innerclass.example; /** * 水果内容 * @author Administrator * */ public interface Contents { String value(); } package com.ajun.test.innerclass.example; /** * 水果目的地 * @author Administrator * */ public interface Destination { //目的地 String readLabel(); } package com.ajun.test.innerclass.example; public class Goods { private String des="is ruit!!"; //方法外部 private class Content implements Contents{ private String name = "apple "+des; @Override public String value() { return name; } } //方法外部 private class GDestination implements Destination{ private String label ; private GDestination(String label){ this.label= label; } @Override public String readLabel() { return label; } } //匿名内部类 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()); } }
apple is ruit!! Beijing Shanghai Tianjin