Computer Science, asked by abhrajitsen3702, 10 months ago

Important things to put when we write a java program

Answers

Answered by nuan
0

Answer:

Image titled 91968 11

In order 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.

Image titled 91968 2

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.

Image titled 91968 3

3

Make sure that you have the Java Software Development Kit installed. 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 running javac. 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

Image titled 91968 4

1

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.

Image titled 91968 5

2

Declare your class and your main method. The main method public 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.

public class HelloWorld {

public static void main(String[] args) {

}

}

Image titled 91968 6

3

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:

System tells the system to do something.

out tells the system that we are going to do some output stuff.

println stands for "print line," so we are telling the system to print a line in the output.

The parentheses around ("Hello World.") means that the method System.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.

Image titled 91968 7

4

Put it all together. Your final Hello World program should look like the following:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World.");

}

}

Image titled 91968 8

5

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 in javac 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.

Image titled 91968 9

6

Run the program. Finally, we get to run our program! In command prompt or terminal, type in java HelloWorld. This tells Java that you want to run the class HelloWorld. You should see "Hello World." show up in your console.

hope it helps

Similar questions