PIC20A Lecture 2: A simple program
In the section, we will learn how to
- write a simple application
- use comments
- run a java program
- print out message on screen
You must read the textbook p32-36 or
A Closer Look at HelloWorld.
1. // name this file HelloWorld.java
2.
3. /* Usage :
4. type java HelloWorld
5. at Dos Command prompt */
6.
7. /** Written by Charles Li */
8.
9. public class HelloWorld {
10. public static void main(String[] args) {
11. System.out.println("Hello World !!!");
12. }
13.}
Line 1
// name this file HelloWorld.java
Explanation:
Same as C++, words after // are comments.
Every words on the same line after // will be ignored by the compiler
Syntax :
//your comments
Line 3-5
/*.... */
Explanation :
As in C++, all the words between /* and */ will be ignored.
/* and */
can be on one single line or on different lines.
Syntax :
/* your comment */
or
/* comment 1
comment 2
comment 3 */
Line 7
/**....*/
Explanation:
This is very simple to /* comments */.
Everything between /** and */ will be ignored.
Again, the comments can be on either one single line or on
multiple lines. The difference between the these 2 types of comments is that the
comment between the /** and */ will appear in the documentation generated
by the Java tools (javadoc, you will learn this later).
Syntax :
/** your comment */
or
/** comment 1
comment 2
comment 3 */
Line 9
public class HelloWorld
Explanation:
HelloWorld is the name of the class. You must name the file
HelloWorld.java. This is the only possible name of this file.
Syntax :
public class classname {
.........
}
Line 10
public static void main(String[] args)
Explanation:
- Similar to C++,
main function is the entry point of the program.
The body of main function is the starting point of the program.
Codes inside the program will be executed.
- You can run the program by
java classname
- Not every class has a main function.
If you want to run the program by
java someclass
, then you should implements the main function.
Syntax :
public static void main(String[] args) {
// your codes
}
Detail Explanation for PIC10B students(don't worry if you don't understand it now)
public indicates the method main is a public method
static indicates the method is a class method.
void is the return type
main is the method name
String[] args means that the argument is an array of String named args.
Line 11.
System.out.println("Hello World !!!");
Explanation:
Syntax :
System.out.println("print something with a return at the end");
System.out.print("print something without return");
Detail Explanation for PIC10B students(don't worry if you don't understand it now)
System is a class in java.
out is a class member of the class System.
println(or print) is a member function of out.
- The functions have one argument (
String).
/* Usage :
type java HelloWho Charles
at Dos Command prompt
The output will be
Hello Charles
*/
public class HelloWho {
public static void main(String[] args) {
System.out.println("Hello
" + args[0] );
}
}
Explanation:
args is an array of String
args[0] is the first string after java HelloWho.
- If you type
java HelloWho Charles Li then args[0]
is Charles, args[1] is Li.
- If you type
java HelloWho "Charles Li", then arg[0]
is Charles Li (the whole word).
Question :
What is the output of
java HelloWho Amy
java HelloWho Amy Betty
java HelloWho "Amy Betty"
System.out.println
- You can add 2
Strings together by an operator +
- Example
System.out.println("Hello " + "Charles");
is same as
cout << "Hello " << "Charles" << endl;
in C++.
- You can also print out strings with numbers
- Example
int num = 100;
System.out.println("There are " + num + " students.");
is same as
int num = 100;
cout << "There are " << num << " students." << endl ;
Summary
After this lecture, you should know
- 3 types of comments in Java . (a) // (b) /* comments */ (c)
/** comments */
- How to write a program with a main function. And how to execute
the program.
System.out.println
- What is the meaning of
args[0], args[1]....