• About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy Policy
  • Change the purpose of use

Tnhelearning.edu.vn - Various useful general information portal

  • Photo
  • Bio
  • How To
  • Tech

How to Write Your First Program with Java

February 20, 2024 by admin Category: How To

You are viewing the article How to Write Your First Program with Java  at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.

X

wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 76 people, some of whom are anonymous, have edited and improved the article over time.

This article has been viewed 3,384 times.

Java is an object-oriented programming language introduced in 1995 by James Gosling. That is, it represents concepts like “objects” and “fields” (which are properties that describe objects). Java is a “write one place, run another” language: it’s designed to run on any platform that has a Java Virtual Machine (JVM). As a multilingual programming language, Java is quite easy to learn and understand for beginners. This article is an initial introduction to Java programming.

Table of Contents

  • Steps
    • Write your first Java program
    • Hello World Program
    • Input and output
  • Advice

Steps

Write your first Java program

Image titled 91968 1

Image titled 91968 1

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/29/91968-1.jpg/v4-728px-91968-1.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/2/29/91968-1.jpg/v4-728px-91968-1.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
To start programming with Java, you need to set up a working environment. Many programmers use Integrated Development Environments (IDEs), such as Eclipse and Netbeans, for Java programming. However, you can still write and compile Java programs without them.
Image titled 91968 2

Image titled 91968 2

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/8/8e/91968-2.jpg/v4-728px-91968-2.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/8/8e/91968-2.jpg/v4-728px-91968-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Any program similar to Notepad is enough to program with Java. Sometimes conservative programmers still prefer to use the text editors included in the terminal, such as vim and emacs. Sublime Text is a good text editor that can be installed on both Windows and Linux-based computers (Mac, Ubuntu, etc.). It is also the editor used in this tutorial.
Image titled 91968 3

Image titled 91968 3

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/f/f4/91968-3.jpg/v4-728px-91968-3.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/f/f4/91968-3.jpg/v4-728px-91968-3.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Make sure that the Java Software Development Kit is installed. You will need it to compile the program.

  • On computers using Windows operating systems, if the environment variables are not correct, there may be an error when running javac . Please refer to the article on how to install Java Software Development Kit to avoid this error.

Hello World Program

Image titled 91968 4

Image titled 91968 4

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/6c/91968-4.jpg/v4-728px-91968-4.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/6c/91968-4.jpg/v4-728px-91968-4.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
First, we will create a program that prints the text “Hello World.” In a text editor, create a new file and save it as “ChaoThegioi.java”. ChaoThegioi is your class name and this class name needs to match the file name.
Image titled 91968 5

Image titled 91968 5

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/e/eb/91968-5.jpg/v4-728px-91968-5.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/e/eb/91968-5.jpg/v4-728px-91968-5.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Declare the main class and method. The main method public static void main(String[] args) is the method that will be executed when the program runs. The declaration is the same in all Java programs.

 public class ChaoThegioi { public static void main ( String [] args ) { } } 
Image titled 91968 6

READ More:   How to Paint Eyebrows

Image titled 91968 6

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/2d/91968-6.jpg/v4-728px-91968-6.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/2/2d/91968-6.jpg/v4-728px-91968-6.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Write a command line that prints the words “Hello World.”

 System . out . println ( "Hello World." );
  • Let’s look at the composition of this command line:
    • System tells the system to do something.
    • out tells the system that we are going to do something with the output.
    • println stands for “print line” and with it we are asking the system to print a line in the output.
    • The outer parenthesis ("Chào Thế giới.") indicates that System.out.println() method takes one parameter, and in this case, the String "Chào Thế giới."
  • Note that in Java there are some rules that we must follow:
    • Always end lines with a semicolon.
    • Java distinguishes between uppercase and lowercase. Therefore, to avoid errors, you must write the method name, variable name and class name in correct uppercase or lowercase.
    • The private code block of a given method or loop is enclosed in curly braces.
Image titled 91968 7

Image titled 91968 7

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/1/1e/91968-7.jpg/v4-728px-91968-7.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/1/1e/91968-7.jpg/v4-728px-91968-7.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Merge. Your final Hello World program will look like this:

 public class ChaoThegioi { public static void main ( String [] args ) { System . out . println ( "Hello World." ); } } 
Image titled 91968 8

Image titled 91968 8

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/65/91968-8.jpg/v4-728px-91968-8.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/65/91968-8.jpg/v4-728px-91968-8.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Save the file and open a command prompt or terminal interpreter. Navigate to the folder where you saved ChaoThegioi.java and type javac ChaoThegioi.java . This code will tell the Java compiler that you want to compile ChaoThegioi.java. If there is an error, the compiler will tell you where you made the mistake. If there were no errors, there would be no message from the compiler. Now, look in the ChaoThegioi.java directory, you will see ChaoThegioi.class. This is the Java file used to run your program.
Image titled 91968 9

Image titled 91968 9

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/f/f6/91968-9.jpg/v4-728px-91968-9.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/f/f6/91968-9.jpg/v4-728px-91968-9.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Run the program. Finally, we have to run the program! At the command prompt or terminal, type java ChaoThegioi . This code tells Java that you want to run the ChaoThegioi class. The words “Hello World.” will appear on your monitor screen.
Image titled 91968 10

Image titled 91968 10

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/1/11/91968-10.jpg/v4-728px-91968-10.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/1/11/91968-10.jpg/v4-728px-91968-10.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Congratulations, you’ve written your first Java program!

Input and output

Image titled 91968 11

Image titled 91968 11

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/ba/91968-11.jpg/v4-728px-91968-11.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/b/ba/91968-11.jpg/v4-728px-91968-11.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Now, we will extend the Hello World program to take input from the user. In this program, we have printed a string of characters that can be read by the user. However, the interactive part of the program lies in the user inputting it. Now, we’ll extend the program, ask the user for a name, and then greet them by name.
Image titled 91968 12

Image titled 91968 12

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/f/fa/91968-12.jpg/v4-728px-91968-12.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/f/fa/91968-12.jpg/v4-728px-91968-12.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Import Scanner class. In Java, you can access a number of built-in libraries. However, to use them, we need to import them into the program. One of those libraries is java.util, which contains the Scanner object that we need to get information from the user. To import the Scanner class, add the following line at the beginning of the program.

 import java.util.Scanner;
  • This command tells the program that we want to use the Scanner object available in the java.util package.
  • To access all objects in the java.util package, simply write import java.util.*; at the beginning of the program.
READ More:   How to Kill Small Ants
Image titled 91968 13

Image titled 91968 13

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/d/d3/91968-13.jpg/v4-728px-91968-13.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/d/d3/91968-13.jpg/v4-728px-91968-13.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
In the main method, create a new instance of the Scanner object. Java is an object-oriented programming language, so it represents the concept of using objects. Scanner is an instance of an object with fields and methods. To use the Scanner class, we must create a new Scanner object – we will be able to add fields and use its methods. To do so, we write:

 Scanner userInputScanner = new Scanner(System.in);
  • userInputScanner is the name of the Scanner object we just created. Note that this name is written in CamelCase form (meaning words are written next to each other, the first letter of each word is capitalized) – this is the Java variable naming convention.
  • We use the new operator to create a new instance of an object. In this case, we created a new instance of the Scanner object by writing new Scanner(System.in) .
  • The Scanner object takes a parameter indicating what to scan. In this case, we enter System.in as a parameter. System.in asks the program to scan input from the system, which is the input that the user will type into the program.
Image titled 91968 14

Image titled 91968 14

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/1/17/91968-14.jpg/v4-728px-91968-14.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/1/17/91968-14.jpg/v4-728px-91968-14.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Ask the user to enter information. You will have to tell the user when to type something on the control screen. This can be done with the System.out.print or System.out.println code.

 System.out.print("What's your name?"); 
Image titled 91968 15

Image titled 91968 15

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/8/8d/91968-15.jpg/v4-728px-91968-15.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/8/8d/91968-15.jpg/v4-728px-91968-15.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Tell the Scanner object to receive the next line the user typed and store it as a variable. Scanner will always receive user input data. The next line will tell the Scanner to take the user’s input and store it in a variable:

 String userInputName = userInputScanner.nextLine();
  • In Java, the convention for using an object’s method is objectName.methodName(parameters) . In userInputScanner.nextLine() , we call the Scanner object by the name we assigned it and then we call its method nextLine() , which takes no parameters.
  • Notice that we are storing the next line in another object: the String object. We have named userInputName for this object.
Image titled 91968 16

Image titled 91968 16

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/1/15/91968-16.jpg/v4-728px-91968-16.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/1/15/91968-16.jpg/v4-728px-91968-16.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Print greetings to the user. Now that the user name has been stored, we can print greetings to them. Remember the code System.out.println("Chào Thế giới."); that we wrote in the main class? Any code that we just wrote will come before that line of code. Now we can adapt that line of code to:

 System.out.println("Hello " + userInputName + "!");
  • How do we combine “Hello”, username and “!” with "Xin chào " + userInputName + "!" called String concatenation.
  • Here, we have three string of characters: “Hello”, userInputName, and “!”. In Java, String literals are immutable. So when we concatenate these three strings together, we are essentially creating a new string of characters that contains the greeting.
  • Next, we’ll take this new string and enter it as a parameter into System.out.println .
Image titled 91968 17

READ More:   How to Draw Abstract Painting

Image titled 91968 17

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/28/91968-17.jpg/v4-728px-91968-17.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/2/28/91968-17.jpg/v4-728px-91968-17.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Recombine and save. We have the following program:

 import java.util.Scanner;

public class ChaoThegioi {
    public static void main(String[] args) {
        Scanner userInputScanner = new Scanner(System.in);
        System.out.print("What's your name? ");
        String userInputName = userInputScanner.nextLine();
        System.out.println("Hello " + userInputName + "!");
    }
} 
  • Image titled 91968 18

    Image titled 91968 18

    {“smallUrl”:”https://www.wikihow.com/images_en/thumb/4/4b/91968-18.jpg/v4-728px-91968-18.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/4/4b/91968-18.jpg/v4-728px-91968-18.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
    Compile and run. Go to the command prompt or terminal and run with the command we used in the first run of ChaoThegioi.java. First, we have to compile the program: javac ChaoThegioi.java . Then we can run it: java ChaoThegioi .
  • Advice

    • Java is an object-oriented programming language, so you should read more about the basics of this programming language.
    • Object-oriented programming has many unique features. Three of the main features are:
      • Encapsulation : the ability to limit access to certain elements of an object. Java uses keywords to define private, protected, and public modes of fields and methods.
      • Polymorphism : the ability to take on multiple identities of an object. In Java, an object can be cast into another object to use that object’s methods.
      • Inheritance : the ability to use fields and methods from a class that is in the same hierarchy as the current object.
    X

    wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, 76 people, some of whom are anonymous, have edited and improved the article over time.

    This article has been viewed 3,384 times.

    Java is an object-oriented programming language introduced in 1995 by James Gosling. That is, it represents concepts like “objects” and “fields” (which are properties that describe objects). Java is a “write one place, run another” language: it’s designed to run on any platform that has a Java Virtual Machine (JVM). As a multilingual programming language, Java is quite easy to learn and understand for beginners. This article is an initial introduction to Java programming.

    Thank you for reading this post How to Write Your First Program with Java at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.

    Related Search:

    Related Posts

    How to Create Curved Text in Photoshop
    How to fall asleep faster
    How to Install FBReader to Read eBooks

    Category: How To

    Previous Post: « How to Describe Symptoms to a Doctor
    Next Post: How to Install Minecraft »

    Copyright © 2025 · Tnhelearning.edu.vn - Useful Knowledge