Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Write Your First Program in Java

Learn how to write your first program in java with clear steps, practical context, and useful troubleshooting guidance.

Table of Contents

This updated guide examines How to Write Your First Program in Java and organizes the essential facts, background, and practical takeaways in clear American English.

Method 1

Writing Your First Java Program

  1. How to Write Your First Program in Java — contextual image 1 to start writing programs in Java, set up your work environment. Many programmers use Integrated Development Environments (IDEs) such as Eclipse and Netbeans for their Java programming, but one can write a Java program and compile it without bloated IDEs.
  2. How to Write Your First Program in Java — contextual image 2 Any sort of Notepad-like program will suffice for programming in Java. Hardcore programmers sometimes prefer to use text editors that are within the terminal such as vim and emacs. A very good text editor that can be installed on both a Windows machine and on a linux-based machine (Mac, Ubuntu, etc.) is Sublime Text, which is what we will be using in this tutorial.
  3. How to Write Your First Program in Java — contextual image 3 Make sure that you have theJava Software Development Kitinstalled. You will need this for compiling your program.
    • In a Windows-based operating system, if the environment variables are not correct, you might get an error when runningjavac. Refer the installation article How to Install the Java Software Development Kit for more details about JDK installation to avoid this error.

Method 2

Hello World Program

  1. How to Write Your First Program in Java — contextual image 4 We will first create a program that prints "Hello World. " In your text editor, create a new file and save it as "HelloWorld.java". HelloWorld is your class name and you will need your class name to be the same name as your file.
  2. How to Write Your First Program in Java — contextual image 5 Declare your class and your main method. The main methodpublic static void main(String[] args)is the method that will be executed when the programming is running. This main method will have the same method declaration in every Java program.
    publicclassHelloWorld{publicstaticvoidmain(String[]args){}}
  3. How to Write Your First Program in Java — contextual image 6 Write the line of code that will print out "Hello World. "
    System.out.println("Hello World.");
    • Let's look at the components of this line:
      • Systemtells the system to do something.
      • outtells the system that we are going to do some output stuff.
      • printlnstands for "print line," so we are telling the system to print a line in the output.
      • The parentheses around("Hello World.")means that the methodSystem.out.println()takes in a parameter, which, in this case, is the String"Hello World."
    • Note that there are some rules in Java that we have to adhere to:
      • You must always add a semicolon at the end of every line.
      • Java is case sensitive, so you must write method names, variable names, and class names in the correct case or you will get an error.
      • Blocks of code specific to a certain method or loop are encased between curly brackets.
  4. How to Write Your First Program in Java — contextual image 7 Put it all together. Your final Hello World program should look like the following:
    publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println("Hello World.");}}
  5. How to Write Your First Program in Java — contextual image 8 Save your file and open up command prompt or terminal to compile the program. Navigate to the folder where you saved HelloWorld.java and type injavac HelloWorld.java. This tells the Java compiler that you want to compile HelloWorld.java. If there are errors, the compiler will tell you what you did wrong. Otherwise, you shouldn't see any messages from the compiler. If you look at the directory where you have HelloWorld.java now, you should see HelloWorld.class. This the the file that Java will use to run your program.
  6. How to Write Your First Program in Java — contextual image 9 Run the program. Finally, we get to run our program! In command prompt or terminal, type injava HelloWorld. This tells Java that you want to run the class HelloWorld. You should see "Hello World." show up in your console.
  7. How to Write Your First Program in Java — contextual image 10 Congratulations, you have made your first Java program!

Method 3

Input and Output

  1. How to Write Your First Program in Java — contextual image 11 We will now extend our Hello World program to take input from the user. In our Hello World program, we printed out a string for the user to see, but the interactive part of programs is when the user gets to enter input into the program. We will now extend our program to prompt the user for his or her name and then greet the user by his or her name.
  2. How to Write Your First Program in Java — contextual image 12 Import the Scanner class. In Java, we have some built in libraries that we have access to, but we have to import them. One of these libraries is java.util, which contains the Scanner object that we need to get user input. to import the Scanner class, we add the following line to the beginning of our code.
    importjava.util. Scanner;
    • This tells our program that we want to use the Scanner object which exists in the package java.util.
    • If we wanted to have access to every object in the java.util package, we simply writeimport java.util.*;at the beginning of our code.
  3. How to Write Your First Program in Java — contextual image 13 Inside our main method, instantiate a new instance of the Scanner object. Java is an object-oriented programming language, so it represents concepts using objects. The Scanner object is an example of an object that has fields and methods. to use the Scanner class, we have to create a new Scanner object that we can populate the fields of and use the methods of. To do this, we write:
    ScanneruserInputScanner=newScanner(System.in);
    • userInputScanneris the name of the Scanner object that we just instantiated. Note that the name is written in camel case; this is the convention for naming variables in Java.
    • We use thenewoperator to create a new instance of an object. So, in this instance, we created a new instance of the Scanner object by writingnew Scanner(System.in).
    • The Scanner object takes in a parameter that tells the object what to scan. In this case, we put inSystem.inas a parameter.System.intells the program to scan the input from the system, which is the input that the user will type into the program.
  4. How to Write Your First Program in Java — contextual image 14 Prompt the user for an input. We have to prompt the user for an input so that the user knows when to type something into the console. This can be accomplished with aSystem.out.printor aSystem.out.println.
    System.out.print("What's your name? ");
  5. How to Write Your First Program in Java — contextual image 15 Ask the Scanner object to take in the next line that the user types in and store that in a variable. The Scanner will always be taking in data on what the user is typing in. The following line will ask the Scanner to take what the user has typed in for his or her name and store it in a variable:
    StringuserInputName=userInputScanner.nextLine();
    • In Java, the convention for using an object's method isobjectName.methodName(parameters). InuserInputScanner.nextLine(), we are calling our Scanner object by the name we just gave it and then we are calling its methodnextLine()which does not take in any parameters.
    • Note that we are storing the next line in another object: the String object. We have named our String objectuserInputName
  6. How to Write Your First Program in Java — contextual image 16 Print out a greeting to the user. Now that we have the user's name stored, we can print out a greeting to the user. Remember theSystem.out.println("Hello World.");that we wrote in the main class? All of the code that we just wrote should go above that line. Now we can modify that line to say:
    System.out.println("Hello "+userInputName+"!");
    • The way we chained up "Hello ", the user's name, and "!" by writing"Hello " + userInputName + "!"is called String concatenation.
    • What's happening here is that we have three strings: "Hello ", userInputName, and "!". Strings in Java are immutable, which means that they cannot be changed. So when we are concatenating these three strings, we are essentially created a new string that contains the greeting.
    • Then we take this new string and feed it as a parameter toSystem.out.println.
  7. How to Write Your First Program in Java — contextual image 17 Put it all together and save. Our code should now look like this:
    importjava.util. Scanner;publicclassHelloWorld{publicstaticvoidmain(String[]args){ScanneruserInputScanner=newScanner(System.in);System.out.print("What's your name? ");StringuserInputName=userInputScanner.nextLine();System.out.println("Hello "+userInputName+"!");}}
  8. How to Write Your First Program in Java — contextual image 18 Compile and run. Go into command prompt or terminal and run the same commands as we ran for our first iteration of HelloWorld.java. We have to first compile the program:javac HelloWorld.java. Then we can run it:java HelloWorld.

Sample Java Programs

How to Write Your First Program in Java — contextual image 19Sample Basic Java ProgramHow to Write Your First Program in Java — contextual image 20Sample Java Program with Input

FAQ

What is How to Write Your First Program in Java about?

It provides a structured overview of java, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.