Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample analysis of Java Object class and wrapper class

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of sample analysis of Java Object classes and wrapper classes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

View source code method

Double-click the shift key in IDEA to search for related class names and view the source code

Click Structure, and the part on the left framed with an orange bed is the structure of the class, which contains all the methods, properties, and declarations of the inner class. The icon with a small green lock in the structure indicates the public permission, the key icon is the inherited permission (protected), and the icon of the red lock is the private permission. On the right is the source code.

Definition and Application of Object Class

The Object class is the default parent of all classes, and there is no need to use extends to display inheritance, so all methods of the Object class (except private) can be used by objects of all classes. So all objects can be received through Object

Example:

Object obj1 = "test"; Object obj2 = new Student ("Xiao Chen", 18); Object obj3 = 123

The Object class is extended in Java so that it is not only the parent class of all classes, but can be transformed upwards to Object classes as long as it is a reference data type, including arrays and interfaces, that is to say, the Object class is the highest parameter unification of reference data types.

Next, let's look at two common methods in the Object class.

1. ToString ()

Convert any object into a string output and print the object content. System.out.println () receives any object and outputs it, which is the toString method called by default

Run the following code:

Class Student {private String name; private int age; public Student (String name,int age) {this.name = name; this.age = age;}} public class ObjectTest {public static void main (String [] args) {Student S1 = new Student ("Zhang San", 17); System.out.println (S1);}}

Running result:

We can see that it is not the result we expected. Why? Let's take a look at the source code of toString ()

To output the specific content at this point, you need to override the toString () method:

Public String toString () {return "Student {" + "name='" + name +'\'+ ", age=" + age +'}';}

/ / output: Student {name=' Zhang San', age=17}

2. Equals ()

Compare whether two objects are equal, usually compare properties, follow the above example, let's create a new object and compare S1:

Student S1 = new Student ("Zhang San", 17); Student S2 = new Student ("Zhang San", 17); System.out.println (s1.equals (S2))

/ / output: false

S1 and S2 have the same name and age, so why is the output false? Let's look at the source code of the equals method

Where there is new, there is a new object, so S1 and S2 are obviously not the same address. At this point, the Student class defaults to the equals method provided by the Object class. For the Object class, it does not know what properties the subclass has, so it compares the address of the object.

If we want to compare attribute values, we override the equals method:

Public boolean equals (Object obj) {/ / compare the current object with the incoming obj / / 1. Determine whether obj is empty if (obj = = null) {return false;} / / 2. Determine whether obj is itself, that is, the same address if (obj = = this) {return true;} / / 3. Determine whether obj is an object of this class, such as passing in a string, which is not of type Student and cannot compare to if (! (obj instanceof Object)) {return false;} / / 4. At this point, obj must be the object of the Student class, and the two objects are not the same address / / the downward transformation is restored to Student. Compare the object properties Student stu = (Student) obj; return this.name.equals (stu.name) & & this.age = = stu.age;}}

Overriding the equals method is very important! Be sure to master, the code applied to the instanceof and downward transformation of the knowledge links are as follows, interested can take a look at oh

Detailed explanation of Java object-oriented programming Polymorphism

Definition and Application of Packaging Class

A wrapper class encapsulates eight data types into classes:

Object wrapper class (direct subclass of Object Ray)

Boolean 、 Character

Numerical wrapper class (direct subclass of Number class)

Byte 、 Short 、 Integer 、 Long 、 Float 、 Double

Take the Integer class as an example:

Public static void main (String [] args) {/ / take the int-> class Integer data = new Integer (10); / / value is to extract the contents of the wrapper class int a = data.intValue (); System.out.println (a);}

/ / output: 10

Packing and unpacking

Boxing: changing a basic type into a wrapper class object

Unboxing: restore the values in the wrapper class object to the basic type

In the above example:

In our actual operation, it is very troublesome to unpack repeatedly. Automatic packing and unpacking are provided for us in Java.

Code example:

Public static void main (String [] args) {Integer a = 20; int b = axi2; System.out.println (b);}

/ / output: 22

As you can see, an is an object of the Integer class, but it can operate directly with integer data. In fact, Integer a = 20; it is automatically boxed, and int b = astat2; it is the process of automatic unpacking. So in usage, there is no difference between the wrapper class and the basic type. But keep in mind that the default value for the basic data type is 0 and the default value for the wrapper class is null

So when do we use wrapper classes and when do we use basic data types? The Ali coding protocol provides as follows:

To define member variables in a class, you must use the wrapper class to declare

In the method, a large number of arithmetic operations use basic types

Implement the wrapper class public class IntDemo {/ / save the specific integer value private int data; public IntDemo (int data) {this.data = data;} public int intValue () {return this.data;} public static void main (String [] args) {/ / int-> class IntDemo demo = new IntDemo (10); int data = demo.intValue () System.out.println (data+1);}}

/ / output result: 11

Let the Object class receive the basic data type through the wrapper class

We already know that the Object class can accept all reference types, but it doesn't work when it comes to basic data types, and the wrapper class perfectly solves this problem.

Using the Object class to receive, modify the main method of the above example as follows:

Public static void main (String [] args) {/ / int-> class IntDemo demo = new IntDemo (10); Object obj = demo; IntDemo demo1 = (IntDemo) obj; int data = demo1.intValue (); System.out.println (data+1);}

/ / output: 11

Object comparison of wrapper class

Unified use of the equals method

Integer a = 10; Integer b = 10; System.out.println (a. Equals (b))

/ / output: true

Conversion between wrapper class and String

① wrapper class-- > String: use String.valueOf ()

② String-- > wrapper class: use the wrapper class's parse*** ()

Code implementation:

Public static void main (String [] args) {/ / Integer-> String Integer x = new Integer; String y = String.valueOf (x); System.out.println (y); / / String- > Integer String str = "123"; Integer a = Integer.parseInt (str); System.out.println (a);}

/ / output result:

two hundred

one hundred and twenty three

Note that during String conversion, if the string is not composed of pure numbers, a type conversion exception occurs at run time.

Thank you for reading! This is the end of this article on "sample analysis of Java Object classes and wrapper classes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report