Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Getting Started with Java

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Getting Started with Java

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Getting Started with Java

Elements of a Java Application

I understand that you are eager to type some code in your editor and run it to see your first Java
application in action! Do not worry, your expectation will be fulfilled by the end of this tutorial. But
before we move on, I would like to do through several elements that you need to know to fully
understand what you are doing.

Even if you are familiar with some other programming language, know about compilation, know what an
executable file is you may be interested in the following because Java works in a way that differs from C
or C++.

Compiling and Executing Java Code

There are several steps that you need to follow to create a Java application. This tutorial shows you how
to create a very simple Java application. If you need to create an enterprise application, the creation
process is more complex but at its core you will find these simple steps.

The first of these steps is to write some Java code in a text editor.

Then this code has to be transformed to another format, which can be executed by your computer. This
transformation is conducted by a special piece of software called a compiler. Some languages do not
have a compiler; Java does. Every compiler is specific to a language.

The file produced by a compiler is often called a binary file or an executable file. Whereas you can read a
source code and understand it, binary or executable files are not meant to be read by a human person.
Only your computer can make sense of it.

This code contains special binary codes called byte code. This is a technical term that you may come
across. The precise description of what is this byte code is beyond the scope of this tutorial.

Compiling some code may fail; your code has to be correct for the compiler to produce an executable
version of it. Do not worry, this page gives you the code you are going to compile. All you need to do is
copy it and paste it in your text editor.

Once the compiler produced the binary file that you need, you can execute this binary file, that will your
program.

These two steps: compilation and execution require two specific pieces of software that are part of the
Java Development Kit, also known as the JDK. You will see how to download the JDK for free and how to
install it later in this tutorial.

Note that starting with Java SE 11 you can also merge these two steps into one, by executing a .java file
directly. You can use these feature only if you are executing a program that is written in a single file. This
way of executing your java application does not work if your java code spans more than one file.
Creating a First Java Class

The first step you need to know is that the Java code you are writing is saved in plain text files. In this
tutorial, your application will be written in a single text file. Larger applications may require thousands of
such files.

Java is an object-oriented language. If this technical term does not mean anything to you, do not worry,
all you need to remember at this point is that all the code you write must be held in a Java class.

A Java class is created by a special declaration in a text file. Just copy the following code and paste it in
your text editor. Congratulation! You have created your first Java class!

public class MyFirstClass {

Copy

The name of this Java class is MyFirstClass. You need to save this text in a file named MyFirstClass.java. A
Java class must be saved in a file that has the same name as your class with the extension .java. This is
mandatory and is in fact very convenient because you do not need to open a file to know what class is
written in it.

You can give this class any name as long as it does not start with a number. There is a convention though:
the name of a Java class starts with a capital letter. This is not mandatory but all Java developers follow
this convention. When you become a seasoned Java developer, seeing a class that does not follow this
convention will look weird to you.

If you are following this example to the letter, you should save the MyFirstClass class in a text file
called MyFirstClass.java.

Just a word of warning: you should be using a plain text editor to create and save this file. Using a word
processor will not work.

Preparing the Compilation of your First Class

Compiling is the second step you need to follow after the creation of your first class. It consists of
transforming the Java code you wrote in your MyFirstClass.java file into another format that can be
executed. The result of this transformation will be stored in another file created by the compiler. The
name of this file will be MyFirstClass.class.

So far the only tool you have been using is a plain text editor. Compiling this class requires a compiler;
something you may not have on your computer. Fortunately, you can download this compiler and use it
for free. Let me guide you through this process.
As of now, downloading "Java" means downloading the Java Development Kit, also known as the JDK.
The JDK contains many tools and among them are the ones you will be using to compile and run a Java
application. It is officially distributed by the OpenJDK project and by Oracle.

You may have heard about some other elements, also called "Java".

The JRE stands for Java Runtime Environment. It is a subset of the JDK that is not distributed by the
OpenJDK or Oracle anymore. It only contained the tools needed to run a Java application. You cannot
compile your code with the tools provided in the JRE.

You may also have heard about J2EE, Java EE or Jakarta EE. All these acronyms refer to the Java
Enterprise Edition. It is a set of tools and libraries to create enterprise-class applications. Java EE is
different from the JDK and is outside the scope of this tutorial. You do not need Java EE to compile and
run the simple application we are creating in this tutorial.

cr

You might also like