Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's the difference between the following two Java lines?

I was given two of these line of code. What are the differences between two of these and which one is better for simple program.

public static < E > void swap(List< E > list, int i, int j);

vs

public static void swap(List<?> list, int i, int j);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public static < E > void swap(List< E > list, int i, int j);

Depends on generic class, sample

public class Test<E> {

  public <E> swap(List<E> list, int i, int j) {
    // your code
  }
}

public class ExtendTest<Person> {

  public Person swap(List<Person> list, int i, int j) {
    // your code
  }
}
public static void swap(List<?> list, int i, int j);

Can use with non-generic class, sample:

public class Test {

  public List<?> swap(List<?> list, int i, int j) {
    // your code
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading