1

I'm learning Java these weeks.

Here is a function code for my course:

public class MethodCall
{
   public static void main(String[] args)
   {
      int a = 5, b = 7;
      int m = min(a, b);
      System.out.println("Minimum is "+ m);
   }

   public static int min(int c, int d)
   {
      int m2;
      if (c < d)
           m2 = c;
      else
           m2 = d;

      return m2;
   }
}

Why do I need to use extra variables: c and d?

Why not to write this code like this:

public class MethodCall
{
    public static void main(String[] args)
    {
        int a = 5, b = 7;
        int m = min(a, b);
        System.out.println("Minimum is "+ m);
    }

    public static int min(int a, int b)
    {
        int m2;
        if (a < b)
            m2 = a;
        

etc.

?

The result is the same and the code isn't redundant, is it?

  • 5
    You just changed their name. They're still different variables, since they are in a different scope. – Federico klez Culloca Sep 23 '22 at 14:59
  • You can name your variables whatever you like. Did you *try* to make your changes? Did something not work? They aren't "extra variables", changing their names doesn't remove them. – David Sep 23 '22 at 15:00
  • You can use any name for them you want. The course probably used different names to make it easy and clear when referring to them in the text. – T.J. Crowder Sep 23 '22 at 15:00
  • 2
    Changing variable names only changes how readable code is to a human being. It changes nothing about how the code is actually executed. – OH GOD SPIDERS Sep 23 '22 at 15:01
  • "the code isn't redundant, is it?" <- The code is neither "redundant" in the first nor the second version. You seem confused and probably think giving your method parameters the same name as some other variable somehow makes them related... This is not the case. Both versions you posted are absolutely identical. – OH GOD SPIDERS Sep 23 '22 at 15:03
  • 1
    the parameter of a method work like a local variable of that method and is not related to the variables of any other block (simple test: do `a = 99` inside the `min()` method, the variables in `main()` will not be changed) – user16320675 Sep 23 '22 at 15:03
  • 1
    Regarding the update... *"The result is the same"* - Because no logic was changed. All you did was re-name a couple of variables. The code still does the same thing. *"the code isn't redundant, is it?"* - Define "redundant" in this case. Since your variable names are single letters with no contextual/semantic meaning then re-using the same letter **could** be considered making the code less clear. Why not give **meaningful** names to your variables? – David Sep 23 '22 at 15:03
  • 3
    I think your confusion might be this: The `a` and `b` **variables** in `main` are *not* the same as your `a` and `b` **parameters** to the `min` function (originally `c` and `d`). They're completely different things that have the same names (in your version). There is nothing more complex about the original vs. your version (arguably, for a teaching example, the original is simpler for the reason I mentioned above: giving distinct things different names). – T.J. Crowder Sep 23 '22 at 15:07
  • Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – f1sh Sep 23 '22 at 15:09
  • also: https://stackoverflow.com/questions/38177140/what-is-scope-in-java – f1sh Sep 23 '22 at 15:09

0 Answers0