Encapsulation is to hide the object’s information inside the object and prohibit external programs from directly accessing the properties and methods inside the object.
1. Basic types can only be passed by value, and the encapsulation class corresponding to each basic type is passed by reference.
2. In terms of performance, basic types in Java are created on the stack, and all object types are created on the heap (references to objects are created on the stack). For example, Integer i=new Integer(10); where new Integer() is created on the heap, and its reference Integer i is on the stack. The emergence of encapsulated classes is to make it more convenient to use some methods that are not available in basic types, such as valueOf(), toString(), etc. Also, if you want to pass a reference to an int object instead of a value, you can only use a wrapper class.
The calling efficiency of allocating memory on the stack is much different from that of allocating memory on the heap. Although allocating memory on the stack is efficient, allocating memory on the stack has the problem of memory leaks. (This is a problem that mediocre programmers basically cannot solve...) Java uses a very genius method to improve the efficiency of allocating memory on the heap. Despite this, Java is still slow. It's unlikely that he'll be as fast as C, although he's been promising that one day virtual machines will be as fast as machine code.
JDK5.0 can automatically package, that is, basic data can be automatically packaged into encapsulated classes. The advantage of basic data types is that they are fast (it does not involve the construction and recycling of objects). The purpose of encapsulated classes is mainly to be better There are many methods for processing data conversion, and they are easy to use.
Of course, the transfer of encapsulated types is by reference. For example,
Integer a = new Integer(1);
means that an Integer type reference a refers to a piece of memory, and the data in this memory is 1; and what is stored in a is this The reference (address) of block memory. When passing a to other methods or objects, the reference of a is passed.
Conversion between types:
String b = "123456"; int c = Integer.parseInt(b);
means converting the string 123456 into an integer number, where parseInt is a static method and can be used directly
Another point is that in some cases, it is necessary to use When it comes to encapsulated classes, such as a collection List, it can only add objects to it, that is, Object, so directly storing numbers is definitely not possible. You need to encapsulate the numbers into encapsulated type objects and then store them in the List, such as
List list = new ArrayList(); list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(4));
JDK5.0 can automatically seal packets in the future, so it can be abbreviated as
List list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(4);
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
JavaScript to obtain the city and geographical location of the user
The relationship between JavaScript calling mode and this keyword binding
The relationship between JavaScript calling mode and this keyword binding
The above is the detailed content of Implementation steps of encapsulation in java. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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






