www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Our First Java Program Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
In this lesson we will learn to write our first Java program. Before doing so, let’s first understand some basics of Java program files.

Java Program Files:
Java program files are very similar to the .c or .cpp program files. In Java the source code is written in the .java file. Each .java source file can contain at most one public class. If there is a public class, then the class name and file name must match. Furthermore, every piece of java code must be the part of a class.

The Java compiler creates Bytecode from the .java program file and stores the same in a .class file. This bytecode, contained in the .class file, generated by the Java compiler is ready to be executed by the Java Virtual Machine (JVM). For each class in a source file (both public and non-public classes), the compiler creates one ".class" file, where the file name is the same as the class name.

Our First Java Program:
Let’s write our first Java program:

class HelloWorld {		
	    public static void main( String [ ] args )	{		    
		     System.out.println("Hello world!");	
     }	
}

You must be glad to see that the above code is very similar to the C or C++ code. When the above code is run after compilation, it simply prints “Hello world!” to the console.

Important points to remember:

  • Function main is the entry point of a java program.
  • Function main is a member of the class.
  • Every class MAY have at most one main function. There may be more than one main function in a Java program, however.
  • The main function must be public static void.
  • The main must have only one argument: an array of String. This array contains the command-line arguments.
  • There is no final semi-colon at the end of the class definition.

Compiling a Java Program File:
We assume that you have downloaded and installed the latest version of JDK (Java Development Kit) from:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

After the installation of JDK, you need to add the bin directory of your java installation to the system path variable, so that the system could find the java compiler and interpreter, whenever needed.

When compiling a program, you type the full file name, including the ".java" extension and when running a program, you just type the name of the class whose main function you want to execute.

Use javac command to compile your java program files.
C:\ javac helloworld.java

Use java command to run or execute the compiler generated .class files.
C:\ java helloworld

Note that your directory structure may be different from the above.


Next >>> Lesson No. 3: Java Data Types





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.