How to Write Your First Program With Java

Author: Monica Porter
Date Of Creation: 15 March 2021
Update Date: 27 June 2024
Anonim
Creating your first Java application with IntelliJ IDEA
Video: Creating your first Java application with IntelliJ IDEA

Content

Java is an object-oriented programming language introduced by James Gosling in 1995. That is, it represents concepts like "object" and "field" (which are properties that describe the object). Java is a "write one place, run somewhere else" language: it is designed to run on any platform that has 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.

Steps

Method 1 of 3: Write your first Java program

  1. To start programming with Java, you need to set up your work environment. Many programmers use Integrated Development Environments (IDE), such as Eclipse and Netbeans, to program Java. However, you can still write and compile Java programs without them.

  2. Any program similar to Notepad is sufficient to program with Java. Conservative programmers sometimes prefer to use text editors that are in terminal, such as vim and emacs. Sublime Text is a good text editor that can be installed on both Windows computers and Linux-based machines (Mac, Ubuntu, etc.). It is also the editor used in this guide.
  3. Make sure that Java Software Development Kit installed. You will need it to compile the program.
    • On Windows computers, if the environment variables are not correct, there may be an error running javac. Please refer to the article on how to install the Java Software Development Kit to avoid this error.
    advertisement

Method 2 of 3: Hello World Program


  1. First, we will create a program that prints the words "Hello World."In the text editor, create a new file and save it as" ChaoThegioi.java ". ChaoThegioi is your class name and this class name should match the filename.
  2. Declare main class and method. Main method public static void main (String args) is the method that will be executed when the program is run. The declaration is the same in all Java programs.

  3. Write the code with the words "Hello World.
    • Let's look at the components 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're asking the system to print a line at the output.
      • Outer parentheses ("Hello World.") Indicates the method System.out.println () takes one parameter, and in this case it's String "Hello World."
    • Note that in Java there are a few rules that we must follow:
      • Always end with a semicolon.
      • Java distinguishes uppercase and lowercase letters. Therefore, in order to avoid errors, you must write the method name, variable name, and class name in the correct case.
      • The private code block of a given method or loop is enclosed in curly brackets.
  4. Merge. Your final Hello World show should look like this:
  5. Save the file and open your command line or terminal interpreter. Navigate to the folder where you saved the ChaoThegioi.java and type javac ChaoThegioi.java. This code will tell the Java compiler that you want to compile ChaoThegioi.java. If there are errors, the compiler will tell you where you made it. If there are no errors, then there should be no messages from the compiler. Now, look at the ChaoThegioi.java directory, you will see ChaoThegioi.class. This is the Java file you use to run your program.
  6. Run the program. Finally, we must 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.
  7. Congratulations, you've written your first Java program! advertisement

Method 3 of 3: Input and output

  1. Now, we will expand the Hello World program to get user input. 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 that the user will input it. Now, we will expand the program, ask the user to enter a name and then send a specific greeting to them.
  2. Enter 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 we need to get information from the user. To enter the Scanner class, we add the following line at the beginning of the program.
    • This command line tells the program that we want to use the Scanner object available in the java.util package.
    • To access every object in the java.util package, we just need to write import java.util. *; at the beginning of the show.
  3. In the main method, create a new instance of the Scanner object. Java is an object-oriented programming language, so it represents the concepts of object usage. Scanner is an example 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:
    • userInputScanner is the name of the Scanner object we just created. Note that this name is written in the CamelCase form (i.e., words are written in a row, the first letter of each word is capitalized) - this is the variable naming convention in Java.
    • We use operators new 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 receives a parameter telling you what to scan. In this case, we enter System.in as a parameter. System.in asks the program to scan the input from the system, which is the input the user will type into the program.
  4. Ask user to enter information. You will have to ask the user to know when to type something on the console screen. This can be done with code System.out.print or System.out.println.
  5. Tell the Scanner object to receive the next line the user has typed in and save it as a variable. The Scanner will always receive the data the user entered. The next line will ask the Scanner to receive the user's input and store it in a variable:
    • In Java, the convention of using methods of an object is objectName.methodName (parameters). In userInputScanner.nextLine (), we call the Scanner object by the name we have assigned to it and then we call its method. nextLine (), this method takes no parameters.
    • Notice that we are storing the next line in another object: the String object. I have given a name userInputName for this object.
  6. Print greeting to the user. Now that the username has been stored, we can print a greeting to them. Remember the code System.out.println ("Hello World."); that we wrote in the main class? Any code that we just wrote will be before that line of code. Now we can modify that line of code to:
    • The way we combine "Hello", the username and the "!" with "Hello" + userInputName + "!" is called String concatenation.
    • Here, we have three strings of characters: "Hello", userInputName, and "!". In Java, String is immutable. So when we put these three strings together, we are essentially creating a new string containing the greeting.
    • Next, we will take this new string and enter it as an argument System.out.println.
  7. Merge and save. We have the following program:
  8. Compile and run. Go to the command prompt or terminal and run it with the command we used in the first run of ChaoThegioi.java. First, we have to compile the program: javac ChaoThegioi.java. Next, we can run it: java ChaoThegioi. advertisement

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:
    • Packing properties: the ability to limit access to certain components of the object. Java uses keywords to define private, protected, and public modes of fields and methods.
    • Polymorphism: the ability to recognize multiple object identifiers. In Java, an object can be passed into another object using the methods of that object.
    • Inheritance: the ability to use fields and methods from the class that are in the same hierarchy as the current object.