• Post Reply Bookmark Topic Watch Topic
  • New Topic

for this below java program why output is shown like this?  RSS feed

 
rajesh villuri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for this below java program why output is shown like this?

public class constr {

private static constr ct;

public constr(char c,int... x)
{
System.out.println(c);
System.out.println(x);
}

public static void main(String[] args) {
ct = new constr('f',1,2,3);
}

}



output:
f
[I@1e41869

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your constr method uses varargs syntax.

The argument variable 'x' in this method will look like an array of ints (int[]). Arrays in Java are objects. When you print an int[] like you do, then the toString() method will be called implicitly on the int[] object. Since int[] does not have its own toString() method, the toString() method in class Object will be called. It prints a string of the form: <class name>@<hashcode>

In this case, "[I" is the internal class name of the int[] object, and "1e41869" is the hashcode of the object.

If you want to print the contents of the array, then use Arrays.toString():
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!