First program in Java

New learners have a very excitation for writing first program. In this tutorial we will start by printing "hello world" and then we will write program which will ask for your name and will print is in format "hello! your_name". Let's start. I am assuming you are using eclipse.

Print hello world!

First make a project and inside it make a package with name starting with lowercase. A package is a collection of similar class. Then make a class inside this package. Here my package is findalltogether and class is Tutorials.

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. System.out.print("hello world!");
  7. }
  8. }

This program is small but has many concepts. We will read them all in this tutorial series. Learning programming is slightly confusing in the starting. Take patience until 5th tutorial, you will find it very easy after that. Now take a brief introduction with all terms.

  • In the first line we are telling the package of the file. File is inside the findalltogether package.
  • Third line is defining a class. Public is access specifier determine who can use the definitions that follow. Public means anybody can use this class. We will read about it later. 'class' keyword is defining a new class named Tutorials. A class is collection of similar tasks. The long code of the program is distributed into number of files and classes.
  • Note that there can be maximum one public class in one file and file name must be
  • Line 5 is defining a static method named main. Methods are tasks which contains instruction statements. static keyword tells that this method is not bounded with any object of the class and we can access it without making any object. void is return type. It means that this method is not returning anything.
  • main is a method which executes automatically when we run the program. Every project have must atleast one main method for execution.
  • String is data type which stores text. args is name of collection of strings. We will read about data types in next tutorial.
  • Line 7 is printing "hello world!" without quotes. System is a built-in class (library) of java. out is a static object of this class and print is the method (task) of object type out. This method is used to print something in the console.
  • After 7th line, there is a semicolon. In java syntax, every statement (other than conditional statement, loops, classes and methods) ends with a semicolon.

Note that we call a static object of a class by '.' like ClassName.object and we call a method of an object also using '.' object.method. In object oriented programming we use objects for calls but for static objects or methods we need not to make any object. We can call them directly using name of the class. If still you are confusing with objects then every variable (we will read in next tutorial), method and object are objects of that class.

Take and print your name:

Now we are writing a program which will ask your name and will print your name.

  1. package flowkl;
  2. import java.util.Scanner;
  3. public class Tutorials
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner input = new Scanner(;
  8. System.out.print("write your name ");
  9. String name = input.nextLine();
  10. System.out.println("your name is "+name);
  11. System.out.print("thank you");
  12. }
  13. }
  • In line 3, we are including a library class of java from package java.util using import. We will use this class for taking input in console.
  • In line 8, we are making an object of class Scanner. We make object of a class like this: ClassName objectName. new keyword is initializing this object with a new instance of class Scanner which is taking an argument Argument is extra information that we are sending to the class.
  • If you can not understand it then assume that there is a class Car. We want to use this class then we have to make an object of this class. Car object; But this object is empty we will give it a new car. Every car has a number so we also have to send one more information (argument). So our code will be: Car object = new Car(number)
  • In line 10, we are defining a variable name. Variable is memory space which stores data. When you will run this code name will take a text input in console. Write your name and press enter.
  • Note that in line 11, we are using println instead of print because we want a line break after printing the name. It will change the line after printing line.


Comments are the statements which are ignored by compiler. These are human readable and are written in the program so that program can be understood later easily by same or other programmers.

There are two types of the comments in Java:

  1. Single Line comments: These are one line comments written after double backslashes '//'.
  2. Double Line comments: These can be written with '/*' and '*/' symbols. These can be written in more than one line.
  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // this is single comment
  7. /*
  8. * this is multiline comment
  9. * can occupy more than one line
  10. * '*' for each line is not required
  11. */
  12. int a; // comments can be anywhere
  13. }
  14. }

Reserved Keywords:

Java has some keywords which can not be used as the name of any object, variable, method or class. These keywords are:


Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250