Programming Fundamentals
Objectives:
Identify the basic parts of a Java program
Differentiate among Java literals, primitive data
types, variable types ,identifiers and operators
Develop a simple valid Java program using the
concepts learned in this chapter
Dissecting my first Java program
public class Hello
{
/**
* My first java program
*/
public static void main(String[] args) {
//prints the string "Hello world" on screen
System.out.println("Hello world!");
}
}
The first line of the code,
public class Hello
access specifier class name
The next line which contains a curly brace {
Indicates the start of the block
public class Hello
{
or
public class Hello {
The next three lines indicates a Java comment
/**
* My first java program
*/
A comment is something used to document a part of a code. It is
not part of the program itself, but used for documentation purposes.
It is good programming practice to add comments to your code.
A comment is indicated by the delimiters “/*” and “*/”. Anything
within these delimiters are ignored by the Java compiler, and are
treated as comments.
The next line,
public static void main(String[] args) {
or can also be written as,
public static void main(String[] args)
{
indicates the name of one method in Hello which is
the main method. The main method is the starting
point of a Java program.
The next line is also a Java comment,
//prints the string "Hello world" on screen
Two ways of creating comments. The first one is by
placing the comment inside /* and */, and the other
one is by writing // at the start of the comment.
The next line,
System.out.println("Hello world!");
prints the text “Hello World!” on screen. The
command System.out.println(), prints the text
enclosed by quotation on the screen.
Java Statements and blocks
A statement is one or more lines of code
terminated by a semicolon
Ex. System.out.println(“Hello world”);
A block is one or more statements bounded by an
opening and closing curly braces that groups the
statements as one unit.
Ex. public static void main( String[] args ){
System.out.println("Hello");
System.out.println("world");
}
Exercises
1. Create a class named:[YourName]. The program
should output on the screen:
Welcome to Java Programming [YourName]!!!
cont.
2. Create a class named: MyTree. The program
should output the following lines on the screen:
I think that I shall never see,
a poem as lovely as a tree.
A tree whose hungry mouth is pressed
Against the Earth’s sweet flowing breast.