Navigate / search

Get started with Java. “Hello World”

What is Java?

Java is a general purpose programming language was first developed and released by Sun Microsystems (1995). At the time of this post the current owner of Java is Oracle Corporation. Java is available for almost all platforms.

 

Why Java?

I am not going to mention the tons of template reasons, such as platform independence and performance improvements and so on and so forth.

It is much more complicated why java is a good choice to learn and master. It is indeed beneficial that many resources you can find for java as well as the open source implementation (OpenJDK). However, what in my opinion describes java as a brilliant programming language is how Java managed to evolve in the last decades without breaking existing enterprise solutions. In case of many other programming languages I found myself in the situation that something in the new version was completely redesigned and now it is not even similar to its predecessor. Java managed to find a good balance that is beneficial for developers that whatever we have learnt we do not need to throw it into a trash bin elegantly, but can reuse all the knowledge. It is indeed a big responsibility to manage the right balance between improving and supporting adequately the existing solutions in future releases.

The community behind Java is awesome and it is extremely easy to find support. Therefore, the whole ecosystem works perfectly. Java is a language that is not going to disappear from the market soon as we need a general purpose language like this.

There are thousands of other reasons could fill up entire hard disks.

 

Install Java

In order to get started with Java you need to install JDK (Java Development Kit).
You can find installation instructions here: https://www.java.com

 

IDE

Integrated development environment is a type software that provides a large variety of facilities for software development. You do not necessarily need to use an IDE to develop software, however, it provides you convenient features to increase your productivity with software development.

I recommend to use IntelliJ Idea. It has community edition that you can use for free of charge.

 

Hello World

1
2
3
4
5
6
public class MyFirstMainClass {
 
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Above code prints “Hello World!” onto the console. The text printed out is a string literal that needs to be enclosed between double quotation marks in Java.

Save your file as MyFirstMainClass.java

This step can be done with your IDE by creating the class in which case your IDE manages the compilation. If you do not use IDE you can just type the code into a simple text editor and save the file as MyFirstMainClass.java. Please note that the extension needs to be “.java”.

 

(In the command line you need to be navigated to the directory in which the file was saved.)

In order to compile the code you need to use the following command from command line:

javac MyFirstMainClass.java

Above command compiles the file after which you will find a file called MyFirstMainClass.class file in the same directory as the .java file is. The file is a compiled bytecode JRE(Java Runtime Environment) can interpret and execute.

 

To execute the program use the following command:

java MyFirstMainClass

 

Compilation error

If you did not type something correctly you might have received a compilation error. The compiler issues compilation errors when the program does not meet the language rules that is called syntax error.

Example: missing semicolon “;”.

 

Comments in code

Java supports single and multiline comments as well as documentation comments.

Single line comment:
1
// my comment

 

Multiline comments
1
2
3
4
/*
* My
* Comment
*/

In case of above comment the java compiler will ignore the comment from /* to */

 

Documentation comments conform to the following pattern:

1
/** documentation */

 

IDE can generate documentation comments that is the simplest way to keep documentation comments updated.

At the very beginning documentation comment should be ignored. Practice to use single line comments and multiline comments.

 

Command line vs IDE

In today’s world you are not expected to do compilation by using command line and type manually but using an IDE which does all related tasks and provides many other useful smart features to help you. However, I do recommend using command line at the beginning to understand what is happening behind the scenes. The understanding you gain by using command line can help you in the future in situations when you do need to use it on a server or at least understand it in order to create configuration, in which case being familiar with the command line is an enormous advantage.

 

Variables and constants

Variables are used to store values that can change during the program execution. A constant is similar to a variable, however, its value cannot change once it was set.

Variables point to memory locations which are used to store values.

A variable has a name and a data type. Example data types: Integers, String literals, floating point numbers.

 

Variable declaration

Example:

1
int a;

The variable name is “a” and its type is Integer. Please note that the declaration conforms to the following pattern:

<dataType> <variableName>;

 

Variable assignment

We can assign values to variables. When we do so we instantiate the variable.

Example variable assignment:

1
a = 12;

Setting the value of a variable to 12. Please note that the declaration conforms to the following pattern:

<variableName> = <value>;

When we assign a value for a variable first, it is often called as variable initialization.

 

Example variable declaration and assignment on one go:

1
int a = 12;

Declaring and setting the value of the variable. Pattern:

<dataType> <variableName> = <value>;

 

Value can be another variable.

Example:

1
2
int b = 15;
int a = b;

 

Example program:

1
2
3
4
5
6
7
8
9
10
11
12
public class MyFirstMainClass {
 
    public static void main(String[] args) {
 
        System.out.println("Hello World !");
 
        int b = 15;
        int a = b;
 
        System.out.println(a);
    }
}

 

Variable and constant naming conventions

What is technically available and can compile does not necessarily mean it is good. It is a must to follow conventions and good practices. Meaningful names are important and challenging as well.

 

Variable names should start with a lowercase letter. Use a full word rather than an abbreviation.

If the name of a variable consists of more than one word, we know by now the first letter is always lowercase, however, all the subsequent words should start with capital letters.

Example:

1
2
3
4
int netPrice = 1200;
String firstName = “John”;
String lastName = “Doe”;
int personalAccountNumber = 43554;

In case of constants all the letters should be capital and words are separated by underscores.

Example:

1
2
static final int FIX_SUPPORT_FEE = 3;
static final String HOME_DIRECTORY =/home/myname/;

At this point you do not need to worry about constants too much. Focus on understanding variables and the way they work.

Leave a comment