Beruflich Dokumente
Kultur Dokumente
Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL
Java Java
MO NDA Y , A PR IL 22, 2013
Learning Java
Install Java
When to make a method static in Java Top 5 JQuery books for Beginners and Web developers - Best of lot How SSL, HTTPS and Certificates works in Java web applications Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization 10 Java Exception and Error Interview Questions Answers 5 must read books to learn Object Oriented and Java Design patterns - Best of lot
S t r i n ga r g s [ ] ,which is actually an array of j a v a . l a n g . S t r i n gtype, and it's name is a r g shere. It's not necessary to name it a r g salways, you can name it s t r A r r a yor whatever you like, but most programmer prefer to name it a r g s ,because that's what everyone else do. When we run a Java program from command prompt, we can pass some input to our Java program. Those inputs are stored in this String args array. For example if we modify our program to print content of S t r i n ga r g s [ ] ,as shown below:
p u b l i cc l a s sS t r i n g A r g s T e s t{ p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ){ S y s t e m . o u t . p r i n t l n ( " S t r i n ga r g u m e n t sp a s s e dw h i l er u n n i n gt h i sJ a v aP r o g r a m:" ) ; f o r ( S t r i n ga r g u m e n t:a r g s ) { S y s t e m . o u t . p r i n t l n ( a r g u m e n t ) ; } } }
and after compiling, we run this Java program as j a v aS t r i n g A r g s T e s tJ a v aJ 2 E EA n d r o i d , then this program will print following output : String arguments passed while running this Java Program : Java J2EE Android because, we have passed three arguments separated by white space. If you want to combine multiple words, which has some white space in between, then you can enclose them in double quotes. If we run our program again with following command : j a v aS t r i n g A r g s T e s t" T h i si so n ea r g u m e n t "" T h i si sa n o t h e ra r g u m e n t " it will print: String arguments passed while running this Java Program : This is one argument This is another argument By the way, if you are using Eclipse to run Java Program, then you can provide String argument to your Java program using " R u nC o n f i g u r a t i o n " .When you run Java program, by right click on Java class with main method, it creates a Run Argument for that class. you can access them as R i g h tc l i c k > R u nA s > R u nC o n f i g u r a t i o n . In second tab, you can see two section Program Arguments and VM Arguments. You can pass all your String arguments from this program arguments section. As shown in this image By the way, you can write your S t r i n ga r g s [ ]as S t r i n g [ ]a r g sas well, because both are valid way to declare String array in Java. Another interesting thing to note is that, from Java 5 onwards, you can varargs or variable arguments in main method instead of S t r i n ga r g s [ ] . Which means following declaration of main method is also valid : p u b l i c s t a t i cv o i dm a i n ( S t r i n g . . .a r g s ) , and you can access this variable argument as normal array in Java. E.g. if we
Follow Us
Follow @javinpaul 3,239 follow ers
Javarevisited on
Follow
+3,257
javarevisited.blogspot.in/2013/04/what-is-string-args-in-java-main-method.html
1/3
7/15/13
now. Related Java Programming Tutorials from Javarevisited Blog How to install JDK 7 in Windows 8 64-bit machine How to set PATH and Classpath in Windows 8 notebook What is JAVA_HOME environment variable in Java How to debug Java Program in Eclipse IDE How to create JAR file to bundle Java program in Eclipse
Javarevisited
Like 4,660 people like Javarevisited.
Security System
You might like:
Notebook Computer
Programming Software
Programm
10 Reasons to Learn Java Programming Language and Why Java is Best What is Abstraction in java - Java abstraction tutorial and example Java Tips and Best practices to avoid NullPointerException in Java Applications How to override compareTo method in Java - Example Tutorial
Recommended by
Recommendd Books Java Programming and Design Pattern Spring Framework Subscribe by email: Subscribe By Javin Paul
Posted by Javin Paul at 5:41 AM Labels: core java, java beginners tutorial, programming
Running Shoes
Langs
Java Programming
Java Programm
Subscribe To This Blog Free Posts Comments Followers Join this site
w ith Google Friend Connect
Security System
Notebook Computer
Programming Software
Home
Older Post
javarevisited.blogspot.in/2013/04/what-is-string-args-in-java-main-method.html
2/3
7/15/13
References Java API documentation JDK 6 Spring framework doc Struts ANT Maven JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.
About Me
Privacy Policy
javarevisited.blogspot.in/2013/04/what-is-string-args-in-java-main-method.html
3/3