Java TOPIC 05 – YOUR FIRST JAVA
PROGRAM LESSON NOTE WHICH IDE SHOULD I USE? During the lessons, Mr. Campeau will
likely suggest an IDE to start with.
However, if you really prefer using a different IDE, then you are
welcome to do so. FILENAMES Some IDEs require that you enter the
source code’s filename before you even type in the source code. So we will discuss filenames right now. FILE
EXTENSION Java source code files end with the
.java extension. FILENAME
VS CLASS NAME The filename you choose has to match
the class name that you put in your source code. There are certain rules that apply to class
names and hence apply to file names.
We will look at this more closely later on. For now, just remember that they have
to be the same. JAVA
PROGRAM TEMPLATE Here is the template for all simple
Java Programs: public
class FirstProgram { public static void main(String[] args) { //All code goes here (for now) } } Note
that the above template would work in a file called FirstProgram.java. JAVA
PROGRAM TEMPLATE EXPLANATION Here
is an explanation of the template:
At first, the template may seem
cumbersome or messy, but you will quickly get use to it. OUTPUT STATEMENT In java, we use the following
statement to output a message to screen: System.out.print("Hello world"); If we want the cursor to be placed on
the next line after outputting the message, we use: System.out.println("Hello world"); It is easy to forget the semicolon at
the end. Also, Java is case sensitive
to make sure that everything is exactly the same. SIMPLE PROGRAM
We can now write our very first simple
program in Java. We simply write the
template and place an output statement at the appropriate location. public
class FirstProgram { public static void main(String[] args) { System.out.println("Hello
world"); } } INDENTING Notice
how the code inside the curly brackets is indented by three spaces. This is considered good programming
practice and should always be done (even if the compiler won’t complain about
it). Be sure to indent code in your
programs! COMMON ERRORS
|
|