During session 1
In this session, we are going to use Notepad and Terminal for practising compiling.
1. Compile and run the program in the command line
When you start writing a piece of code in Java in any editor (e.g. Notepad, Gedit, etc.), the program looks like a simple text file. Unlike Python, the program cannot be executed at once. In Java, the program has to be written, complied and after that executed.
1. Create a folder for your Java files (e.g., "c:\myProject", or "d:\myProject").
2. Launch a programming text editor (e.g. NotePad, or NotePad++, or TextPad - DO NOT use MS Word) and enter the source code:
/* * First Java program to say Hello */ public class HelloWorld { // Save as "HelloWorld.java" into "d:\myProject" public static void main(String[] args) { System.out.println("Hello, world!"); } }
3. Save the file as HelloWorld.java into your folder (e.g. d:\myProject).
4. Start a cmd: click the "Start" button ⇒ "run..." ⇒ Enter "cmd").
5. Set the Current Drive to the drive where you have saved your source file HelloWorld.java. (e.g. if the source file is saved in drive d, enter d:
as follow:
C:> d: D:>
6. Set the Current Working Directory to the directory with the source file via the cd
(Change Directory) command. (e.g. if the source file is saved into d:\myProject:
D:> cd \myProject D:\myProject>
7. Issue a dir
(List Directory) command to confirm that the source file is present in the current directory:
D:\myProject> dir ...... xx-xxx-xx 06:25 PM 277 HelloWorld.java ......
8. Invoke the JDK compiler javac
to compile the source code HelloWorld.java.
D:\myProject> javac HelloWorld.java
The compilation is successful if the command prompt returns. Otherwise, error messages would be shown.
The name specified is not recognized as an internal or external command ...
or Bad command or file name
. This means that java cannot find the compiler. In such case show the path to the compiler , e.g.:
D:\myProject>path C:\Program Files\Java\jdk-9.0.4\bin
9. The output of the compilation is a Java class called HelloWorld.class. Issue a dir
command again to check for the output.
D:\myProject> dir ...... xx-xxx-xx 01:53 PM 416 HelloWorld.class xx-xxx-xx 06:25 PM 277 HelloWorld.java ......
10. To run the program, invoke the Java Runtime java
:
D:\myProject> java HelloWorld Hello, world!
Conclusion: Computers do not directly understand Java. We need a compiler between the source code and the computer. When we are programming using the command line interface, the command javac HelloWorld.java
will compile the HelloWorld.java file into bytecode, which can be executed using the Java interpreter. To run the compiled program, you can use the command java HelloWorld
where HelloWorld is the name of the original source code file.
2. Kitty
Write a program that prints a kitty speaking a greeting, similar to (but different from) the following:
/\_/\ ----- ( ‘ ’ ) / Hello, \ ( - ) < Junior | | | | \ Coder! / (_|_) -----
Be careful with back-slashes \.
3. A random number
Study the following example:
double randomDouble = Math.random()*5+15; long roundedRandomLong = Math.round(Math.random()*5+15); int invalidInt = (int)Math.random()*5+15;
What is the output of the statements? What data type are the outputs?
Write a program that generates two random integers in the range of 21 and 90 (not inclusive), and prints out their fraction accurate to third decimal place.
After this session, we are going to use IntelliJ - a modern development environment which takes care of compiling the source code. When we choose to run the program, the development environment compiles and executes the program. Generally, all development environments compile source code while it is being written by the programmer, which means that simple errors will be noticed before executing the program.