2

I had to make a program which was able to print symbols based off an array with numbers, with the numbers corresponding to the amount of symbols to print.

I got this to work; here is the code:

class printChart {
  int[] values;

  void barChart(int[] values){
    for (int i=0;i<values.length;i++){
      int number = values[i];
      for (int j=0;j<number;j++){
        System.out.print("*");
      }
      System.out.println();
    }
  }

  void demo(){
    barChart(new int[] {2,5,0,4,3});
  }
  public static void main(String[] args){
    new printChart().demo();
  }
}

My question is as follows: How does Java know that the {2,5,0,4,3} array should be assigned to the variable values? I'm assuming it's because I set void barChart to pass along int[] values, but I'd like to know more about the inner workings and what's going on exactly.

Rob
  • 61
  • 9
  • 1
    Reference of array creating using `new int[] {2,5,0,4,3}` is passed to `values` parameter of `barChart(int[] values)` method. – Raman Shrivastava Jan 12 '16 at 12:38
  • You do know that the `values` array inside the `barChart` method is not the same as the variable `values` you created above, correct? – krillgar Jan 12 '16 at 12:39
  • krillgar, I did not know that. Which is way I'm interested in the inner workings of Java instead of only typing stuff. What is the difference between the 2? – Rob Jan 12 '16 at 12:41
  • This answer to a closed question might help you: http://stackoverflow.com/a/20671118/2094953 – MrHug Jan 12 '16 at 12:52
  • the argument you have passed into barChart(), is created and stored in the heap with a java made reference (as it has no variable name), and by referencing what should be done to value in barChart(), the java virtual machine knows how to manipulate your argument. – dxpelou Jan 12 '16 at 12:54

2 Answers2

1

In Java, everything is pass-by-value and it is also important to know what the value is.

This method

void demo(){
    barChart(new int[] {2,5,0,4,3});
}

Do the same as this one

void demo(){
    int[] arr = new int[] {2,5,0,4,3};
    barChart(arr);
}

In the first one, there is created new array with 2,5,0,4,3 values and its reference is copied to parameter values in barChart method.

In second one, there is created new array with 2,5,0,4,3 values and its reference is copied to variable arr. Then the value of arr (which is reference to array) is copied to parameter values in barChart method.

And this is how it works and why barChart method knows the values.


Also good point by Łukasz, the second line does not do anything in your program, therefore you can change this :

class printChart {
  int[] values;

  void barChart(int[] values){

to this

class printChart {

  void barChart(int[] values){
libik
  • 22,239
  • 9
  • 44
  • 87
  • I think I understand it better know, thanks a bunch! – Rob Jan 12 '16 at 12:57
  • 1
    also worth mentioning that second line of OP's code `int[] values;` is useless and shadowed by method argument. – Łukasz Jan 12 '16 at 12:57
  • One more question; does this mean that it's redundant to declare int[] values at the start of the code? – Rob Jan 12 '16 at 13:03
  • 1
    @Rob - that `int[] values` in start of your code is `class variable` and it is not actually used in your program. If you have parameter and class variable with same name, then inside of that function, the parameter "wins". If you want to call class variable inside that function, you can call it as `this.values`. – libik Jan 12 '16 at 13:15
  • 1
    @Rob - but redundant is not right word. When you call barChart you have two independent variables with same name. – libik Jan 12 '16 at 13:19
  • Hi Libik. Thanks for clarifying. Maybe not redundant, but since this program doesn't use the class variable I can just as easily delete it, unless I'm misstaken – Rob Jan 12 '16 at 13:21
  • 1
    @Rob - yes, you can. – libik Jan 12 '16 at 13:22
1

I'm not so sure what your question is, but let me tell you bit what you've done.

You've implemented a method(function) named void barChart(int[] value)
To run this method you must need to pass a one dimensional Array of Integer values to it.

Now comes the interesting part.

You've created a class Variable int[] values; in code line 2.
Also you've have created the lokal variable "value" in the method void barChart(int[] value).
What you've done is called overshadowing. The method "barChart()" only uses the lokal value which is passed to it when it is called.
You never used the class variable once, hence you could delete it.

Now if you want to use the class variable you could either:
a) Change the name of the variable (class or local)
b) In the method "barChart" write a this.value instead of just value. This will ensure that you are using the class variable and not the local one.