Sie sind auf Seite 1von 3

7/15/13

What is String args[] in Java main method

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

Java Test Tool


Searc h

What is String args[] in Java main method


Recent Posts
What is String args is used for or Why we use String arg[] in main method is a common doubt among Java beginners. First thing newbies exposed while learning Java programming language is writing HelloWorld program in Java, Though H e l l o W o r l dis smallest sort of program, it contains lot of useful information and concept for newbies Java programmers, one of them is main method and it's different attribute. Though I covered bit of S t r i n g a r g s [ ] , while discussing Why main is public static in Java, I thought to write this article to explain String args array in more detail. Let's see a simple Java HelloWorld again to understand more about String args in Java.
p u b l i cc l a s sH e l l o J a v a { 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 ( " H e l l o w o r l di nJ a v a " ) ; } }

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

What is String args[] in Java main method


use this syntax of main, in your earlier program we don't need to change anything else, it would work, just like it is working

Please share with your friends if like this article

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

F acebook social plugin

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

+7 Recommend this on Google

Environmentally Friendly Used For

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

No comments: Post a Comment


E n t e ry o u rc o m m e n t . . .

Members (1250) More

Comment as: Google Account Publish Preview


Already a member? Sign in

Newer Post Subscribe to: Post Comments (Atom)

Home

Older Post

Learn Java Programming Java Sample Programs Java Test Tool


Blog Archive 2013 (97) July (4) June (9) May (14) April (18) How to Find Runtime of a Process in UNIX and Linux... Eclipse No Java Virtual Machine was found Windows ... What is java.library.path , How to set in Eclipse ... Difference between valueOf and parseInt

javarevisited.blogspot.in/2013/04/what-is-string-args-in-java-main-method.html

2/3

7/15/13

What is String args[] in Java main method


method in ... 10 Reasons to Learn Java Programming Language and ... What is String args[] in Java main method How to Convert InputStream to Byte Array in Java -... 10 Abstract Class and Interface Interview Question... How to Convert JSON array to String array in Java ... Top 10 Puzzles, Riddles, Logical and Lateral Think... How to Check If Number is Even or Odd without usin... How to Compare Two Enum in Java - Equals vs == vs ... How to get current stack trace in Java for a Threa... Spring Framework Tutorial - How to call Stored Pro... How to increment decrement date by days in Java - ... Difference between trunk, tags and branches in SVN... What is maximum Java Heap Size for 32 bit and 64-b... JUnit 4 Tutorial - Test Exception thrown by Java M... March (16) February (18) January (18) 2012 (218) 2011 (145) 2010 (33)

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

Das könnte Ihnen auch gefallen