Why does arguments in the methods are initialized with their default value in Java?

In Java, local variables are not initialized by the compiler. Then, if I try to print the value of an argument without passing any value in it, then it prints their default value. Why is that?


6 Answers
1-6 of  6
6 Answers
  • All  the class level variables(whether arguments) have some default value whereas all local variables must be initialised (else it is compile time error).

  • If you try to call a parameterized method without any parameters it will give you a compilation error. You compulsorily have to provide the required parameters to the method.

  • As far as i understand your question, you are trying to print the value of the variable which you have given in your mehod....see there is a rule of java that compiler can give garbage or default value to the variable at the class level but not at the method(function) level...so if you have declared the same variables at class level and doesnt declared those in method then it take those default value....otherwise there will be a compilation error...and if you are talking like you have passed no arguement and u declared like void xyz(int a) and you haven't passed any value then also it shows compilation error...

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here