What is Java?
Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the Oak name to Java.
Application
According to Sun, 3 billion devices run Java. There are many devices where Java is currently used. Some of them are as follows:
- Desktop Applications such as acrobat reader, media player, antivirus, etc.
- Web Applications such as irctc.co.in, javatpoint.com, etc.
- Enterprise Applications such as banking applications.
- Mobile
- Embedded System
- Smart Card
- Robotics
- Games, etc.
How to Create Your First Java Program
The basic steps to create the Hello World program are: write the program in Java, compile the source code, and run the program.
Write the Java Source Code
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
All Java programs are written in plain text — therefore you don’t need any special software. For your first program, open up the simplest text editor you have on your computer, likely Notepad.
The entire program looks like this:
While you could cut and paste the above code into your text editor, it’s better to get into the habit of typing it in. It will help you learn Java more quickly because you will get a feel for how programs are written, and best of all, you will make mistakes! This may sound odd, but each mistake you make helps you to become a better programmer in the long run. Just remember that your program code must match the example code, and you’ll be fine.
Note the lines with “//” above. These are comments in Java, and the compiler ignores them.
- Line //1 is a comment, introducing this program.
- Line //2 creates a class HelloWorld. All code needs to be in a class in order for the Java runtime engine to run it. Note that the entire class is defined within enclosing curly braces (on line /2 and line //6).
- Line //3 is the main() method, which is always the entry point into a Java program. It also is defined within curly braces (on line //3 and line //5). Let’s break it down:
public: This method is public and therefore available to anyone.
static: This method can be run without having to create an instance of the class HelloWorld.
void: This method does not return anything.
(String[] args): This method takes a String argument. - Line //4 writes “Hello World” to the console.
Save the File
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
- Save your program file as “HelloWorld.java”. You might consider creating a directory on your computer just for your Java programs.
It’s very important that you save the text file as “HelloWorld.java”. Java is picky about filenames. The code has this statement:
- This is an instruction to call the class “HelloWorld”. The filename must match this class name, hence the name “HelloWorld.java”. The extension “.java” tells the computer that it’s a Java code file.
Open a Terminal Window
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
Most programs that you run on your computer are windowed applications; they work inside a window that you can move around on your desktop. The HelloWorld program is an example of a console program. It does not run in its own window; it has to be run through a terminal window instead. A terminal window is just another way of running programs.
- To open a terminal window, press the “Windows key” and the letter “R”.
- You will see the “Run Dialog Box”. Type “cmd” to open the command window, and press “OK”.
A terminal window opens on your screen. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files they contain, and run programs. This is all done by typing commands into the window.
The Java Compiler
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
- Another example of a console program is the Java compiler called “javac.” This is the program that will read the code in the HelloWorld.java file, and translate it into a language your computer can understand. This process is called compiling. Every Java program you write will have to be compiled before it can be run.
- To run javac from the terminal window, you first need to tell your computer where it is. For example, it might be in a directory called “C:\Program Files\Java\jdk\1.6.0_06\bin”. If you don’t have this directory, then do a file search in Windows Explorer for “javac” to find out where it lives.
Once you’ve found its location, type the following command into the terminal window:
E.g.,
Press Enter. The terminal window will just return to the command prompt. However, the path to the compiler has now been set.
Change the Directory
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
Next, navigate to the location your HelloWorld.java file is saved.
To change the directory in the terminal window, type in the command:
E.g.,
You can tell if you’re in the right directory by looking to the left of the cursor.
Compile Your Program
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
We’re now ready to compile the program. To do so, enter the command:
- Press Enter. The compiler will look at the code contained within the HelloWorld.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.
- Hopefully, you should have no errors. If you do, go back and check the code you’ve written. Make sure it matches the example code and re-save the file.
Tip: Once your HelloWorld program has been successfully compiled, you will see a new file in the same directory. It will be called “HelloWorld.class”. This is the compiled version of your program.
Run the Program
Microsoft product screenshot(s) reprinted with permission from Microsoft Corporation.
All that’s left to do is run the program. In the terminal window, type the command:
When you press Enter, the program runs and you will see “Hello World!” written to the terminal window.
Example with a simple Java program
Open Notepad and type in this program, maintaining the upper and lower case, because Java is a case-sensitive programming language.
- class Krishna {
- public static void main(String[] args) {
- System.out.println(“(“Welcome to Basic concept of Java”);
- }
- }
- After writing this code, save the program. When you save it, you need to save the program with only the class name like:
Krishna.java //.java is extension of Java file - After saving, compile and run the program, so you need to open a “cmd”. Click the Window button and type “cmd” then hit Enter and open a “cmd”. Then type the cmd command, for going to the location where your Java program is. For example, mine is at “desktop” so I need to type: cd desktop.
- Then show desktop, on your cmd, then type the following to compile:
javac Krishna.java
//javac for Java compile - When your Java program is compiled successfully, with no error, then you have an auto-created .class file.
- If the compile was successful, then to run type:
Java Krishna
Welcome to the Basic concept of Java
Now we can create a program in Java, without a main() and terminate the curly bracket, with a semicolon ({;};)
The following is an example:
- class Krishna {
- ;
- static {
- ;
- System.out.println(“Welcome to Basic concept Of Java”);
- };
- };
See that this program has no errors, when compiled and run.
See the following output here:
Java has more packages like:
- import java.util.Scanner;
- import java.awt.*;
- import java.applet.*;
AWT is the Abstract Window Toolkit.
The following is an example of a packages program:
- import java.util.Scanner;
- class Oddeven {
- public static void main(String[] args) {
- int a;
- System.out.println(“Please enter the number “);
- Scanner in = new Scanner(System.in);
- a = in .nextInt();
- if (a % 2 == 0) {
- System.out.println(“this number is even num”);
- } else {
- System.out.println(“this number is odd num”);
- }
- }
- }
Use the same process as above, for the first Java program and for other Java programs. Compile and run it.
When you compile the same with the same class name load the .class name file. Then you can run using the command only.
java Oddeven //java and class name only.
See the following image with the program and output in “cmd”:
- After run show message
- Please enter the number
- User enter number like 2
- Show message like
- this number is even num
- And again run show message
- Please enter the number
- user enter like 5
- Show message below
- this number is odd num