• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2021
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

Java Program Basic | Hello World Program Explained for Beginners

Heena Rajpal/13881/0
CodeJAVA
YouTube CSEstack

Since we are thorough with the basics of Java now, it is the time that we create our first Java program.

Yes, we’ll create the conventional Hello World program first to see how lines of code of Java look.  We further delve into the details explanation of some Java program basic questions.

You can create your first program once you have installed Java on your system. You can type the first program in Notepad, Notepad++ or any other text editor or code editor.

Hello World Program in Java:

If it is your first program, open basic notepad editor. And write following code.

class FirstDemo
{
    public static void main (String args [])
    {
        System.out.println(“Hello, World!”);
    }
}
Note: The program is saved with the name same as that of main class with an extension ‘.java’.

Hence our program will be saved as FirstDemo.java.

That is it! Our first Java program is ready. Isn’t it simple?

Understanding each line of this program is very necessary if you want to learn Java.

Before running this program here is complete explanation guide of your first program.

Though very concise and simple, we need to notice a few things:

Java Program Basic Questions:

These following basic questions are very important if you want to excel in Java programming. If you are preparing for an interview, you must know.

Can we write a Java program without Class?

No!

First of all, every Java program is going to have at least one class.

The class that contains the main function is often referred to as the main class. We always declare the main function inside a class.

How can we write Java program without function?

However, you can write a Java program without any function which is contrary to many of other programming languages like C and C++.

class FirstDemo
{
    static
    {
        System.out.println(“Hello, World!”);
    }
}

Why do we write public static void main in Java?

Unlike C and C++, we do not simply declare void main() or int main(). We make the function public as well as static.

Note that we will compile and run our program on the command line. The interpreter needs access to the main function to run it. Hence, the main function has to be public.

We make the function static so that we can directly call the function on the command line with the class name without creating its object.

Why is string args used in Java?

String args [] is used to pass string arguments to the main function.

Here the arguments are blank []. However, they can be used to pass strings which are helpful due to various reasons which we will discuss after creating strong foundations for ourselves in Java.

What is the meaning of System.out.println()?

As we have already discussed, System is a class in Java’s java.lang package which is the main (root) package in Java.out is a static member of System class and represents output buffer.

  • System is a class in Java’s java.lang package which is the main (root) package in Java.
  • Here, out is a static member of System class and represents output buffer.
  • The keyword println is an overloaded function used for printing the output.

This line will always be used to print anything in Java.

Comment in Java:

Commands are very important for any programming languages. It gives readability to your program so that other people can understand your code in human readable format.

In Java, we can put single line as well as multiline comments.

  • Single line comment use //
  • Multiline comments come between /*…..*/.

Compile and Run Hello World Program:

Let us see how to compile and run the program on Windows.

I consider you have saved classpath and path variables for Java as I described in JDK installation guide.

Now, Go to command line and change the current directory to the directory where the program is saved.

To compile the program, type:

javac FirstDemo.java

This step will create a ‘.class’ file wherever your program is saved. This .class file can be run on any platform.

To run the program, type in the command prompt:

java FirstDemo

Output:

You will see the output on your screen as below.

Hello, World!

So here we are done with the creation of our first Java program.

Let us create another small program where we have a look at the use of variables at the very basic level.

Exercise: Write a simple Java program to add two numbers.

class AdditionDemo
{
    public static void main (String args [])
    {
        int a=20;
        int b=10;
        int c;
        c=a+b;
        System.out.println(“The sum is ”+c);   //Concatenation operator
    }
}

Just run this program to see the output. If you are not aware of integer variables, you don’t need to worry. I am going to describe it in next sections of this tutorial.

Any doubt running your first program or related to Java program basic? Start the discussion in a comment section.


« JDK Java Installation Guide Basic Datatypes in Java »

CodeJava
Heena Rajpal
Heena Rajpal is pursuing Computer Science Engineering from Indore. She has a knack for writing and an inquisitive outlook towards Computer Science fields like Database Management Systems, Object Oriented Programming and languages like C, C++, JAVA, Python, HTML etc.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Exception Handling
  11. Java- ‘throw’ vs ‘throws’ Keyword
  12. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- HashSet
  5. Java- HashMap vs HashSet

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2021 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard