All tutorials are implemented in text (here), and also with exercises and multimedia in the Tutorial Player. The main home page has a search engines, and more facilities.
main()main() is a method that starts any Java application.
Arguments are passed as follows;
java ProgramName arg1 arg2 "a string arg"
Try coding the example. The filename must the same as the class name, ArgumentText, with a .java suffix. Use javac ArgumentText.java to create ArgumentText.class.
class ArgumentTest {
public static void main(String[] arguments) {
for (int i=0; i < arguments.length;i++) {
System.out.println("Argument " + i + ":" + arguments[i]);
}
}
}
Run this application by entering in DOS or UNIX
java ArgumentTest arg1 arg2 "string in arg"
You will see:
Argument 0: arg1 Argument 1: arg2 Argument 2: arg3
I have written this program to show the concepts involving methods, arguments, classes. Run this program with different names, and see that the program correctly identifies good customers. In practice, the list of good customers would be obtained from a database query.
import java.lang.*;
class Customer {
public boolean goodCustomer(String customerName) {
String[] goodCustomer = { "Joe Harris", "Bob Francis"}; // Could be from a database query
for (int i=0;i < goodCustomer.length;i++) {
if (customerName.compareTo(goodCustomer[i]) == 0) {
return true;
}
}
return false;
}
public static void main(String[] arguments) {
for (int i=0; i < arguments.length;i++) {
System.out.println("Argument " + i + ":" + arguments[i]);
}
Customer thisCustomer = new Customer();
for (int i=0;i < arguments.length;i++) {
if (thisCustomer.goodCustomer(arguments[i])) {
System.out.println("good name" + arguments[i]);
}
}
}
}