Software to install and first program
Install the following software in the order below. You will be asked to choose your operating system for each software, and whether it’s 32-bits or 64-bits. If not sure, use this link to determine the same.
-
Java Standard Edition (Java SE), previously known as Java Standard Development Kit (Java SDK). Current version (on the date of writing this document):
10.0.2. You’ll be fine as long as you have version 8 or above. -
Eclipse IDE for Java developers. Current version (on the date of writing this document):
Photon. Previous versionsOxygenandNeonshould also be ok.
NOTE: You are free to use any other IDE besides Eclipse (such as NetBeans or IntelliJ) if you want. However, we shall be doing all screencasts and video tutorials using Eclipse.
First java program
Once you have installed Java SE, you are ready to write and run Java programs. First we’ll learn how to write and run a java program from command prompt.
Open notepad and type the following code in it.
1
2
3
4
5
public class MyFirstProgram {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Save the file as MyFirstProgram.java (case sensitive).
Open command prompt (or terminal on Mac), go to the folder in which MyFirstProgram.java exists, and type the following:
1
javac MyFirstProgram.java
This is the compilation step.
If everything went ok, it would execute this command and generate a new file named MyFirstProgram.class in the same directory asMyFirstProgram.java. This is the classfile or bytecode. It’s this file that actually executes. Once it’s generated, you can even delete the source file (MyFirstProgram.java) provided you don’t need to make any further changes).
You run the classfile using the java command.
1
java MyFirstProgram
This is the execution step.
If there are no run-time errors, you will get the following output on the console.
1
Hello world!
Write a simple java program
Write a java program that initializes two variables,
distance,time, and give them values of your choice. Calculate the speed and display on the console. Compile and run the java program.Solution
Debug a simple program
What’s wrong with the following code?
1 2 3 4 5 public class Worker { public static void main(String[] args) { } System.out.println("How do you do?"); }SOLUTION
Display statement exists outside the
mainmethod. Corrected version:
1 2 3 4 5 public class Worker { public static void main(String[] args) { System.out.println("How do you do?"); } }
Debug a simple program
What’s wrong with the following code?
1 2 3 4 5 public static void main(String[] args) { public class Worker { System.out.println("All right!"); } }SOLUTION
mainshould be inside theclass, not the other way round. Corrected version:
1 2 3 4 5 public class Worker { public static void main(String[] args) { System.out.println("All right!"); } }
Debug a simple program
What’s wrong with the following code?
1 2 3 4 5 public class Worker { public static void main(String[] args) { System.out.println("Wassup!") } }SOLUTION
Missing semi-colon at the end of line 3. Corrected version:
1 2 3 4 5 public class Worker { public static void main(String[] args) { System.out.println("Wassup!"); } }
First java program in Eclipse
Adding more functions to a java program
You will notice that the main function is labelled static by Java. Your program will not run if you remove this keyword. A function being static means that the only data it operates on (if any) are the parameters passed.
Equivalent versions of the same function in Processing and Java are provided below -
| Processing | Java |
|---|---|
The public keyword means that the method is visible to any code in the project. In this unit, we use public visibility for all methods (and data values).
</div> I