Next: Method calls
Up: Unit 02
Previous: Summary
Consider the following Java program:
public class MyMessage {
public static void main(String[] args) {
//What does this program do?
/* Do you know it?
It's easy. */
System.out.println("JA");
System.out.println("VA");
}
}
Form of the program
- Single words in a Java program are separated by blanks. E.g.,
public class.
- Continuing on a new line has also the effect of separating two words (as
if we were inserting a blank).
- We can use an arbitrary number of blanks or new lines to separate words
(at least one).
The indentation has no effect at all on the execution of the
program. However, for readability of the code, it is very important to use
proper indentation.
Comments
It is possible to annotate the text of the program with comments. In
Java we can make use of two types of comments:
- // denotes the start of a comment that spans only till the end
of the line;
- /* ...*/ delimits a comment that can span several
lines.
Comments have no effect on the execution of the program. Again, they are
used to improve the readability of the program code.
Other properties of Java
- public class MyMessage. All Java programs are collections of
Java classes.
- public static void main(String[] args). This is the standard
main method, which indicates where the execution of the program
should start.
- System is a predefined Java class.
- System.out is an object that is an instance of the
predefined class PrintStream. This object is defined in the class
System and its purpose is to handle the system output of the
program (e.g., monitor output).
- println(...) is a method (operation) that is supported
for the objects of type PrintStream (it is a property of the class
PrintStream).
- System.out.println("...") is a call of the method
println("...") on the invocation object System.out,
and it has "..." as parameter.
Note: typically, Java adopts the convention that class names start with
a capital letter, and method names with a lowercase letter. You better
respect this convention, if you want your programs to be understood.
Next: Method calls
Up: Unit 02
Previous: Summary