What does "String[] args" contain in java?
When I run the following program:
public class Test
{
public static void main(String[] args)
{
System.out.println(args);
}
{
It p开发者_如何转开发rints: [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 String
in the array.
And we now resume with our regularly-scheduled answer...
args
is 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:
public class Test
{
public static void main(String[] args)
{
int index;
for (index = 0; index < args.length; ++index)
{
System.out.println("args[" + index + "]: " + args[index]);
}
}
}
For java Test one two three
, that will output:
args[0]: one args[1]: two args[2]: three
Or loop like this if you don't need the index:
public class Test
{
public static void main(String[] args)
{
for (String s : args)
{
System.out.println(s);
}
}
}
So, what does
"[Ljava.lang.String;@153c375"
mean?
That's Java's default toString
return value for String[]
(an array of String
). See Object#toString
. The [
means "array", the L
means "class or interface", and java.lang.String
is self-explanatory. That part comes from Class#getName()
. The ;@153c375
is ;@
followed by the hashCode
of the array as a hex string. (I think the default implementation of hashCode
for Object
indicates 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.)
String[] args
in main method is the String array of the command line arguments.
[Ljava.lang.String;@1d1e730
are the class name ([Ljava.lang.String
is String[]
) and the object's hashcode (@1d1e730
);
if you want to print the actual values of the Strings in the array, you can use a simple for-each loop:
for(String arg:args)
System.out.println(arg);
It's a form of name mangling used for disambiguating method overloads. The method name is appended by a series of characters describing the parameters and return type: the parameters appear sequentially inside parentheses, and the return type follows the closing parenthesis. The codes are as follows:
- Z: boolean
- B: byte
- C: char
- S: short
- I: int
- J: long
- F: float
- D: double
- L fully-qualified-class-name ; : fully qualified class
- [ type : array of type
- V: void
So according to above codes [Ljava.lang.String;@153c375
Array of string (java.lang.String fully qualified class name) followed by hascode.
String[] args
is an Array of Strings and contains the arguments that were given when the application was started. Java does not require you to use the name args, you could just as well specify String[] foo
but that will make things unclear if you later read your code again.
Default implementation of toString
method for Object is classname;@identityHashCode
.
I think, this is what you expect:
System.out.println(java.util.Arrays.toString(args));
It's a string array.
- Modify your code to this:
public class Test{
public static void main(String[] args){
System.out.println(args[0]);
}
}
- Now compile this code:
$>javac Test.java
- Now run:
$>java Test hello
This will print: "hello"
Because "hello" is the argument you are passing to your class.
If you try: args[x], where x=0..n
and run your class via command line: java Test your arguments, then you will see any contents which you pass..
The main method has a parameter that is an array of String references. So each time you try to print args, it gives you memory location of array 'args' because this String array args located a place in memory for array elements.
That say you have an simple program called 'HelloWorld.java' like this:
public class HelloWorld
{
public static void main(String [] args)
{
for(int i =0; i<args.length; i++)
System.out.println(""+args[i]);
}
}
Ready to test this program with command line interface:
java HelloWorld a b c
We can see that this program prints thouse arguments after 'java Helloworld' a b c
Lets make it simple
1: Write a simple class named "test"
public class test {
public static void main(String[] args) {
System.out.println("Passed argument is: " + args[0]);
}
}
2: Now compile it in the following way
javac test.java
3: Now run this by passing an argument
java test Ayoub
** the output will be**
Passed argument is: Ayoub
精彩评论