10x Blogs Directory

Thursday, February 19, 2009

Analyzing the Basic Program

1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • Indicates the name of the class which is Hello
  • In Java, all code should be placed inside a class declaration
  • The class uses an access specifier public, which indicates that our class in accessible to other classes from other packages (packages are a collection of classes). We will be covering packages and access specifiers later.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • The next line which contains a curly brace { indicates the start of a block.
  • In this code, we placed the curly brace at the next line after the class declaration, however, we can also place this next to the first line of our code. So, we could actually write our code as:
    public class Hello{

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */

  • The next three lines indicates a Java comment.
  • A comment
    -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.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 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.
  • All programs except Applets written in Java start with the main method.
  • Make sure to follow the exact signature.

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7
//prints the string “Hello world” on screen

The 7th line is also a Java comment

______________________________________________________________________


1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7 //prints the string “Hello world” on screen
8
System.out.println(“Hello world”);

The command System.out.println(), prints the text enclosed by quotation on the screen.
______________________________________________________________________

1 public class Hello
2 {
3 /**
4 * My first Java program
5 */
6 public static void main( String[] args ){
7 //prints the string “Hello world” on screen
8 System.out.println(“Hello world”);

9 }
10 }

The last two lines which contains the two curly braces is used to close the main method and class respectively.