Java Tutorial – First Java Class, Compiling and Execution

Java Tutorial – First Java Program, Compiling and Execution

In this tutorial we will work on creating our very first Java program. We will discuss and dissect the different parts of a java program. In addition, we will be compiling our java program using javac the java compiler into bytecode. Finally, we will will execute our simple application and show the results.

What’s Covered

  1. My First Java Class (MyFirstClass.java)
  2. Output of My First Java Class
  3. Dissecting the Structure of the Java Source File
  4. Compiling our Java Source File
  5. Executing our Compiled Java Class

My First Java Class (MyFirstClass.java)

public class MyFirstClass {

  public static void main(String[] args) {

    System.out.println("Welcome to my first java program...");

  }

}

Output of My First Java Class

Welcome to my first java program...

Dissecting the Structure of the Java Source File

first_program_structure

Structure of the Java Class in detail

  • Create a class called MyFirstClass
  • Declare the class as public
  • Use the keyword class to create an object of that type
  • The first character of the class’s name is uppercased and remaining characters are lowercased.
  • Our class’s body is populated with fields and methods
  • System.out.println() will output to the console whatever message is in quotes.
  • In order for our class to be executable, it must contain a main() method.

main() Method Signature

In order for the JVM to correctly execute the main() method of a Java program it must contain the keywords public, static, and void.

Compiling our Java Source File

  1. Save the Java source file(s)
  2. In order to compile our Java source file you will need to go into the directory where our source files reside
  3. Compile the Java source file(s) by using the javac command followed by the file you wish to compile.
  4. Use the following command: javac MyFirstClass.java
  5. If everything goes well, you will see a new file being created with a *.class extension.
  6. We should now see: MyFirstClass.class
  7. This *.class file will contain bytecode in a format that the JVM will be able to execute

The following example below will show you the commands I ran on a Windows system to create MyFirstClass.class.

C:\workspace_core\MyFirstApp\src>dir
03/05/2015  06:14 PM               143 MyFirstClass.java

C:\workspace_core\MyFirstApp\src>javac MyFirstClass.java

C:\workspace_core\MyFirstApp\src>dir
Directory of C:\workspace_core\MyFirstApp\src

03/05/2015  06:20 PM               453 MyFirstClass.class
03/05/2015  06:14 PM               143 MyFirstClass.java

Executing our Compiled Java Class

Now that our compiler has created and compiled our .class file we are ready to execute that class file and in our environment.

C:\workspace_core\MyFirstApp\src>java MyFirstClass
Welcome to my first java program...
java_first_program

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *