how does net.java.dev.jaxb.array.StringArray works?

1.5k Views Asked by At

I know about the working of a list. when i make the following code its working fine but i cant access its values and i know what are the values in it but following output is showing which is wrong.

 List<StringArray> searchresponse = searchContent(data, pasta, chan, Type, arrS, arrk);
System.out.print(searchresponse);

this output =
[net.java.dev.jaxb.array.StringArray@787582d3] is not correct. How to show the items which are coming in response of that function which is called ?

2

There are 2 best solutions below

1
On

System.out.print(/*Object*/ o) is equivalent to System.out.print(/*Object*/ o.toString())

In your case o is searchresponse

[net.java.dev.jaxb.array.StringArray@787582d3]

This is the default toString() behaviour.

public String More ...toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

To verify, try this:

String s = searchresponse.toString();
System.out.println(s);// prints net.java.dev.jaxb.array.StringArray@HEXCODE
1
On

Whenever we try to print objects, compiler will find toString() method in Object class and produce string representation of objects.You will have to override this method to get the actual values of instance variables.

class A
  { 
     String name;
     int id;
     A(String name, int id)
       {
          this.name=name;
          this.id=id;
       }
  public String toString()
      {
        return (name+" "+id);
      }
  public static void main (String ...a)

    { 
       List<A> list = new ArrayList<A>();
       A o = new A("a",1);
       A o1= new A ("b",2);
       list.add(o);
       list.add(o1);
       System.out.println(list); 

   }

 }

Output

[a 1, b 2]