TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Write Your First Java Program
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java BasicJava Tutorials

Write Your First Java Program

Last updated: Oct 15, 2023 10:07 am
By Meenakshi Agarwal
Share
7 Min Read
Write Your First Java Program
Write Your First Java Program
SHARE

This tutorial provides you with a simple and cleaner approach to writing your first Java program. It explains every single line of code to bring clarity to you.

Contents
Pre-requisitesJDKText Editor or IDEObjectiveWrite your first Java programBuild and compile your codeExecute the Java programConclusion

Introduction

Before you get on to writing your first Java program, make sure you know what will you get after learning Java. So, here are some reasons why you should learn Java.

FeatureDescription
Java is popular.It is a programming language that is used by many big companies, such as Google, Amazon, and Netflix.
Java is versatile.You can use it to create many different types of applications, such as web applications, mobile applications, and desktop applications.
Java is portable.The code written in Java can run on many different platforms, such as Windows, Mac, and Linux.
Java is secure.It has built-in features to prevent security vulnerabilities.
Java is object-oriented.Being object-oriented makes it a good language to learn for beginners.

In short, Java is a powerful and versatile programming language that is a good choice for beginners and experienced developers alike. Here are some additional benefits of learning Java:

BenefitsA Short Description
High salary potentialJava developers are in high demand and command high salaries.
Job securityJava is a mature language with a large community of developers, so there is a lot of job security in the Java field.
Learning curveJava is a relatively easy language to learn, so you can start developing applications quickly.
Open sourceJava is an open-source language, so there are many free resources available to help you learn.

If you are interested in learning to code, Java is a great language to start with. It is a powerful, versatile, and in-demand language that can open up many opportunities for you.

Learn to Write Your First Java Program

The tutorial has the following sections to help you learn quickly.

Pre-requisites

JDK

Before you paddle up to write your first Java program, make sure you have the Java SE development kit installed on the system.

You can confirm it in many ways:

1) On Windows, open the “Run” window (Press Win+R), and type “appwiz.cpl” to launch the “Programs and Features” dialog. Search for the Java SE Development Kit. Or you can directly go to the “C:\Program Files” or “C:\Program Files (x86)\” directory, and check if it has some “jdkX.X.X_XX” folder inside.

2) On Linux or Mac OS, open the terminal (CTRL+T), and run the following commands to check Java availability.

# check if java is installed
whereis java

# check java location
which java

In case, you don’t find Java on your system, then download and install the latest version from the formal Oracle site. Once you have everything set up, then check if the JAVA_HOME variable exists or not.

Open the console window, and issue one of the following commands as per the OS you are using:

# On Windows
echo JAVA_HOME = %JAVA_HOME%

# On Linux or Mac OS
echo JAVA_HOME = $JAVA_HOME

By the way, if you don’t have the above variable set, then don’t worry and add one yourself. Adding JAVA_HOME to your environment is not only a good practice but useful too.

a) To do it on Windows, open the “Edit environment variables” dialog and set up the JAVA_HOME pointing to the Java installation directory.

b) To do it on Linux or Mac OS, update the user profile “~/.profile” by adding an entry of JAVA_HOME.

Text Editor or IDE

You can either pick a text editor (like Notepad or Notepad++ on Windows and Vi/Vim on Linux/Mac OS) or an IDE (like Eclipse or NetBeans or CodeBlocks) to write Java code.

It’s an ideal practice to begin using an IDE (Integrated Development Environment) so that you can avoid the headache of manually compiling the code and rectifying errors highlighted by the interpreter.

Don’t miss to read the Java Coding Guidelines.

Objective

Since time, newbie programmers have traditionally used the “Hello World!” as their first programming exercise.

In this tutorial also, we’ll be using it to illustrate the basics of Java programming syntax and code flow.

Write your first Java program

First, launch either a text editor or the IDE that you have chosen. Create a file named “MyFirstProgram.java” and save it. Next, here are some thumb rules for you to remember.

1. Every Java program has a main class, and its name must match the name of your Java file (i.e., MyFirstProgram in our case). Also, you need to use “public” access modifier before it.

// Class name should match the name of the Java file
public class MyFirstProgram {

2. As per standard naming conventions, Java classes should start with a capitalized character, and follow the camel case format (in which the first letter of each word is capitalized).

// The class name remains in CamelCase format
public class << MyFirstProgram >> {

3. The rules suggest against underscores (_) and dollar signs ($).

Also, note that every main class has an entry point routine called “main.” You place the main business logic in this function.

class MyFirstProgram {
public static void main(String args[]) {

Now, check out the following lines of code and type them into the “MyFirstProgram.java” file.

// MyFirstProgram.java

public class MyFirstProgram {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

After seeing the above code, you might have observed a few keywords used there. Let’s understand their meaning and purpose.

Public: It is an access modifier indicating that you can call the main from anywhere, even from outside the program.

Static: It is a keyword that means the function can exist and run by itself (without creating an object).

Void: Another keyword, it means that the function doesn’t return any value.

Build and compile your code

Open a terminal window and change the current directory to where your Java file is available:

cd /path_to_file/

Once done, the next line to write is:

# Build and compile java program
javac MyFirstProgram.java

Sometimes, you get the error that the system didn’t find the “javac” command or it’s an inoperable program or batch file. In such a case, check if JDK exists and its environment variable points to the correct path.

Once successful, the compiler will at that point produce a bytecode file called MyFirstProgram.class. Java Virtual Machine (JVM) interprets and executes this file.

C:\>dir MyFirstProgram.*
Volume in drive C has no label.
Volume Serial Number is 9050-B24D

Directory of C:\

03/23/2019 12:27 AM 434 MyFirstProgram.class
03/23/2019 12:27 AM 156 MyFirstProgram.java
2 File(s) 590 bytes
0 Dir(s) 13,673,246,720 bytes free

The Java programming dialect compiler (javac) pursues source files written in the Java programming dialect and turns them into bytecode. Alternatively, the compiler can likewise process comments found in the source utilizing the Pluggable Annotation Processing API.

Execute the Java program

For the final step, use the following command:

# Run the java program
java MyFirstProgram

If you see the output, then you have successfully coded your very first Java program!

C:\>java MyFirstProgram
Hello World!

Conclusion

If the insight into the program was too much for once, don’t worry about it.

As you make progress with your Java skills, you will start realizing the need for every keyword or access modifier used in your very first program.

For the time being, take a moment to appreciate your first-ever Java code.

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article JVM vs JRE vs JDK Introduction to JVM, JDK, JRE
Next Article Java Data Types Explained

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x