#Is parameter a local variable

31 messages · Page 1 of 1 (latest)

oak sierra
#

I just had an interview on my code in a Java Introduction class. An the interviewer asked me, in this method which one is the local variable?

    public double lengthOfHypotenuse(double a, double b) {
        return Math.sqrt(a * a + b * b);
    }

I answered a and b.

He say I am wrong because they are parameters. But am I though? I went and google and see lots of websites that say parameters are local variables. Though in my class's Java resource it defines local variables as this.

Definition: Local variable – A variable that is defined within the body of a method and is accessible within that method only.

So who is correct and who is wrong?

raven steepleBOT
#

This post has been reserved for your question.

Hey @oak sierra! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

raven steepleBOT
hard eagle
#

depends on your definition of local variable

#

if it's something that's scoped locally, then parameters are local variables
if it's something that's declared in a method/constructor/init block body, then parameters aren't local variables. (parameters are declared in the method signature, not in the method body)

#

some places use the former definition, some use the latter

#

some languages use the former/latter by standard

#

it varies a lot

#

in practice it doesn't matter

#

i think the takeaway is that; that wasn't a useful question to ask

oak sierra
#

@hard eagle what do you mean by
"if it's something that's declared in a method/constructor/init block body, then parameters aren't local variables."

if its declared in a method, isnt that a local variable, but then it wouldn't be a parameter right?

hard eagle
#

if it's declared in a method body, it's a local variable and not a parameter

oak sierra
#

so i should ask my tutor about it right?

#

cause in java parameters are local variable?

hard eagle
oak sierra
#

@hard eaglein my class java resource they use this
Definition: Local variable – A variable that is defined within the body of a method and is accessible within that method only.

so can we say its a wrong definition?

hard eagle
#

afaik, at least

oak sierra
hard eagle
#

actually seems like the java spec does make a distinction between parameters and local variables

#

i mean makes sense in that context

#

different story as to whether developers themselves consider them completely separate things though

hard eagle
#

well, that one is making a distinction between them, so wouldn't be super helpful to your cause lol

#

I already closed it but you could try searching [java] parameter local variable in stack overflow

oak sierra
#

yeah i just realize that HAHA, but i figure it would still be helpful for my knowledge

#

okay thanks

raven steepleBOT
# oak sierra okay thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.