How to Write a Simple JAVA Program

How to Write a Simple JAVA Program

In this topic, we will understand and write a simple Java program (Assuming Java is installed on your system). A Program is all about step by step instructions to be executed by the computer to achieve our requirement in a quick and efficient way. To understand Java basics we will write a simple program to print “Hello! RoundTheTech”.

The prerequisites to start the Java Programs

  • Java installation should be completed on your system. Downloading JDK URL is https://www.oracle.com/java/technologies/javase-downloads.html
  • Set Java PATH and CLASSPATH environment variables. For more information go to the article How to Set Java Environment Variables.
  • Create the Java program
  • Compile the Java program
  • Run the java program

Java Example: 

package com.java;
public class FirstTry {
public static void main(String[] args) {
System.out.println(“Hello! RoundTheTech.”);
}
}

We will save this example’s source code as FirstTry.java. The compiler will use this source code file to compile and will convert it into the bytecode named FirstTry.class. This FirstTry.class would be available for JVM to execute the program.

This Java Program can be developed using any text editor or using the Integration Development Environment(IDE)

How to create Java program using text Editor

  • Write the Java Source Code in any text editor like Notepad/Notepad++.
  • Save the File with .java extension. like FirstTry.java.
  • Open command prompt
  • Set Java PATH environment variable ( For help go to the URL )
  • Run ‘javac’ command to compile the program. For detailed information check the article
Compile Your Program
Compile Your Program
  • After compilation you will see, one more file has been generated with .class extension. Like FirstTry.class
  • Set CLASSPATH. (To take help you can go to the URL )
  • Run the Java class using the “java” command to see the output.
Run Your Program
Run Your Program

How to create a Java Program using Eclipse Integration Development Environment(IDE)

  1. Open Eclipse IDE and create a Workspace
Open Eclipse IDE and create a Workspace

2. Create new Project by clicking on option File->New->Project

Create new Project by clicking on option File->New->Project

3. Select ‘Java Project’ or ‘type of project you want to develop’ from Wizard option and click on ‘Next’ button

4. New Java project screen will appear and do as below:

  • Give a project name by which you want to create a Java project.
  • By default the new project will be created in a workspace location. But if you want to create a new project on another location, uncheck “use default location” checkbox and give your desired location.
  • Configure JRE for this newly created project. For the same select configure JRE under JRE section and give JDK installation location. 
  • By default separate folders are created for the source and class files as ‘src’ and ‘bin’ respectively. For any change in this default setting, you can do it through the ‘project layout’ section. 
  • Click next
Java Project Screen

5. In your system, the new project will be created at your given project location. (as per screenshot project will be created in workspace location). Close welcome screen if appeared. 

6. You can see your created project in eclipse IDE.

7. Create new java package

  • By clicking on new ‘New Java Package’ icon on eclipse toolbar
  • By right-click on src->new->Package
src->new->Package
  • Package screen will appear, give package name
Give Package Name

8. Create New class 

  • By clicking on new ‘New Java Class’ icon on eclipse toolbar
  • By right-click on src->new->Class
 SRC->NEW->Class
  • Give class Name
Give class Name

9. Write java code in the created class. Class will automatically be compiled and will lie in the class folder.
By default compiled source code goes in the bin folder.

10. Run the class to see the output. You can run the class and see the output on the console only when it has the main() method. You can run java class in eclipse using different ways:

  • Right-click in the class ->Run As->java application
  • Click on the eclipse Run menu-> Run option. Selected class will be executed and you can see the output in the eclipse console.
  • Click on the Eclipse toolbar icon Run <Class name>. Selected class will be executed and you can see the output in the eclipse console.
Eclipse Class Run

How to Set JRE in Eclipse

(Assuming Java is installed in C:\Program Files\Java\jdk1.8.0_211 location)

  • In new Java project screen, select configure JRE under JRE section
    Or 
    Go to Eclipse ‘Window’ menu->Preferences->Java->Installed JREs
  • Click on Add/Edit button
  • Click on installed JRE location
  • Click on the Finish button.
Installed-JRE-Location
Installed-JRE-Location

Explanation of Java Program

In this program, we used keywords – package, class, public, static, void, main, String[], System.out.println(). Always you should keep in mind that Java is a case sensitive language, so you have to use the same java keyword as mentioned. If there will be any mistake in the alphabet case it will not work, like if main() is typed as Main(), Program will not be executed.

package

‘package’ is a java keyword used in small cases only.  The Java package is a collection of related classes, sub-packages, and interfaces. Generally we keep related classes into the same package for easy management. For example- w created the java.util package, then all utility classes will go into this package. We can easily write our own class, and on a requirement basis, we can import other class(es) from the existing or another package.

packages are used for:

  • It gives the facility to avoid name conflicts in java. Two java classes/Interfaces can have the same name but in different packages. Like Account named class can exist in two different packages: com.java.classsubject and com.java.department.
  • java libraries are made using packages, classes, and interfaces that are easy to manage. While writing a class, on a requirement basis you can import a single class (along with its methods and attributes), or a complete package that contains all the classes.
  • Package makes development easy in terms of searching/locating and usage of classes, interfaces, enumerations, and annotations.
  • It helps to provide controlled access to classes, interfaces, methods, and attributes.
  • We can consider to Packages as data encapsulator.

class

In Java, the class keyword is used to declare a class. The class keyword is written followed by the Class Name. It is used in small cases only. In Java, everything is written inside the class.

Class-Name is not a keyword while it is a name by which Java class would be identified. Class Name could have more than one word without space and it is always unique. Class Name could be the combination of alphabets and digits but the First letter would be an alphabet. It is given followed by a class keyword and always given in CamelCase means the first letter of each word will be in capital letters. Like in our example- The Class Name is FirstTry. In FirstTry there are 2 words First and Try. Both words (First and Try) are written without space and both of the words’ first letter is capital.

public

As we know public means open to everyone and in the same way, it is used in java, if a class is defined as public it means it will be available to every other class. It is used in small cases only. In Java, the public is a keyword and used as an access modifier. Access modifier means the visibility or scope of a class, constructor, variable, method, or data member. In the given example, the public keyword is used for method main(),  which will allow this main method to be visible out of this class too.

static

It is a keyword and is given in small cases only. The static keyword can be used with variables, methods, and blocks in a class and known as static members. Static members in class do not belong to instance while Static members belong to the class. So these members are invoked directly by class name and no need to create an object to invoke them. For static members, a single copy is created in memory which is shared among all the objects.

JVM calls main() to execute the program. It is not required to create an object to invoke the main method. Since main() is created as a static method followed by static keyword

void

void means will not return anything. In Java, void is a return type of a method. It is used followed by method name. If a method name is written followed by a void keyword it means that method will not return any value.

main

main is a method name in Java and used in small cases only. It is used by JVM as a starting point to execute the program.

String[] args

‘String[]’ is a  String type Array parameter while ‘args’ is the parameter name in main() method. It is used to pass the argument values to the function. If you want to pass any value(s) as an input to the function you can pass using this command line argument array.

System.out.println()

It is a statement used to print the message(s) or argument(s) passed to it on console, like in the given example it is used to print “Hello Round the Tech”. In this statement:

  • system: is a final class name written under the java.lang package. It contains several useful methods and class fields. It can not be instantiated.
  • out : is an instance of java.io.PrintStream class in System class. It a static member of the System class, since called directly by class name(System.out)
  • println():  is a method in java.io.PrintStream class under java.io package.

Java programs can be written in Notepad or Notepad++ or any other text editor. You can also use any advanced IDE(Integrated Development Editor) that supports Java to write java programs. For beginners, my suggestion is to write some simple programs using a text editor such as Notepad/Notepad++(my choice is Notepad++) to understand the actual underlying concept and workflow of the program. Although for project development, we use Java Integrated Development Environment (IDE) too. Using IDEs, development becomes fast and easy. Generally for Java development, developers prefer to use Eclipse, NetBeans, IntelliJ IDEA, Android Studio because of these development favorable features. Some of the famous IDEs are mentioned below:

  • Eclipse
  • NetBeans
  • IntelliJ IDEA Community Edition
  • Android Studio
  • Enide Studio 2014
  • BlueJ
  • jEdit
  • jGRASP

For More Information about Java IDEs, Please Visit Post: Best and Popular IDEs for Java Development 2020

Leave a Reply

Your email address will not be published. Required fields are marked *