0

I want to know about the difference between Parameters, Arguments & Local Variables in JAVA. Can anyone explain me about them very well?

There is an unclear point in the following code for me. This is a part of a Java code.

Language(String t) {
    name = t;
}

and.....

Language() {
    String t;
    name = t;
}

I want to know the difference between above two types. What is the difference? What is the difference when running the code?

shmosel
  • 42,915
  • 5
  • 56
  • 120

2 Answers2

0

In addition to the fact that as commenters pointed out above your second code snippet cannot compile, parameters and arguments are essentially the same thing (t in your first example). They are variables passed to a method.

A local variable is a variable declared within a method, so in a fixed snippet:

public void myMethod(string t) {

    int x = 6;
    String y = t;
}

Here t is an argument (or parameter). y is a local variable.

CBass
  • 953
  • 6
  • 9
0

The difference between your two examples is that in the first example:

Language(String t) {
    name = t;
}
  • this example takes a parameter in the constructor (assuming Language() is a constructor by common naming conventions. A parameter is also called an argument. It's a value that is passed in from outside when this method/Constructor is called. 'name' is not defined anywhere in this example, so as it is, this code won't compile.

In your second example:

Language() {
    String t;
    name = t;
}
  • there is no argument/parameter defined
  • 't' is a local variable. Local means it's local within the scope of this block { } and therefore only has visibility inside this block
  • name again is not defined anywhere, so will not compile
  • 't' is never assigned a value and so is null
  • name if it was defined somewhere would be assigned null

The main difference therefore is that the first example takes a parameter and attempts to assign it to undefined 'name', whereas the second example does not take a parameter.

Kevin Hooke
  • 2,015
  • 1
  • 16
  • 31
  • 1
    *'t' is never assigned a value and so is null*. Incorrect. Local variables don't have defaults. – shmosel Jul 12 '17 at 21:01