Java Programming Ground RulesComputer Programming I Instructor: Greg ShawCOP 2210 Java Programming Ground RulesThe traditional first program, Hello.java, is very simple yet showssome of the basic requirements of Java programs.public class Hello{public static void main (String args[]){// display a greeting in the console windowSystem.out.println("Hello, world!") ;}}1. The basic unit of Java programming is the class. The Java Libraryis a collection of many classes that programmers may reuse.Programmers also create their own classes. Java programs generallyconsist of several classes, at least one of which is defined by theprogrammer. The above program has only one programmer-definedclass, called Hello2. Each class may contain any number of programmer-defined methods,which contain all the executable statements (aka: instructions,commands) of the program 3. Every Java program must have one special method called main,spelled all in lowercase The first statement executed in every Java program is alwaysthe first statement in the main method4. The main method declaration (or, “heading”)public static void main (String args[])must appear exactly as shown5. All the statements in a class or method definition must be enclosedin a pair of braces {}6. Java is case-sensitive All Java reserved words (public, class, static, void) uselowercase letters only. Reserved words - also known as keywords- have special meaning to Java and may not be used for anythingelse. Appendix C contains a summary of Java’s reserved words Java identifiers are names given to objects (out), classes(Hello), variables (args), and methods (println). Although someidentifiers may contain only lowercase letters, they are notkeywords Class names from the Java Library (String, System) begin with anuppercase letter. Programmer-defined class names (Hello) shouldtoo7. Java statements must end with a semi-colon. The Hello programcontains only one statement8. Generally, each Java class definition is contained in a separateJava file and declared public, meaning anyone can use the class. Wewill have more to say about access specifiers such as public lateron 9. The name of the file must be exactly the same – includingcapitalization – as the name of the public class, and must have theextension .java For example, the file containing the Hello class must be namedHello.java(Although a Java file may contain multiple class definitions, thisis considered bad form for several reasons. For us, each Javaclass will be defined in a separate Java file. If a file doescontain multiple classes, only one may be designated public andthat one must be the class containing the main method) 10. Respectable Java classes and methods also contain liberaldocumentation, or “comments.” Comments are ignored by thecompiler. They are for humans only, to explain what the programdoes and how. Different kinds of comments will be discussed inclass. The main method of the Hello class has an example of aninline comment. Two forward slashes, //, turn the remainder of the currentline only into a
View Full Document