SlideShare a Scribd company logo
2
Most read
4
Most read
Usage of command-line arguments




                    http://improvejava.blogspot.in   1
                                                         1
Objective


On completion of this period, you would be able to know,

              • The usage of command-line arguments




                                                             2
                            http://improvejava.blogspot.in
Recap

• In the last class we discussed
   • The String class
   • Constructors of String class
   • Methods of String class




                        http://improvejava.blogspot.in   3
                                                             3
Usage of command-line arguments


• How to pass information into a program when you run it?

• This is accomplished by passing command-line arguments to
  main()

• A command line argument is the information that directly
  follows the programs name on the command-line when it is
  executed




                       http://improvejava.blogspot.in         4
                                                                  4
Usage of command-line arguments                          contd..

• To access command-line arguments inside a java program is
  quite easy

• They are stored as Strings in the String array passed to main()

• For example, the following program displays all of the
  command-line arguments that it is called with




                                                                    5
                                                                        5
Example Program : command-line arguments
class CommandLine {

      public static void main (String args[]) {

         for(int i=0; i<args.length; i++) {

              System.out. println(“ args [ ”+ i “ ] : ”+args[i]);

           } // end of for
      } // end of main
}// end of class
                       http://improvejava.blogspot.in        6
                                                                 6
Example Program : Command-line Arguments contd..
 •   Try executing the program as shown here

 •   Java CommandLine this is a test 100 -1

 •When you do, you will see the following output:
             Output :
                        args[0] :   this
                        args[1] :   is
                        args[2] :   a
                        args[3] :   test
                        args[4] :   100
                        args[5] :   -1

                                                    7
                                                        7
Usage of command-line arguments

• All command-line arguments are passed as strings



• You must convert it into desired format, using wrapper classes




                                                                   8
                                                                       8
Example Program
• Usage of command-line arguments that needs conversion
 class CommandLine {
       public static void main (String args[]) {
                   int a = Integer.parseInt(args[0]);
                   int b = Integer.parseInt(args[1]);
                   int c = a + b;
                   System .out. println(“ sum of the ”+ a + “ and ” +
 b+ “ is :”+ c);
       } // end of main
 }// end of class


                                                                        9
                                                                            9
Example Program contd..

•   Try executing the program as shown here,

•   Java CommandLine 10 20

• When you do, you will see the following output


           Output :
                      sum of the 10 and 20 is : 30




                         http://improvejava.blogspot.in   10
                                                               10
Discussion
• What are the differences between command line
  arguments in C and Java ?
                      C                                 Java

  Number of     One less than the                  Equal to actual
  arguments      actual number                        number

   Type of       Character array                       String
  arguments




                  http://improvejava.blogspot.in                     11
                                                                          11
Summary

• About command line arguments
• Usage of command line arguments




                 http://improvejava.blogspot.in   12
                                                       12
Quiz
1. Command-line arguments are passed from
   a. calling method
   b. command prompt
   c. from JVM
   d. from operating system




                 http://improvejava.blogspot.in   13
                                                       13
Quiz                      contd..

2. Command-line arguments are stored in
   a. Variables
   b. Actual parameters
   c. int parameter
   d. String array




                  http://improvejava.blogspot.in             14
Frequently Asked Questions



1. Explain the use of Command-line arguments with an
   example program?




                   http://improvejava.blogspot.in   15
                                                         15
Assignment
1. Write a program in Java that takes command-line
   arguments and display how many number of
   parameters passed

2. Write a program in Java that takes command line
   arguments and that searches for a give is present in
   command-line arguments

3. Write a program in Java that takes command line
   arguments and print the parameter in String array in
   reverse order
                   http://improvejava.blogspot.in    16

More Related Content

What's hot (20)

PPTX
Arrays in java
Arzath Areeff
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PDF
Method Overloading In Java
CharthaGaglani
 
PPTX
parameter passing in c#
khush_boo31
 
PDF
Arrays in Java
Naz Abdalla
 
PPT
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPTX
arrays and pointers
Samiksha Pun
 
PPTX
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
PPTX
Command line arguments
Ashok Raj
 
PPTX
Switch statement, break statement, go to statement
Raj Parekh
 
PPTX
Array in Java
Ali shah
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPTX
Regular Expression
valuebound
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPSX
Collections - Array List
Hitesh-Java
 
PPT
Java packages
Raja Sekhar
 
PPS
Introduction to class in java
kamal kotecha
 
Arrays in java
Arzath Areeff
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Strings in Java
Abhilash Nair
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Method Overloading In Java
CharthaGaglani
 
parameter passing in c#
khush_boo31
 
Arrays in Java
Naz Abdalla
 
BackTracking Algorithm: Technique and Examples
Fahim Ferdous
 
itft-Inheritance in java
Atul Sehdev
 
arrays and pointers
Samiksha Pun
 
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Command line arguments
Ashok Raj
 
Switch statement, break statement, go to statement
Raj Parekh
 
Array in Java
Ali shah
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Regular Expression
valuebound
 
07. Virtual Functions
Haresh Jaiswal
 
Collections - Array List
Hitesh-Java
 
Java packages
Raja Sekhar
 
Introduction to class in java
kamal kotecha
 

Viewers also liked (9)

PPTX
File Handling and Command Line Arguments in C
Mahendra Yadav
 
PPT
17 files and streams
Docent Education
 
PPT
working file handling in cpp overview
gourav kottawar
 
PPT
Perl Intro 4 Debugger
Shaun Griffith
 
PDF
Command Line Arguments with Getopt::Long
Ian Kluft
 
PPT
Perl Intro 9 Command Line Arguments
Shaun Griffith
 
ODP
Geographic Computation in Perl
Ian Kluft
 
PPT
Filehandlinging cp2
Tanmay Baranwal
 
PDF
Command line arguments that make you smile
Martin Melin
 
File Handling and Command Line Arguments in C
Mahendra Yadav
 
17 files and streams
Docent Education
 
working file handling in cpp overview
gourav kottawar
 
Perl Intro 4 Debugger
Shaun Griffith
 
Command Line Arguments with Getopt::Long
Ian Kluft
 
Perl Intro 9 Command Line Arguments
Shaun Griffith
 
Geographic Computation in Perl
Ian Kluft
 
Filehandlinging cp2
Tanmay Baranwal
 
Command line arguments that make you smile
Martin Melin
 
Ad

Similar to Command line arguments.21 (20)

PDF
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 
PDF
Class notes(week 5) on command line arguments
Kuntal Bhowmick
 
DOCX
Class notes(week 5) on command line arguments
Kuntal Bhowmick
 
PPTX
Getting Program Input
Dr. Rosemarie Sibbaluca-Guirre
 
PDF
Java Fundamentals
Shalabh Chaudhary
 
PPT
7 streams and error handling in java
Jyoti Verma
 
PPTX
Command line arguments & This keyword.pptx
abhinandpk2405
 
PPTX
Java programing language unit 1 introduction
chnrketan
 
PPS
Java session08
Niit Care
 
PDF
02-Basic-Java-Syntax.pdf
erusmala888
 
PPTX
23_1-Command line Arguments.pptx
GandavadiVenkatesh1
 
PDF
How to start learning java
billgatewilliam
 
PPT
Chtp414
giovanniveitch
 
PPT
Chapter 2 java
ahmed abugharsa
 
PDF
Command Line Arguments in Computer Programming
manaskrishnaks2004
 
PDF
Java cheat sheet
Piyush Mittal
 
PDF
Java%20 programming%20guide%20 %20quick%20reference
Shalini Pillai
 
PDF
Java%20 programming%20guide%20 %20quick%20reference
Shalini Pillai
 
PDF
Java programming guide - quick reference
Tutorials Tips Tricks
 
PDF
Java Programming Guide Quick Reference
FrescatiStory
 
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 
Class notes(week 5) on command line arguments
Kuntal Bhowmick
 
Class notes(week 5) on command line arguments
Kuntal Bhowmick
 
Getting Program Input
Dr. Rosemarie Sibbaluca-Guirre
 
Java Fundamentals
Shalabh Chaudhary
 
7 streams and error handling in java
Jyoti Verma
 
Command line arguments & This keyword.pptx
abhinandpk2405
 
Java programing language unit 1 introduction
chnrketan
 
Java session08
Niit Care
 
02-Basic-Java-Syntax.pdf
erusmala888
 
23_1-Command line Arguments.pptx
GandavadiVenkatesh1
 
How to start learning java
billgatewilliam
 
Chapter 2 java
ahmed abugharsa
 
Command Line Arguments in Computer Programming
manaskrishnaks2004
 
Java cheat sheet
Piyush Mittal
 
Java%20 programming%20guide%20 %20quick%20reference
Shalini Pillai
 
Java%20 programming%20guide%20 %20quick%20reference
Shalini Pillai
 
Java programming guide - quick reference
Tutorials Tips Tricks
 
Java Programming Guide Quick Reference
FrescatiStory
 
Ad

More from myrajendra (20)

PPT
Fundamentals
myrajendra
 
PPT
Data type
myrajendra
 
PPTX
Hibernate example1
myrajendra
 
PPTX
Jdbc workflow
myrajendra
 
PPTX
2 jdbc drivers
myrajendra
 
PPTX
3 jdbc api
myrajendra
 
PPTX
4 jdbc step1
myrajendra
 
PPTX
Dao example
myrajendra
 
PPTX
Sessionex1
myrajendra
 
PPTX
Internal
myrajendra
 
PPTX
3. elements
myrajendra
 
PPTX
2. attributes
myrajendra
 
PPTX
1 introduction to html
myrajendra
 
PPTX
Headings
myrajendra
 
PPTX
Forms
myrajendra
 
PPT
Css
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPT
Starting jdbc
myrajendra
 
Fundamentals
myrajendra
 
Data type
myrajendra
 
Hibernate example1
myrajendra
 
Jdbc workflow
myrajendra
 
2 jdbc drivers
myrajendra
 
3 jdbc api
myrajendra
 
4 jdbc step1
myrajendra
 
Dao example
myrajendra
 
Sessionex1
myrajendra
 
Internal
myrajendra
 
3. elements
myrajendra
 
2. attributes
myrajendra
 
1 introduction to html
myrajendra
 
Headings
myrajendra
 
Forms
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Starting jdbc
myrajendra
 

Command line arguments.21

  • 1. Usage of command-line arguments http://improvejava.blogspot.in 1 1
  • 2. Objective On completion of this period, you would be able to know, • The usage of command-line arguments 2 http://improvejava.blogspot.in
  • 3. Recap • In the last class we discussed • The String class • Constructors of String class • Methods of String class http://improvejava.blogspot.in 3 3
  • 4. Usage of command-line arguments • How to pass information into a program when you run it? • This is accomplished by passing command-line arguments to main() • A command line argument is the information that directly follows the programs name on the command-line when it is executed http://improvejava.blogspot.in 4 4
  • 5. Usage of command-line arguments contd.. • To access command-line arguments inside a java program is quite easy • They are stored as Strings in the String array passed to main() • For example, the following program displays all of the command-line arguments that it is called with 5 5
  • 6. Example Program : command-line arguments class CommandLine { public static void main (String args[]) { for(int i=0; i<args.length; i++) { System.out. println(“ args [ ”+ i “ ] : ”+args[i]); } // end of for } // end of main }// end of class http://improvejava.blogspot.in 6 6
  • 7. Example Program : Command-line Arguments contd.. • Try executing the program as shown here • Java CommandLine this is a test 100 -1 •When you do, you will see the following output: Output : args[0] : this args[1] : is args[2] : a args[3] : test args[4] : 100 args[5] : -1 7 7
  • 8. Usage of command-line arguments • All command-line arguments are passed as strings • You must convert it into desired format, using wrapper classes 8 8
  • 9. Example Program • Usage of command-line arguments that needs conversion class CommandLine { public static void main (String args[]) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int c = a + b; System .out. println(“ sum of the ”+ a + “ and ” + b+ “ is :”+ c); } // end of main }// end of class 9 9
  • 10. Example Program contd.. • Try executing the program as shown here, • Java CommandLine 10 20 • When you do, you will see the following output Output : sum of the 10 and 20 is : 30 http://improvejava.blogspot.in 10 10
  • 11. Discussion • What are the differences between command line arguments in C and Java ? C Java Number of One less than the Equal to actual arguments actual number number Type of Character array String arguments http://improvejava.blogspot.in 11 11
  • 12. Summary • About command line arguments • Usage of command line arguments http://improvejava.blogspot.in 12 12
  • 13. Quiz 1. Command-line arguments are passed from a. calling method b. command prompt c. from JVM d. from operating system http://improvejava.blogspot.in 13 13
  • 14. Quiz contd.. 2. Command-line arguments are stored in a. Variables b. Actual parameters c. int parameter d. String array http://improvejava.blogspot.in 14
  • 15. Frequently Asked Questions 1. Explain the use of Command-line arguments with an example program? http://improvejava.blogspot.in 15 15
  • 16. Assignment 1. Write a program in Java that takes command-line arguments and display how many number of parameters passed 2. Write a program in Java that takes command line arguments and that searches for a give is present in command-line arguments 3. Write a program in Java that takes command line arguments and print the parameter in String array in reverse order http://improvejava.blogspot.in 16