正在加载图片...
Appendix E Java Primer optional parameters that may be passed on the command line.Output to the screen is done via the call to the method System.out.println().Note that Java supports the C++style of comments,including /This string is a comment for single-line comments and /And this string too is a comment for multiline comments. The name of the file containing the main()method must match the class name in which it appears.In this instance,the main()method is in the class First.Therefore,the name of this file must be First.java.To compile this program using the Java Development Kit (JDK)command line compiler,enter javac First.java on the command line.This will result in the generation of the file First.class.To run this program,type the following on the command line java First Note that java commands and file names are case sensitive. E.1.2 Methods A program usually consists of other methods in addition to main().Figure E.2 illustrates the use of a separate method to output the message Java Primer Now Brewing!In this instance,the String is passed to the static method printIt(), where it is printed.Note that both the main()and printIt()methods return void.Later examples throughout this chapter will use methods that return values.We will also explain the meaning of the keywords public and static in Section E.1.6. public class Second public static void printIt(String message){ System.out.printIn(message); } public static void main(String args[]){ printIt ("Java Primer Now Brewing!"); } Figure E.2 The use of methods.2 Appendix E Java Primer optional parameters that may be passed on the command line. Output to the screen is done via the call to the method System.out.println(). Note that Java supports the C++ style of comments, including // This string is a comment for single-line comments and /* And this string too is a comment */ for multiline comments. The name of the file containing the main() method must match the class name in which it appears. In this instance, the main() method is in the class First. Therefore, the name of this file must be First.java. To compile this program using the Java Development Kit (JDK) command line compiler, enter javac First.java on the command line. This will result in the generation of the file First.class. To run this program, type the following on the command line java First Note that java commands and file names are case sensitive. E.1.2 Methods A program usually consists of other methods in addition to main(). Figure E.2 illustrates the use of a separate method to output the message Java Primer Now Brewing! In this instance, the String is passed to the static method printIt(), where it is printed. Note that both the main() and printIt() methods return void. Later examples throughout this chapter will use methods that return values. We will also explain the meaning of the keywords public and static in Section E.1.6. public class Second { public static void printIt(String message) { System.out.println(message); } public static void main(String args[]) { printIt("Java Primer Now Brewing!"); } } Figure E.2 The use of methods
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有