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 Java uses Object in overloading

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how Java uses Object in overloading, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Using Object in overloading

When overloaded methods are called in JAVA, they always match the same type of parameters first. If not, they will be transformed upwards to match the parameters.

Example:

Public void remove (int I) {...} public void remove (Object object) {...} int I = 0; Integer it = 0; remove (I); / / called the remove (int I) method remove (it); / / called the remove (Object object) method encountered two methods public boolean lSet (List value) {...} public boolean lSet (Object) {.}

When called:

List list = new ArrayList (); list.add (1); list.add (2); lSet (list); / / lSet (Object) is called

Called lSet (Object) instead of lSet (List value)

Didn't you say to match the same type first?

Notice that the parameter List value List in the overloaded method specifies the generic Object, but the List passed in when called is not of the same type at this time.

Solution / / use? Use of wildcards or directly not specifying generic public boolean lSet (List value) {...} Object: overloading equals, hashCode, and implementing compareTo

This document mainly describes how to deal with key values involving objects and classes when using Hashtable and Arrays.sort in Java:

1. Overload equals (): the default equals of an object in java is true; when pointing to the same memory address. If you now need to use the values in the object to determine whether it is equal, overload the equal method.

two。 Overload hashCode (): hashCode is overloaded only when the class needs to be placed in a collection of hash structures such as HashTable, HashMap, HashSet, and so on. Reason: as far as HashMap is concerned, HashMap is a large memory block with many small memory blocks and a series of objects in it. You can use hashCode to find small memory blocks hashCode%size (number of small memory blocks), so when equal is equal, hashCode must be equal, and if it is object objects The hashCode and equal methods must be overloaded.

3. Implementation interface Comparable: when you need to call a function such as sort () to use the basic operation of object comparison, you need to implement the compareTo (Object arg0) of Object.

Note when using 4.binarySearch: since there is no range of lookup array given in Arrays.binarySearch (Object array [], Object key), according to my estimate, it may be 0 to length-1, so it is best that the array can be filled with objects. If the array is partially empty (the array is opened too large), it will make an error. And call the sort function to sort it before searching. Because the array length is the same as the number of objects, sort (Object []) sorts them all without a given range.

Here is a simple example of public class TermPOS implements Comparable {public String term; public String pos; public TermPOS (String a dint string b) {term = a; pos = b } / / for the custom size when calling Arrays.sort (Object []), when the class is followed by implements Comparable public int compareTo (Object arg0) {/ / TODO automatic generation method stub if (this.term.compareTo (TermPOS) arg0) .term)! = 0) return this.term.compareTo (TermPOS) arg0) .term); return this.pos.compareTo (TermPOS) arg0) .pos) } / / hashCode public int hashCode () {return term.hashCode () * pos.hashCode () is overloaded when the class needs to be placed in a collection of HashTable, HashMap, HashSet, and so on hash structures. } / / if you now need to use the values in the object to determine whether they are equal, overload the equal method public boolean equals (Object obj) {if (term.compareTo (TermPOS) obj) .term)! = 0) return false; if (pos.compareTo (TermPOS) obj) .pos)! = 0) return false; return true } public static void testHashtable () {Hashtable t = new Hashtable (); TermPOS x = new TermPOS ("a", "b"); t.put (new TermPOS ("a", "b"), 2); if (t.get (x) = = null) System.out.println ("wrong!"); / / output this else System.out.println (t.get (x)) when the rewrite of hashCode is removed System.out.println (x.equals (new TermPOS ("a", "b"));} public static void testSort () {TermPOS tp [] = new TermPOS [3]; tp [0] = new TermPOS ("b", "c"); tp [1] = new TermPOS ("a", "c"); tp [2] = new TermPOS ("a", "b") Arrays.sort (tp,0,3); for (int I = 0 args I < 3 X I + +) System.out.println (TP [I] .term + "\ t" + TP [I] .pos);} / * * @ param args * @ throws IOException * / public static void main (String [] args) throws IOException {/ / TODO automatic generation method stub testHashtable () TestSort ();}} these are all the contents of the article "how Java uses Object in overloading". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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