First Steps in Java — Part 1

mike dietz
8 min readMay 6, 2020

--

Setup

1: Setup | 2: Variables, Data Types and Sizes | 3: Get Some Input | 4: Operators | 5: Strings | 6: Conditionals | 7: Loops | 8: Methods | 9: Arrays

Photo by Adi Goldstein on Unsplash

Intro

Welcome to this starter course First Steps in Java. I decided to dip into Java — according to different sources, the most popular programming language since it’s beginnings in the 1990s — and I am excited to share my first experiences with you.

Before going through the requirements, I would like to quickly sum up a few historical facts about Java.
The creation of the programming language Java is attributed to James Gosling, who worked in 1995, at the time of invention at Sun Microsystem, which was later acquired by Oracle. Java was inspired by the programming languages C and C++ and served as a role model for C#, PHP, Phyton, and more.

To list Java’s main characteristics: in a few words, Java is:

  • class-based
  • object-oriented
  • strong-typed
  • platform-independent

We’ll look in more detail at the meaning and implications of the first three characteristics later during this course. Let’s just look briefly at Java’s platform-independency.
Java’s success is based on its cross-platform architecture. Instead of having to write for one application different sets of code for the different operating systems (Linux, Mac, and Windows), as necessary with other programming languages, with Java, your code runs on all systems, fulfilling its slogan write once run anywhere (WORA).
How does that work? First, The Java code that the developer writes, is compiled into byte-code, which is analogous to machine code. Compiling means converting the entire program that the developer wrote into machine code. This is done by executing the command javac, which generates a .class file out of a .java file. Then, the byte-code in the .class file is interpreted on the host computer into machine language by a virtual machine (JVM), written specifically for the host hardware. The virtual machine translates the byte-code into the platform’s machine language.
This setup makes it possible to use the same code for all three operating systems. Only the virtual machine needs to be written independently for each computer system.

Developer Code (.java file) → Byte Code (.class file) → Machine Code

The Java Virtual Machine (JVM) is part of the Java Runtime Environment (JRE). End-users have to install the JRE on their machine for java applications or java applets (in a web browser) to run.

For Java development, you need the Java Developer Kit (JDK), which includes:

  • the Java compiler (re-writes the Java code of the .java file into byte-code to a .class file),
  • JAR* (Java Archive, a packaging software that combines all files into one distribution file, .jar),
  • Javadoc (a program for creating HTML-styled documentation),
  • jdb* (a command-line debugger tool).

The Java Developer Kit includes the Java Runtime Environment and is available in different versions:

  • Java SE (Standard Edition)
  • Java EE (Enterprise Edition) with additional libraries that enable large-scale distribution for commercial use
  • Java ME (Micro Edition) a subset of SE for mobile devices

For our purpose, we install the Standard Edition, SE.

Requirements

Java Developer Kit

To get started with Java, let’s go to java.com and download the current Standard Edition (SE version) for your operating system. For my Windows 10 system, at the time of writing, I downloaded
Java SE Development Kit 14 64-bit.
To find out if your computer uses a 32-bit or 64-bit system type, hit the Windows key and enter System.

Oracle provides a clearly written document that contains useful information about the installation, check here.

On my system, the software is installed on C:/Program Files/Java/jdk-14. This information is relevant for setting the PATH Environmental Variable.

Command Line Tool

For this course, we will work with a command-line tool (cml). In Windows 10, open the search field by hitting the Windows key and enter Command Prompt .

To test if we installed the JDK successfully, navigate to the installation folder
C:/Program Files/Java/jdk-14/bin

and enter the command
> java -version

Configure PATH Environmental Variable
To not always having to specify the full path to the installation folder, you can quickly set the PATH Environmental Variable.
For Windows, hit the Windows key, search for environment variables, and open the interface. Create a new user variable, give it the variable name JAVA_HOME and set the variable value to C:/Program Files/Java/jdk-14. Now, you can run java directly from any folder location.

Let’s test it and check the current java compiler version from any directory you are in.

check javac version:
> javac -version

Text Editor

It’s time to create our first java application. A simple text editor, e.g. Notepad, is all you need for this basic course. In Windows 10, you can use the pre-installed text editor Notepad. You will find it by hitting the Windows key and searching for Notepad.
Once you decide to continue with Java programming, you would want to install a Java IDE, e.g. NetBeans, IntelliJ IDEA, Eclipse or Visual Studio Code.

A tip for Notepad: if the files get a little bit longer, here’s a useful shortcut to find the current code line number: in Notepad go to Edit/GoTo (Ctrl + G), it indicates the current line number of the cursor and allows you to search for a specific line number. This is useful for the error messages, especially if your file is a little bit longer.

Hello World!

This is the code of our first application. All parts are explained in the rest of this unit.
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}

Open your text editor and save the file as HelloWorld.java in a directory of your choice. A Java file is recognized by the file ending .java.

File Format

All code files that we create will have the .java ending. The compiler re-writes our code and saves the resulting byte-code in a .class file, which will be translated by the virtual machine (VM) into the computer’s machine language.

Java is case-sensitive,

i.e. ‘helloworld’ differs from ‘helloWorld’ or ‘HelloWorld’. This does not only apply to the file name but also to classes, methods, variables, and data types.
For file and class names, stick to the Pascal naming convention, i.e. start all words in a composed name with a capital letter, as in HelloWorld.

Every Java application needs to have at least one class and the main() method.

Java is a class-based programming language
Every Java application needs to have at least one class. A class is a blueprint, used for creating objects. Every time we call a class, we create an object, a so-called instance of this class.
A class consists of two parts: the class declaration and the class body. Our minimal class declaration contains only the keyword class.
class HelloWorld { }

Class Declaration

We declare a class through the keyword class (one of the many reserved keywords that cannot be used for naming purposes).

Note that the class name has to be identical to the file name.

The class declaration can also include modifiers of different kinds, which we will not worry about on this basic level but we mention them for you to get familiar with. These are the possible modifiers:

  • Access Modifier: public, protected and private
  • Modifier requiring override: abstract
  • Modifier restricting to one instance: static
  • Modifier prohibiting value modification: final
  • Modifier forcing strict floating point behavior: strictfp
  • Annotations (which have a similar purpose as Decorators)

Class Body

The class body is defined by the curly braces {}. It holds methods (functions) and variables. Everything within a class, except for the constructor, is called a class member.

main() Method
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
The main() method is the starting point in a Java application. Launching a Java application starts a Java runtime environment, which loads the specified class and calls the main() method in that class.

Method Declaration
A method must contain a return-type (data type), a name, and a pair of parentheses for the arguments. Within the declaration, the method signature is defined, which is composed of the method name and the argument list (for the reason of method overloading, a topic that will not be covered in this course). The components that a method declaration can have, are:

public static void main(String[] args) { }

public
Access modifier; public defines that the method is accessible from other classes within the entire application, which can contain many different classes. Not of concern for us since we have only one class in this example.

static
A static method is available even if no instance of the class has been created. This method belongs to the class and not the object that is instantiated from the class.

void
Return type; void means that the method does not return a value.

main
Method name; when the application is started, Java searches for the main() method.

String[ ]
Data-type of the arguments; the arguments can be an array of strings.

args
Arguments of the method

Method Body {}
The method body is enclosed in curly braces. It contains statements with expressions.
A statement is a block of code that is a command. An expression is a combination of variables and operators (can be of type assignment, mathematical or logical).
There are three different types of statements:

  • expression statement, changes values, call methods and creates objects
  • declaration statement, declares variables
  • control-flow statements are conditional statements

All Statements have to be terminated with a semicolon.

Our statement calls the print method with the provided argument
Hello World!”.
System.out.print(“Hello World!”);

Let me introduce you to SOP, one of the most often used Java statements, system.out.print. It prints the provided argument in parenthesis, in our case “Hello World!“.

Methods are part of a class, as we have learned already. To call a method from outside the class where it resides, you need to call the class.
System: is a class of the java.long package.
out: is a static member field of the System class of type PrintStream.
print
: is a method of the PrintStream class.

This is probably not very clear yet. What we want to remember, at this point, is that there are many libraries available (i.e. collections of classes) that can be imported into a class to make the methods of these imported classes available. Print is a method of the class PrintStream, which serves as a type for the field out that belongs to the class System.

Constructor

A constructor is a special method (function) that does not return anything (void). It is called when the object is initiated. The constructor can take parameters to set the initial values for your object attributes. If you do not create a constructor, Java will set an empty constructor by default. The constructor must have the same name as the class.

Compilation

Now that we have completed our class. We need to compile it with the command javac.
Our program now gets compiled into a byte-code file with the same name but a different file ending, i.e. .class.
> javac HelloWorld.java

When we call the command java, this new file gets executed in the Java Virtual Machine at runtime. The just-in-time compiler (JIT) converts the byte-code into machine code.

screenshot HelloWorld.java

We created our first Java application and run it!

Get the code for this lesson at GitHub.

Tomorrow’s topic is variables, data types, and data size.

--

--