Pay attention! You’re not taking the blame!

Q: Can a method have multiple variable-length arguments in Java?

A: A method can have only one variable length argument, and that variable length argument must be the last parameter of the method. Java does not allow a method to have multiple variable length arguments or variable length arguments that are not the last position of the method.

Q: Find out what is wrong with the following program and only allow modifications to call the relevant code to fix it?


      
  1. public class Demo {

  2.    public void print(String str. Integer. args) {}

  3.    public void print(String str. String. args) {}

  4. }

  5. / / call

  6. Demo demo = new Demo(a);

  7. demo.print("hello");

  8. demo.print("hello". null);

Copy the code

[bug Mc-108966] – variable length method calls can be made with null values or null values. [bug Mc-108966] – variable length method calls can be made with null values or null values.


      
  1. Demo demo = new Demo(a);

  2. String[] strs = null;

  3. demo.print("hello". strs);

Copy the code

Q: Is there a problem with the following program comment lines, and why?


      
  1. class Base {

  2.    void print(String. args) {

  3.        System.out.println("Base print.");

  4.    }

  5. }

  6. class Sub extends Base {

  7.    @Override

  8.    void print(String[] args) {

  9.        System.out.println("Sub print.");

  10.    }

  11. }

  12. Base base = new Sub(a);

  13. base.print("hello");    / / 1

  14. Sub sub = new Sub(a);

  15. sub.print("hello");    / / 2

Copy the code

A: Comment 1 will compile and print as Sub print. Because the base object transforms the subclass Sub up, the parameter list is determined by the parent class, so it will pass.

Note 2: The String type passed by the subclass does not match the String[] type required by the method. The compiler sees that the subclass overwrites the print method of the parent class. Although the argument list does not match, it does not match the parent class (because it does not match the parent class), so there is a type mismatch compilation error.

Print (Base) {print (args) {print (Base) {print (args) {print (Base) {print (Base) {print (Base) {print (Base) {print (Base) {print (Base); So it’s natural to Override, not Override, so there’s no problem with @override.

Master, don’t be too short, long you certainly won’t finish reading, so this is code farmers daily question purpose ~

After watching the sharing wave, it will be more interesting to discuss with your friends

Relax with a joke

Today noon before having a meal washroom, just saw the boss come out from inside, conveniently ask: “did you eat?”

Boss hey hey smile, did not answer.

I suddenly realized I had said the wrong thing.

Then go to the canteen to eat, the boss is also in the queue for rice, in order to make up for the fault, I bravely take the initiative to say hello: “haven’t eaten ah, thought you had eaten it.”

Take each man’s censure, but reserve your judgment.

Listen to the opinions of others, but reserve your own judgment.