


Understanding of objects and references and inner classes in object-oriented programming in Java
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()); } }Among them, Content and Gdestination are obtained It is well hidden. When calling from the outside, you don't know which specific class is being called, making this class have the feature of multiple inheritance.
apple is ruit!! Beijing Shanghai TianjinFor more articles related to the understanding of objects and references and internal classes in Java’s object-oriented programming, please pay attention to the PHP Chinese website!

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.

The main challenges facing creating a JVM on a new platform include hardware compatibility, operating system compatibility, and performance optimization. 1. Hardware compatibility: It is necessary to ensure that the JVM can correctly use the processor instruction set of the new platform, such as RISC-V. 2. Operating system compatibility: The JVM needs to correctly call the system API of the new platform, such as Linux. 3. Performance optimization: Performance testing and tuning are required, and the garbage collection strategy is adjusted to adapt to the memory characteristics of the new platform.

JavaFXeffectivelyaddressesplatforminconsistenciesinGUIdevelopmentbyusingaplatform-agnosticscenegraphandCSSstyling.1)Itabstractsplatformspecificsthroughascenegraph,ensuringconsistentrenderingacrossWindows,macOS,andLinux.2)CSSstylingallowsforfine-tunin

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
