-1

The following code showed up in a past paper with multiple mistakes (which i have spotted and fixed easily) but what i struggle to understand is the output.

The classes:

import java.util.*;
class Count {
    int i, s, o, l, c;
    public Count(Object[] objects){
        for(int j=0; j<objects.length; j++){
            count(objects[j]);
        }
    }
    public void count(Collection x){c++;}
    public void count(List x){l++;}
    public void count(Double x){i++;}
    public void count(Object x){o++;}
    public void count(String x){s++;}
    public String toString(){
        return c + ", " + l + ", " + i + ", " + o + ", " + s;
    }
}

import java.util.LinkedList;
public class Test {
    static Object[] objects = {new Double(10), new String("Q1"), 
                                new Object(), new LinkedList()};
    public static void main(String args[]){
        System.out.println(new Count(objects));
        for(Object o : objects)
            System.out.println(o);
    }
}

Generated output:

0, 0, 0, 4, 0
10.0
Q1
java.lang.Object@6d06d69c
[]

I'd appreciate if someone could explain the output with reference to the code.

Side note: the first line of output is what puzzles me. The other bit i understand. Last note: This is a unique question regarding the output. It is not a duplicate of any question (to the best of my knowledge - the link to the 'possible duplicate' is regarding method overloading not "how is this output produced?" and the outcomes in both questions are unique to each other) so a precise answer would be helpful. Thanks.

TheDerp
  • 131
  • 1
  • 10
  • The first line is just printing the number of different thing you've counted. You're passing an array of `Object` with 4 elements to your constructor, so `o` is `4`. – azurefrog May 19 '16 at 20:29
  • That first line is the `toString()` method of `Count` and it is showing how many times a certain method in `Count` is invoked. `c` is the number of times `count(Collection)` is called, `l` is the number of times `count(List)` is called, etc. – FriedSaucePots May 19 '16 at 20:29
  • 1
    Sorry, what are you confused about in the output? that it's `0, 0, 0, 4, 0` instead of `1/0, 0/1, 1, 1, 1`? If so, then as @Tom posted, this has been answered. – Ironcache May 19 '16 at 20:40
  • @Ironcache I'm confused about how 0,0,0,4,0 is produced. Will have a look at the answers below to see if they clear my confusion. :) – TheDerp May 19 '16 at 20:47
  • If you would actually read the linked duplicate, then you would know why the output is 4. You don't understand why your code will call `count(Object x)` four times and not one of the other overloaded version. The duplicate answers that. – Tom May 19 '16 at 20:56
  • @Tom I read the link and i understand method overload but what confused me is the content of the array of objects. I had thought e.g. the method count(Double x) would be called from within the loop because the array of objects contained Double (at index 0). That is why i was confused. I had a quick look at the link but it didn't quite answer what i was concerned about, it was helpful though. – TheDerp May 19 '16 at 21:10
  • *"I had a quick look at the link but it didn't quite answer what i was concerned about"* It does answer exactly that. – Tom May 19 '16 at 21:22

4 Answers4

2
 public Count(Object[] objects){
        for(int j=0; j<objects.length; j++){
            count(objects[j]);
        }

In these lines you're creating an array of elements in which each element is of type Object itself (it's the class from which every other class inherits). So output of counting objects of type Object in array shouldn't be a surprise. (Counting is held by overloaded methods. When you call counting function the one that is chosen must have the most suitable type. Hence, counting with Object type as a parameter is picked.)

0, 0, 0, 4, 0

Next, you're creating new array of Objects, initializing each element of the array with different type.

static Object[] objects = {new Double(10), new String("Q1"), 
                                        new Object(), new LinkedList()};

It's possible because of upcasting. As I mentioned before, every element inherits from Object class, so every object in your program is in fact Object. Using mechanism of polymorphism you're program can now "deduce" the real type of elements in your array. Namely, you can see it here

for(Object o : objects)
            System.out.println(o);

where Java is printing information obtained dynamically. Hence, double is printed as double, string as string, object itself doesn't have more sensible toString() method than printing its inner "name"(by the way this is the field each object of type Object has) and of course empty link list is no more than [].

threaz
  • 403
  • 7
  • 14
2
for(int j=0; j<objects.length; j++){
            count(objects[j]);
        }

corresponds to

public void count(Object x){o++;} only

because the argument you're passing when you call count(objects[j]) is an object. You're calling a count function which has an object as parameter, and that's what Java is doing for you.

The number of objects in objects[] is 4. Hence count(Object x) gets called 4 times.

As regards the other integers, they are initialized by default to 0, and hence you see 0.

That's why you see 0 for all variables except o.

attaboy182
  • 2,039
  • 3
  • 22
  • 28
1

One of your problems lies here:

    public void count(Collection x){c++;} // never called, init to 0
    public void count(List x){l++;} // never called, init to 0
    public void count(Double x){i++;} // never called, init to 0
    public void count(Object x){o++;} // called 4 times, result is 4
    public void count(String x){s++;} // never called, init to 0

You never initialize any of the variables (c, k, i, o, s) so I believe the int type is auto initialized to 0. Then the next time it is called it will increment. So end result is:

0, 0, 0, 4, 0 // 

Now the rest:

10.0 // 10.0 because that was value specified when Double was created
Q1 // Q1 because that was the String value when it was created.
java.lang.Object@6d06d69c // memory address of where Object is
[] // initialized to empty list []
sebenalern
  • 2,515
  • 3
  • 26
  • 36
0

The first line outputs the number of Collections, Lists, Doubles, Objects, Strings passed to the constructor of the Count instance in the main method of the Test class, since all of the Count classes only accept one parameter and the count variables are not static four of these will always be zero.

The next four lines print the four objects in the static objects array. This is effectively just printing the results of each objects toSrting() method.

Wayne
  • 3
  • 3