Learn-Java.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Your First Java Program

Your First Java Program

E-mail Print PDF

Every programming language has a unique syntax and structure. This section will introduce you to the anatomy of a Java program. In this section, you will learn about the basic building blocks that make up a Java program.

A Simple Example of a Java Program

public class MyFirstJavaProgram
{
public static void main(String args[])
{
System.out.println("This is what a Java program looks like");
}
}

 

Note: All source codes in this website are highlighted

To Create your first Java program:

  1. Start Dr Java
  2. Copy and Paste the above source code in Dr Java
  3. Click Save. (Dr Java automatically saves your source code file as "MyFirstJavaProgram.java", do not change your file name )
  4. Press F5 to compile your code
  5. Press F2 to run your code.
  6. You will see the text "This is what a Java program looks like" in the interaction window (the box below your source code in Dr Java)

Congratulation, you have succesfully written, compiled and ran a Java code. Ready to understand more about the code you wrote? Scroll down to read more.

Classes

The first line is called the class header. A class groups together the components of an object. Do not worry about what an object is right now; they will be covered in future topics, when Object-Orientated Programming concept is introduced.

For now, just think of a class as a container for the rest of your code. After the class header, we write an opening curly brace ({), followed by some methods or variables, and then a closing curly brace (}). When you create a new program, you will first declare a class. The rest of your code will then be contained inside of the two braces.

In the above example, MyFirstJavaProgram is the name of the class. When you save a Java file it must be named classname.java (where classname is the name of the class). In the example, we would save the file as MyFirstJavaProgram.java

The Main Method

The main method header has the following form:

public static void main(String args[]) 
{

}

The main method is a special method in Java, and every Java program must have one. This is because the program starts running on the first line of the main method.

The main method must be declared inside of a class. After the header of the main method (and all other Java methods), we write opening and closing curly braces (just like when we define a class).
The meaning of static will be discussed in a later section

Statements

An instruction in Java is called a statement. A statement is a single line of code that performs a task. All statements must end with a semicolon. In the first example program, the following statement prints "This is what a Java program looks like" to the screen:.

System.out.println("This is what a Java program looks like");

Case Sensitivity

Java is a case sensitive programming language. This means that it distinguishes between letters that are typed in upper case and letters that are typed in lower case. For example, if you type Main, this is not the same as typing main.

Class Naming Guidelines

A class name should be a noun that starts with a capital letter. The first letter of each word should also be capitalized. For example:

public class Tetris
public class DayPlanner

Last Updated on Saturday, 05 December 2009 04:58  

Search This Site


Newsflash


"Welcome to Learn Java.com. A learning website created to teach programming concept through Java. Learn java.com is a beginner guide to programming and it is also an introductory guide to Java."