Provide Best Programming Tutorials

Create your very first java application

Overview

This toturial will create your very first Java program with some basic explanation to help you better understand.

Prerequest

  • JDK 1.9
  • Any Text Editor

Steps

  1. Writing a program and save it as HelloWorld.java by using any text editor you like.
  2. Using javac command compile this java file to class file.
  3. Using java command to run the program and checkout the result

Workflow

img

Component

HelloWorld.java

1 public class Welcome{
2     public static void main(String[] args) {
3        //Display Hello World! on the console
4         System.out.println("Welcome to Java!");
5     }
6 }

don’t type line number in your program since i put here for line numbers are for reference purposes only

Each source file can contain one public class. The source file’s name has to be the name of that class. By convention, the source file uses a .java filename extension (a tail end of a file name that marks the file as being of a particular type of file.)

Line 1 defines a class. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is HelloWorld.

Line 2 defines the main method. The main method is the entry point where the program begins execution.

Line 3 is a comment that documents what the program is and how it is constructed.Comments help programmer better understand what the code is really doing. They are not programming statements and thus are ignored by the compiler.For more details about comment in java please refer this link.

A pair of curly braces in a program forms a block that groups the program’s components.In Java, each block begins with an opening brace ({ ) and ends with a closing brace (} ). Every class has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statements in the method.

You have seen several special characters (e.g., { } , // , ; ) in the program. They are used in almost every program. Table 1.2 summarizes their uses.

Tables Special Character

Character Name Description
{} Opening and closing braces Denote a block to enclose statements.
() engine to be used for processing templates. Handlebars is the default. Used with methods.
[] extension to be used for dest files. Denote an array.
// Double slashes Precede a comment line.
” “ Opening and closing quotation marks Enclose a string (i.e., sequence of characters).
; Semicolon Mark the end of a statement.

Using Javac complie source file into java bytecode file

A Java compiler translates a Java source file into a Java bytecode file. The following command compiles Welcome.java:

javac Welcome.java

The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM)

Rather than a physical machine, the virtual machine is a program that interprets Java bytecode. This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems. Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM executes your code along with the code in the library. To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated.

img

Picture below shows how to using the javac and java command to compile and run the program.

Do not use the extension .class in the command line when executing the program.

Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class.

img

Questions

  1. What is the Java source filename extension, and what is the Java bytecode filename extension?
  2. What are the input and output of a Java compiler?
  3. What is the command to compile a Java program?
  4. What is the command to run a Java program?
  5. What is the JVM?
  6. Can Java run on any machine? What is needed to run Java on a computer?

Leave a Reply

Close Menu