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

How to use the ancestor class Object in Java

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

Share

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

This article mainly introduces "how to use the ancestor class Object in Java". In the daily operation, I believe that many people have doubts about how to use the ancestor class Object in Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use the ancestor class Object in Java". Next, please follow the editor to study!

I. brief introduction

As mentioned earlier, the Object class, as one of the most commonly used built-in wrapper classes in Java, is the parent of all Java classes, and we often call it ancestral classes to highlight its high status. If a class does not specify a parent class, its default parent class is the Object class. Therefore, the following two pieces of code are of the same nature:

Public class Myee {...} public class Myee extends Object {...}

Because all classes in Java are subclasses of the Object class, you can call all methods in that class

2. ToString () method

This method literally, to is to get, get, String is recognized as a string symbol, so together, this method is the method of getting strings. The official explanation is the string that returns the object.

ToString () also has a more common way, but not directly used. Normally, when we connect a string to an object with a concatenation symbol (+), the system will call the object's toString () method to return a string by default.

The toString () method in the Object class returns a runtime class name @ hexadecimal hash, such as:

/ / defines the Demo class, which actually inherits the Object class class Demo {} public class ObjectDemo01 (String [] args) {Demo d = new Demo (); / / instantiates the Demo object System.out.println ("output without toString ():" + d); System.out.println ("plus toString () output:" + d.toString ());}}

Output result:

Output without toString (): Demo@15db9742

Plus toString () output: Demo@15db9742

As can be seen from the above output, whether you add the toString () method or not, the output is the same.

Therefore, many classes generally override the toString () method.

Public class Person {private String name; private int age; public Person (String name, int age) {this.name = name; this.age = age;} public String toString () {return "name:" + this.name + ": age + this.age;} public static void main (String [] args) {Person per = new Person (" Xiaoming ", 30) / / instantiate Person object System.out.println ("object Information:" + per); / / print object call toString () method}}

Output result:

Object information: name: Xiao Ming: age 30

The above code overrides the toString () method so that the method that has been overridden by the subclass is called when the object is output directly.

3. Equals () method

When we learn about string comparison, we have learned two comparison methods, one is the = = comparison operator, and the other is the equals () method that we will talk about next.

What is the difference between the two ways of comparison? The = = comparison operator compares whether the two variables point to the same instance, while the value of the equals () method only compares whether the contents of the two objects are consistent. Generally speaking, a string is generally concerned only with whether the content is the same.

Use format:

Boolean result = obj.equals (Object o)

Where obj represents the object to be compared and o represents the object to be compared.

Next I'll show the use of the equals () method with a specific code example:

Scenario requirements: write a Java program to implement the user login authentication function. The user is required to enter the account and password from the keyboard, and if the account and password are the same as the specified content, the login will be displayed successfully; otherwise, the login failure will be displayed.

Import java.util.Scanner;public class Test01 {/ / verify username and password public static boolean validateLogin (String uname, String upwd) {boolean con = false; if (uname.equals ("admin") & & upwd.equals ("admin") {/ / compare two String objects con = true;} else {con = false;} return con } public static void main (String [] args) {Scanner input = new Scanner (System.in); System.out.println ("- Welcome to big data management platform -"); System.out.println ("user name:"); String username = input.next (); / / get the user name System.out.println ("password:") entered by the user. String pwd = input.next (); / / get the password entered by the user boolean con = validateLogin (username, pwd); if (con) {System.out.println ("login successful!") ;} else {System.out.println ("incorrect user name or password!") ;}

Failure shows:

Welcome to big data management platform-

User name:

Adimn

Password:

Admin

The user name or password is incorrect!

Successfully displayed:

Welcome to big data management platform-

User name:

Admin

Password:

Admin

Landing is successful! 4. GetClass () method

From the literal point of view of this method, get gets, gets; Class knows it is a class. So this method is literally the way to get the class name. The official interpretation is that the class to which the current object belongs is a Class object. Through this Class object, you can get all the information about the class, including the name of the class, the parent class, the interface it implements, and so on.

Let's demonstrate it with a code example:

Public class Test02 {public static void printClassInfo (Object obj) {/ / get the class name System.out.println ("class name:" + obj.getClass (). GetName ()); / / get the parent class name System.out.println ("parent class:" + obj.getClass (). GetSuperclass (). GetName ()); System.out.println ("the implemented interfaces are:") / / get the implemented interface and output for (int I = 0; I < obj.getClass (). GetInterfaces (). Length; iTunes +) {System.out.println (obj.getClass (). GetInterfaces () [I]);}} public static void main (String [] args) {String strObj = new String (); printClassInfo (strObj);}}

Running result:

Class name: java.lang.String parent class: java.lang.Object interface: interface java.io.Serializableinterface java.lang.Comparableinterface java.lang.CharSequence so far, on the "ancestral class Object in Java how to use" the end of the study, I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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