When I run the following program:
public class Test
{
public static void main(String[] args)
{
System.out.println(args);
}
{
It prints: [Ljava.lang.String;@153c375
and when I run it again, it prints: [Ljava.lang.String;@1d1e730
it gives me different output each time
So, what does “[Ljava.lang.String;@153c375” mean?
Update: I just realized I never answered the question “What does “String[] args” contain in java?” 🙂 It’s an array of the command-line arguments provided to the program, each argument being a
Stringin the array.And we now resume with our regularly-scheduled answer…
argsis an array. To see individual command-line arguments, index into the array —args[0],args[1], etc.:You can loop through the args like this:
For
java Test one two three, that will output:Or loop like this if you don’t need the index:
That’s Java’s default
toStringreturn value forString[](an array ofString). SeeObject#toString. The[means “array”, theLmeans “class or interface”, andjava.lang.Stringis self-explanatory. That part comes fromClass#getName(). The;@153c375is;@followed by thehashCodeof the array as a hex string. (I think the default implementation ofhashCodeforObjectindicates where in memory the array is located, which is why it’s different for different invocations of your program, but that’s unspecified behavior and wouldn’t be any use to you anyway.)