Simple Java Program

1 Simple Java Program

Programming environments

ANYONE WHO IS LEARNING to program has to choose a programming environment that makes it possible to create and to run programs. Programming environments can be divided into two very different types: integrated development environments and command-line environments. All programming environments for Java require some text editing capability, a Java compiler, and a way to run applets and stand-alone applications.

An integrated development environment, or IDE, is a graphical user interface program that integrates all these aspects of programming and probably others (such as a debugger, a visual interface builder, and project management).

A command-line environment is just a collection of commands that can be typed in to edit files, compile source code, and run programs.

Integrated Development Environments

It is really quite remarkable that there are sophisticated IDEs for Java programming that are available for free. Some IDE examples are following:

  • Eclipse IDE -- An increasingly popular professional development environment that supports Java development, among other things. Eclipse is itself written in Java. It is available from http://www.eclipse.org/.

  • NetBeans IDE -- A pure Java IDE that should run on any system with Java 1.3 or later. NetBeans is a free, "open source" program. It is essentially the open source version of the next IDE. It can be downloaded from www.netbeans.org.

  • Sun ONE Studio 4 for Java, Community Edition, for Linux, Solaris, Windows 2000, Windows NT, and Windows 98SE. This was formerly known as "Forte for Java", and you might see it referred under that name. Again, it requires a lot of resources, with a 256 MB memory recommendation. Main site currently at http://www.sun.com/software/sundev/jde/index.html. It is available from there and on the J2SE download page, http://java.sun.com/j2se/1.4/download.html. The Community Edition is the free version.

  • Borland JBuilder Personal Edition, for Linux, Solaris, MacOS X, Windows 2000, Windows XP, and Windows NT. Requires a lot of disk space and memory (256 MB memory recommended). Company Web page at http://www.borland.com. JBuilder site at http://www.borland.com/jbuilder/index.html. The Personal Edition, which is free, has more than enough features for most programmers.

  • Mac OS X Project Builder comes as a standard part of Mac OS X (on the Developer CD). It supports Java as well as some other programming languages.

  • BlueJ is a Java IDE written in Java that is meant particularly for educational use. It is available from http://www.bluej.org/.

  • JCreator, is an IDE written in C++. JCreator is only available on the Windows operating system. It is available at http://www.jcreator.com.

Text Editors

If you decide to use a command-line environment for programming, make sure that you have a good text editor. A programmer's text editor is a very different thing from a word processor. Most important, it saves your work in plain text files and it doesn't insert extra carriage returns beyond the ones you actually type. A good programmer's text editor will do a lot more than this. Here are some features to look for:

  • Syntax coloring. Shows comments, strings, keywords, etc., in different colors to make the program easier to read and to help you find certain kinds of errors.

  • Function menu. A pop-up menu that lists the functions in your source code. Selecting a function from this will take you directly to that function in the code.

  • Auto-indentation. When you indent one line, the editor will indent following lines to match, since that's what you want more often than not when you are typing a program.

  • Parenthesis matching. When you type a closing parenthesis the cursor jumps back to the matching parenthesis momentarily so you can see where it is. Alternatively, there might be a command that will hilite all the text between matching parentheses. The same thing works for brackets and braces.

  • Indent Block and Unindent Block commands. These commands apply to a hilited block of text. They will insert or remove spaces at the beginning of each line to increase or decrease the indentation level of that block of text. When you make changes in your program, these commands can help you keep the indentation in line with the structure of the program.

  • Control of tabs. My advice is, don't use tab characters for indentation. A good editor can be configured to insert multiple space characters when you press the tab key.

Examples of editors: jedit,nedit, BBEdit, notepad++ etc.

Writing,compiling and running a simple java program:

we will learn how to write the Simple java program. Creating simple java example is too easy. Here, we have created a class named Simple that contains only main method and prints a message hello java. It is the simple program of java.

Requirement for Simple Java Example

For executing any java program, you need to

  • create the java program.

  • install the latest JDK if you don't have installed it, download the JDK and install it.

  • set path of the bin directory under jdk.

  • compile and execute the program.

Creating Simple java example

Let's create the Simple java program:

  1. class Simple{  

  2.     public static void main(String args[]){  

  3.      System.out.println("Hello Java")  

  4.     }  

  5. }  

 

save this file as Simple.java

To compile:

javac Simple.java

To execute:

java Simple

Output:Hello Java


Understanding first java program

Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().

  • class is used to declare a class in java.

  • public is an access modifier which represents visibility, it means it is visible to all.

  • static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.

  • void is the return type of the method, it means it doesn't return any value.

  • main represents startup of the program.

  • String[] args is used for command line argument. We will learn it later.

  • System.out.println() is used print statement.

To write the simple program, open notepad and write simple program as displayed below:

As displayed in the above diagram, write the simple program of java in notepad and saved it as Simple.java. To compile and run this program, you need to open command prompt by start -> All Programs -> Accessories -> command prompt

To compile and run the above program, go to your current directory first where your program is saved; my current directory is c:\alka . Write here:

To compile:

javac Simple.java

To execute:

java Simple

Resolving an exception "javac is not recongnized as an internal or external command" ?

If there occurs a problem like displayed in the below figure, you need to set path. Since DOS doesn't know javac or java, we need to set path. Path is not required in such a case if you save your program inside the jdk/bin folder. But its good approach to set path.

Internal Details of Simple Java Program

we have learned about the first program, how to compile and how to run the first java program. Here, we are going to learn, what happens while compiling and running the java program. 

What happens at compile time?

At compile time, java file is compiled by Java Compiler (It does not interact with OS) and converts the java code into bytecode.

What happens at runtime?

At runtime, following steps are performed:

Classloader: is the subsystem of JVM that is used to load class files.

Bytecode Verifier: checks the code fragments for illegal code that can violate accesss right to objects.

Interpreter: read bytecode stream then execute the instructions.

Q) Can you save a java source file by other name than the class name?

Yes, like the figure given below illustrates:

To compile:

javac Hard.java

To execute:

java Simple

Q)Can you have multiple classes in a java source file?

 

Yes, like the figure given below illustrates:

How to set path of JDK in Windows :

Path is required for using tools such as javac, java etc. If you are saving the java file in jdk/bin folder, path is not required.But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK. There are two ways to set path of JDK:

1. temporary

2. permanent

1)Setting temporary Path of JDK in Windows:

For setting the temporary path of JDK, you need to follow these steps:

  • Open command prompt
  • copy the path of bin folder

write in command prompt: set path=copiedpath

For Example:

 

set path=C:\Program Files\Java\jdk1.7.0\bin

Let's see it in the figure given below:

2)Setting Permanent Path of JDK in Windows:

For setting the permanent path of JDK, you need to follow these steps:

  • Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok

For Example:

1)Go to MyComputer properties

3)click on environment variables

4)click on new tab of user variables

5)write path in variable name

6)Copy the path of bin folder

7)paste path of bin folder in variable value

8)click on ok button

9)click on ok button

Now permanent path is set.You can now execute any program of java from any drive

Difference between JDK,JRE and JVM

  1. Brief summary of JVM
  2. Java Runtime Environment (JRE)

  3. Java Development Kit (JDK)

JVM

JVM (Java Virtual Machine) is an abstract machine.It is a specification that provides runtime environment in which java bytecode can be executed.

JVMs are available for many hardware and software platforms (i.e.JVM is plateform dependent).

The JVM performs four main tasks:

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment

 

JRE

JRE is an acronym for Java Runtime Environment.It is used to provide runtime environment.It is the implementation of JVM.It physically exists.It contains set of libraries + other files that JVM uses at runtime.

Implementation of JVMs are also actively released by other companies besides Sun Micro Systems.

JDK

JDK is an acronym for Java Development Kit.It physically exists.It contains JRE + development tools.