Home > Back-end >  Ask what's the difference between two kinds of generic methods.
Ask what's the difference between two kinds of generic methods.

Time:12-04

The following examples to look the same to achieve a goal,

 
Public class WildCardNeedDemo {

Public static void main (String [] args) {
GenericStack IntStack=new GenericStack<> (a);
IntStack. Push (1);
IntStack. Push (2);
IntStack. Push (2);
System. The out. Println (" is the Max number is: "+ max1 (intStack));
System. The out. Println (" is the Max number is: "+ max2 (intStack));
}

Public static & lt; Type extends Number> Double max1 (GenericStack Stack) {
Double Max=stack. Pop (). DoubleValue ();
while (! Stack. The isEmpty ()) {
Double value=(https://bbs.csdn.net/topics/stack.pop). DoubleValue ();
If (value & gt; Max) {
Max=value;
}
}
The return of Max;
}

Public static double max2 (GenericStack<? Extends Number> Stack) {
Double Max=stack. Pop (). DoubleValue ();
while (! Stack. The isEmpty ()) {
Double value=(https://bbs.csdn.net/topics/stack.pop). DoubleValue ();
If (value & gt; Max) {
Max=value;
}
}
The return of Max;
}
}

CodePudding user response:

In this case, you have no difference, because you don't use the type
If you change to the following, however, there are difference between
 public static & lt; Type extends Number> Type max1 (GenericStack Stack) {//the return value type to the type 
Double Max=stack. Pop (). DoubleValue ();
while (! Stack. The isEmpty ()) {
Double value=(https://bbs.csdn.net/topics/stack.pop). DoubleValue ();
If (value & gt; Max) {
Max=value;
}
}
The return stack. Pop ();
}

Public static Number max2 (GenericStack<? Extends Number> Stack) {//here you will find the return value of type cannot change to [?] , this is the difference between
Double Max=stack. Pop (). DoubleValue ();
while (! Stack. The isEmpty ()) {
Double value=(https://bbs.csdn.net/topics/stack.pop). DoubleValue ();
If (value & gt; Max) {
Max=value;
}
}
The return stack. Pop ();
}


CodePudding user response:

reference 1st floor qybao response:
in this case, you have no difference, because you don't use the type
If you change to the following, however, there are differences between


Hello, first post written in GenericStack is their class didn't post, is not conducive to discuss problems and write an example I ask again:

 
Public class question {

Public static void main (String [] args) {
ArrayList IntStack=new ArrayList<> (a);
IntStack. Add (1);
IntStack. Add (2);
IntStack. Add (4);
System. The out. Println (" is the Max number is: "+ max1 (intStack));

ArrayList IntStack2=new ArrayList<> (a);
IntStack2. Add (1);
IntStack2. Add (2);
IntStack2. Add (4);
System. The out. Println (" is the Max number is: "+ max2 (intStack2));
System. The out. Println (intStack2. The size ());
}

The static & lt; Type extends Number> Double max1 (ArrayList Stack) {
Double Max=stack. Remove (stack. The size () - 1). The doubleValue ();
while(! Stack. The isEmpty ()) {
A double value=https://bbs.csdn.net/topics/stack.remove (stack. The size () - 1). The doubleValue ();
If (value> Max) {
Max=value;
}
}
The return of Max;
}

The static double max2 (ArrayList<? Extends Number> Stack) {
Double Max=stack. Remove (stack. The size () - 1). The doubleValue ();
while(! Stack. The isEmpty ()) {
A double value=https://bbs.csdn.net/topics/stack.remove (stack. The size () - 1). The doubleValue ();
If (value> Max) {
Max=value;
}
}
The return of Max;
}
}

CodePudding user response:

Limit the scope of the different, the former parameters are the requirements for the return value is the same type, the latter is not necessarily, you can be introduced into digital, and then return any such as string or definition of object, etc.,

CodePudding user response:

It seems that you didn't understand 2 l example
Whether you use a custom type, or the type of system at, don't affect their difference between
Use your code to illustrate

 public class question {

Public static void main (String [] args) {
ArrayList IntStack=new ArrayList<> (a);
IntStack. Add (1);
IntStack. Add (2);
IntStack. Add (4);
System. The out. Println (" is the Max number is: "+ max1 (intStack));
The Integer max1=max1 (intStack);//the difference: there is no need to forcibly converting

ArrayList IntStack2=new ArrayList<> (a);
IntStack2. Add (1);
IntStack2. Add (2);
IntStack2. Add (4);
System. The out. Println (" is the Max number is: "+ max2 (intStack2));
System. The out. Println (intStack2. The size ());
The Integer max2=(Integer) max2 (intStack2);//forced conversion is needed here, because can't use? As the return value type
}

The static & lt; Type extends Number> Type max1 (ArrayList Stack) {//the return value can be directly used here type type
Type Max=stack. The get (0);
For (type t: stack) {//here can use type to the for loop
If (t.d oubleValue () & gt; Max. DoubleValue ()) {
Max=t;
}
}
The return of Max;
}

The static Number max2 (ArrayList<? Extends Number> Stack) {//return value cannot be used here? As a type
Number of Max=stack. The get (0);
For (Number n: stack) {//can't use here? To the for loop
If (n.d oubleValue () & gt; Max. DoubleValue ()) {
Max=n;
}
}
The return of Max;
}
}
  • Related