1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Java and Environment Setup

Closer Look at the First Java Program

Start course
Overview
Difficulty
Beginner
Duration
56m
Students
67
Ratings
5/5
starstarstarstarstar
Description

In this course, we learn about Java technology and get you started with writing and running a Java program. 

Learning Objectives

  • What is the Java programming language?
  • Java Development Kit (JDK)
  • Java Runtime Environment (JRE)
  • Java Virtual Machine (JVM)
  • Integrated Development Environment (IDE)
  • Installation of Eclipse
  • Write a Java program
  • compile and run a Java program

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. Now that we have understood how to run a Java program, let's have a closer look at the program we have written. The HelloWorld application consists of two primary components. First, the HelloWorld class definition, and the main method. public class HelloWorld. This is the first line of our Java program. Every Java application must have at least one class definition that consists of the class keyword followed by the class name. And the code for each class appears between the opening and closing curly braces. We will discuss the class subject in detail in the next lecture. But now, it's enough to know that every Java application begins with the class definition. public static void main (String[] args).

This is our next line in the program. The public keyword is the access modifier. The modifiers public and static can be written in either order. public static or static public. But the convention is to use public static as shown like that. The void keyword indicates that this method does not return any value. The main word is the name of this method. The main method is similar to the main function in C and C++. It's the entry point for your application, and will subsequently invoke all other methods required by your program. String args, arguments. The main method accepts a single argument,

an array of elements of type String. This array is the mechanism through which the runtime system passes information to your application. For example, Java, my app arg one or arg two. At this stage, I want to switch to Eclipse and give you a few examples about args. First, I will print the size of the args array on the console screen. Of course, you haven't learned what arrays are yet, but for now I won't get hung up on it. I just want to explain the argument here in a little more detail. We will again use the System.out.println method to print the size of the args array to the console. There is an easy way to call this method in Eclipse. If you type sout and press enter, this method will be created automatically.

But if you notice, no suggestions are offered when you type sout. Normally, code editors provide great convenience to developers with their auto completion feature. Now, we will activate this feature of Eclipse. Actually, if you press the control and space keys on the keyboard right now, it'll offer you some suggestions, but I want to show you a more practical way. To activate the auto-complete feature, I open the preferences section from the window menu. Then, I come to the editor section in the Java section, and here, I select the 'Content Assist' option. From the window on the right, I will write all the letters from A to Z in the auto-activation triggers for Java section with a dot at the beginning.

So, Eclipse will automatically suggest some options to us. I click the 'Apply and Close' button. That's it. Now, let's write sout again. And as you can see, the Eclipse shows us some options. I press the enter key on the keyboard, while the System.out.println method is selected. So, the print method is created automatically. Excellent. Now, in the parentheses inside the double quotes, I write, size of args =. Then, I put the plus sign and now I write args.length. The args.length method will give us the size of the args array. As I said, you will understand all of these better in our following lessons. Now, let's run our code. I right click on the HelloWorld.java file. I choose run as Java application. As you can see, the size of the args array is currently zero because no arguments have been assigned yet.

In Eclipse, we can change this. I click on the 'Run' menu from the top menu and open the 'Run Configurations' option from the dropdown menu. In the window that opens in the Java application section, I select our application, namely, the HelloWorld application. Then, I open the 'Arguments' tab from here. You can assign arguments to the args array from the program argument section here. For example, I'm writing test arguments. Here, the argument elements are separated by a space. So at the moment, two elements will be passed into the argument array. If I leave a space and write a number, for example, five. This time three elements will be passed into the argument array. So, the size of the args array will be three. Now, let's click the 'Apply' button and close this window. Now, let's run our application. I right click on the HelloWorld.java file. I choose run as Java application. As you can see this time, the size of the args array is printed as three.

If we want, we can show these elements on the console screen respectively. But I don't want to confuse you too much at this stage. Now, let's continue to explain the subject on the slide. So, as a result, in the Java programming language, every application must contain a main method whose signature is like that. Also, we'll cover how to define a method in Java in the next lectures. Finally, the line System.out.println("Hello World"); println method prints the contents inside the double quotes into the console and inserts a new line after. It uses the system class from the core library of Java. Don't worry if you don't understand it, we'll learn more about classes and methods in the next lectures. So, see you in the next video

 

About the Author
Students
512
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics