Command line arguments.21

974 views
752 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
974
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Command line arguments.21

  1. 1. Usage of command-line arguments http://improvejava.blogspot.in 1 1
  2. 2. ObjectiveOn completion of this period, you would be able to know, • The usage of command-line arguments 2 http://improvejava.blogspot.in
  3. 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. 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. 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. 6. Example Program : command-line argumentsclass 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. 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. 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. 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. 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. 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. 12. Summary• About command line arguments• Usage of command line arguments http://improvejava.blogspot.in 12 12
  13. 13. Quiz1. 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. 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. 15. Frequently Asked Questions1. Explain the use of Command-line arguments with an example program? http://improvejava.blogspot.in 15 15
  16. 16. Assignment1. Write a program in Java that takes command-line arguments and display how many number of parameters passed2. Write a program in Java that takes command line arguments and that searches for a give is present in command-line arguments3. 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

×