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.
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.
Steps
Write your first Java 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
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 ) { } }
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 thatSystem.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.
public class ChaoThegioi { public static void main ( String [] args ) { System . out . println ( "Hello World." ); } }
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. java ChaoThegioi
. This code tells Java that you want to run the ChaoThegioi class. The words “Hello World.” will appear on your monitor screen. Input and output
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.
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 writingnew 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.
System.out.print
or System.out.println
code.
System.out.print("What's your name?");
String userInputName = userInputScanner.nextLine();
- In Java, the convention for using an object’s method is
objectName.methodName(parameters)
. InuserInputScanner.nextLine()
, we call the Scanner object by the name we assigned it and then we call its methodnextLine()
, 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.
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
.
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 + "!"); } }
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.
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: