Tech With Tim Logo
Go back

Introduction

A Bit About Java

Java is a very popular cross compatible object orientated programming language that has been around since 1996. It is most famous for being used to create games like Minecraft and RuneScape. The main advantages of Java is that it is platform independent and can be easily transferred to almost all the different platforms. This makes developing cross compatible applications much faster and easier as you only need to code one version.

Java is said to be one of the hardest programming languages to learn as it has very complicated and formal syntax compared to other languages like python. However, once mastering Java learning other languages is quite simple and can be done in minimal time.

Running a Java Program

Unlike many languages a Java program must be compiled before it can be run. This is what makes it cross-compatible. The machine translates your high-level Java code into a lower level form that can be understood by the Java interpreter. However, constantly recompiling our Java program each time we run it is inconvenient and can be time-consuming if our program contains many files and packages. This is why we are going to download and install an IDE.

Installing an IDE

An IDE is an Integrated Development Environment. It contains many helpful tools and features that make our lives as programmers much easier. One of the main advantages of an IDE is the ability to run your code from within the environment.

We will be using the Eclipse IDE, download it here. There are many versions of Eclipse but the version we are interested in is the following. eclipse-ide-download-768x126.png

Setting Up Eclipse

The first time we run Eclipse it is going to ask us to create a project directory. You can simply choose any reasonable place on your machine for this.

Note: If eclipse asks you to choose a version select the same as above, Eclipse IDE for Java Developers.

Creating a Project

Before we can start writing any code we need to create a new project. To do this simply navigate to File > New > Java Project kuva_2023-05-01_183608526.png After this we will name our project and select finish. kuva_2023-05-01_183630406.png Now we need to create a new package inside our Java project. A package is essentially a grouping of classes and different Java code. We will create a new package by right clicking on our Java Project and selecting New > Package. kuva_2023-05-01_183654080.png

You can leave the name the same as your project name. kuva_2023-05-01_183708251.png Next we are going to create a class file. This is where we will be writing most of our code. To create a class file right click on your package, click New > Class. kuva_2023-05-01_183737520.png It is important that you check the box stating "public static void main(String[] args)". You can name your class anything you want as long as it starts with a capital letter. kuva_2023-05-01_183754146.png And now we we should have a class file with the following content in it.

package tutorial;

public class Main {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
    }

}

Creating Your First Program

In Java we write all of our code in whats known as a class (what you just created). Inside the class there are things called methods. Methods are where most of our code goes and for now we are going to be writing all of our code from within the method called "main". All the code inside this method will automatically be executed when run the program and for now that is what we want. To make things more clear, inside the second set of "{}" is where all of our code will go.

The first thing that we are going to do it print some output to the console. We can type System.out.println("Text") to print the string "Text" to the console. Whenever we want to print a string of text we must enclose it with double quotation marks("").

package tutorial;

public class Main {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello World");
    
    }

}

The Semi-Colon

";" is a semi-colon.

This is a very important character in Java and it must come after almost every line of code we write. If the line does not start with a "{" or "}" we must have a semi-colon after our code statement, to tell Java to move to the next line. Essentially when you write a semi-colon you are terminating that line of code and telling Java to move on in the program.

See above where after "System.out.println("Text")" I place a semi-colon.

Running Our Program

To run our program is fairly simple. We can click on the green play button in the top menu bar to run our code. If eclipse does not detect any errors it will run the program and you can see the output in the console near the bottom of the screen. kuva_2023-05-01_183807555.png

Design & Development by Ibezio Logo