Every Java program has at least one
method
i.e., Main method
Every Java program has at least one method
Can we pass arguments to the
main method...?
How do we pass arguments to the
- Command Prompt
main method...?
What can be passed as arguments - Inputs can be passed
to the main method...? as arguments
Command Line Arguments
public static void main(String args[])
main() function of a Java program accepts arguments from
command prompt or from other shell scripts.
String args[] – passes the arguments to main function and stored
as string array
Predict the output, if input “hello” is passed
in the command prompt
class Main{
Output:
public static void main(String args[]) {
hello
[Link](args[0]);
}
}
Write a program to add two numbers using command line arguments.
Sample Sample Output:
Input:
10 20 30
Add Two Numbers
import [Link];
class Main{
public static void main(String args[]){
Command line
Scanner in = new Scanner([Link]);
arguments...?
int x = [Link]();
int y = [Link]();
int z = x + y;
[Link](z);
}
}
Add Two Numbers
import [Link];
class Main{ Error
:
public static void main(String args[])
{
int sum = args[0] + args[1];
[Link](sum);
}
}
Parsing
Conversion of String to Number
Types of Parse Methods:
[Link](); Convert string to
[Link](); Integer
Float
Double
[Link]();
Add Two Numbers
import [Link];
class main {
public static void main(String args[]) {
int a = [Link](args[0]);
int b = [Link](args[1]);
int sum = a + b;
[Link](sum);
}
}
Predict the output
import [Link];
public class main {
public static void main(String args[]) {
String s= "156";
Output
[Link](s + 1); 1561
}
}
Predict the output
import [Link];
class main {
public static void main(String args[]) {
String s= "156"; Output
double x = [Link](s);
[Link](x + 1);
157
}
}
THANK YOU