In this section, let's learn how to write a simple program in java. Write a simple hello java program after installing JDK . To create a simple java program, you need to create a main
class that contains methods. Let's first understand the requirements.
To execute any java program, you first need:
jdk/bin
the path of the directory, refer to:Now let's create hello java
the program, that is, create a Simple
class with the following code:
|
Save the above code in a file: Simple.java
. Simple.java
Compile the code in the file:
|
|
Output result:
Hello Java
Let's see what class
, public
, static
, void
, main
, String []
, System.out.println()
mean.
class
Keywords are used to declare a class in java.public
The keyword is an access modifier indicating visibility, which means visible to all.static
is a keyword, if a method is declared as static
, it is called a static method. The core advantage of static methods is that they can be called directly without creating an object. main
Methods are executed by the JVM, so it doesn't need to create an object to call the main
method. So it saves memory.void
is the return type of the method, it means it does not return any value.main
Indicates program start (entry of execution).String [] args
Used for command line parameters, which will be learned later.System.out.println()
is the printout statement. We'll look System.out.println
at the inner workings of statements later.Write a simple java program in the editor (Notepad) and save it as a Simple.java
file. To compile and run this program, you can go to Start Menu -> All Programs -> Accessories -> Open Command Prompt .
To compile and run the above program, first change to Simple.java
the directory where the saved file is located. In this example the directory is F:\worksp\javabase
. Enter this directory at the command prompt and enter step by step:
Simple.java
Compile the code in the file:
|
|
Hello Java
There are many ways to write a java program. Modifications that can be made in a java program are as follows:
1) By changing the order of the modifiers, the method prototype does not change.
Let's look at main
the simple code of the method again.
|
2) Java arrays can use post-type, pre-variable or post-variable.
Let's look at main
the code for the different ways of writing the method.
|
var-args
3) Provide support for the main method by passing 3 dots
Let us see the simple code used in the main
method, the usage var-args
that we will learn in the new Java features chapter var-args
.
|
4) The semicolon at the end of a class in java is optional.
Let's take a look at the simple code below.
|
|
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)
If the problem as shown in the figure below occurs, you need to set the path. Since DOS doesn't know javac
or java
command, we need to set it path
. In this case, the path is not required if the program is saved in jdk/bin
the folder. But it is better to set the path, after setting the path, you can use or command javac
anywhere . For Java setup path, please refer to: Java JDK Installation and Configuration