0

and thanks for reading this question from a beginner.

I tried to run this code on the command:

public class a{
  public static void main(String[] args){
    String[] s = args;
    System.out.println(s);
  }
}

and the following input: "java a this is string"

gives me:

"[Ljava.lang.String;@65d4ab0e" as an output.

What should I do to get "this is string" as output?

James C
  • 207
  • 1
  • 7
  • `s` is an *array*, which is an *object*, and in Java since each object has `toString()` method, the default is displaying the class name representation, then adding `@` sign and then the hashcode.. You *don't* want to print the object, but its content, use [`Arrays.toString`](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-java.lang.Object:A-) method. – Maroun Jan 22 '15 at 09:07
  • There are a couple of options. Easiest Arrays.toString(s); other option is to loop through your string array and print each member in turn. The output you see it's a representation of the array object, not it's members. Confusing if coming from some other languages, but logical once you get the hang of it. – J Richard Snape Jan 22 '15 at 09:09

2 Answers2

1

You are printing the array object itself, not the content of the array. The hashcode value [Ljava.lang.String;@65d4ab0e indicates that, s is an array and it's has String values. The leading [ says, the object you have printed is an array.

public class a{
  public static void main(String[] args){
    String[] s = args;
    System.out.println(Arrays.toString(s));
  }
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

Use Arrays.toString(s)

Take a look at http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(java.lang.Object[])

Andres
  • 10,561
  • 4
  • 45
  • 63