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!