The Programming Newbie's Introduction to Java

Đã đăng vào - Cập Nhật Lần Cuối vào

According to the TIOBE index, Java is the number one programming language among developers in March 2016. The Java platform is used in many domains, ranging from banking software and ATMs to Smart TVs and smartphones. According to Statista, more than 1 billion Android smartphones have been sold in 2014. This is important because all the applications and the Android OS too has been (mostly) written in Java. In this article, I will present the basic elements of the Java programming language, so you get a good understanding of it.


Java is a trademark of Oracle and the installation libraries for Java Standard Edition can be downloaded from Oracle’s website. In order to develop Java applications, you have to download the JDK (Java Development Kit). This includes the JRE (Java Runtime Environment), which is all you'll need if you only want to run Java applications.

Once you complete the installation you can validate it by typing in a terminal (on a Linux and Mac) or a command prompt (Windows):

jupiter:~ greg $ javac -version

javac 1.8.0_66

Tools for Development

Most Java developers use two tools – the two IDEs (integrated development environments) Eclipse and IntelliJ. Eclipse is an old player on the IDE market, it is free for use, and in the last 2-3 years as the Java language has started to advance, the IDE became better and better. The current release, Eclipse Mars 2 is really good, compared to the older, 3.8 version. IntelliJ by JetBrains is much newer, and has a lot of nice features for developers.

We have NetBeans too, but this is not so popular, likely caused by the memory and performance issues that hounded it in the past. It's now much better and has a bigger user base, but still incomparable with the number of Eclipse or IntelliJ users.

Before choosing an IDE, please keep in mind that a normal text editor with syntax highlighting will do for learning the Java language. IDEs have been developed to speed up the development process and to help the developer as much as it can. When you are learning, you should see and experience everything the language has to offer.

Java Console Application

As with most of the programming languages, Java supports console applications. This application is the traditional "hello world".

<pre class="brush:java;toolbar:false;">package main;

public class Main {
         public static void main(String[] args) {
                  System.out.println("Hello World!\n");
                  System.out.println("App Arguments are:");

                  for (String argument : args) {
                           System.out.println("\t" + argument);

Let’s go line by line. First, we have the package declaration, and our Main class is inside the main package. Please take a look at the casing; on a Windows OS it does not matter if you write capital or lower letters, but on Linux or any Unix-based system, this may cause issues, since Unix is a case sensitive operating system.

After the package declaration, we have the class declaration, and our class is called Main (it's common within the Java community to name the program entry class as Main class). Next we have the main method, which has to be public static and usually does not return any value, so it is marked as void too. The main method receives the parameters what the user might pass into the application.

Inside the method, the first line writes "Hello World!" into the console. After that, we enumerate the command line arguments of the application. For example, if I invoke this main method and give the value1 13 someNumber 34.55 arguments to it, then the app will write those out to the console.

Java Packages

With the way C++ uses namespaces for organizing large amounts of code in Java, we have the package concept. Packages represent folder structures – for instance, in the previous example, our Main class was defined in the main package.

As you can see in the screenshot, the package structure is the same as the directory structure: it has the src folder, the main folder, and the Main class.

There are two approaches that can be used when you plan the package structure of your projects. One approach is to package the source code based on features. For example, if your application is used by a car dealer, then you may have packages for cars, clients, employees, repair shop and so on. Inside each package, there would be all the source code which is specific for that feature, so the cars package would contain the data model classes for each car type, the customization list, the order sending logic and the invoicing related code structures.

The second approach is to package the code by application layers. For example, the same car dealer app could be packaged into data models, data access layers, services, and loggers.

Most of the developers agree that the package by feature approach should be used because this the most efficient way to minimize the scope of the classes used for the implementation. It makes the code more modular and decreases the dependencies between modules.

Type System

Java is a statically typed, general-purpose programming language. Java has so-called primitive data types.

byte, short, int, long

The byte, short, int and long types are used for storing numbers; the byte stores numbers on 8 bits, the short stores on 16 bits, int stores them on 32 bits, and long may store up to 64bit-long numbers. Starting from Java 1.8, the int and long types support storing of unsigned values too.

     public static void showNumberTypeDetails() {
                  short myShort = 123;
                  int myInt = Integer.MAX_VALUE - 12344;
                  long myLong = Long.MAX_VALUE - 998345;
                  float myFloat = Float.MAX_VALUE - 654344F;
                  double myDouble = Double.MAX_VALUE - 2342342D;

                  System.out.println("myShort value is: " + myShort);
                  System.out.println("myInt value is: " + myInt);
                  System.out.println("myLong value is: " + myLong);
                  System.out.println("myLong value is: " + myFloat);
                  System.out.println("myLong value is: " + myDouble);


float and double

In Java, you can use two types for storing real numbers: float, which is single precision, and stores the values in 32 bits, while the double is used for double precision numbers, which stores the values in 64 bits.

The output of the code sample above is:

Sample Numbers are:
myShort value is: 123
myInt value is: 2147471303
myLong value is: 9223372036853777462
myLong value is: 3.4028235E38
myLong value is: 1.7976931348623157E308



Java has Boolean type, which can have true and false values.

         public static void showBooleanDetails() {
                  boolean isMyCodeOld = true;
                  System.out.println("\n\nIs my code old? " + (isMyCodeOld ? "Yeah Mate sure it is" : "No no, its brand new!"));

The output of the code is:

Is my code old? Yeah Mate sure it is

Strings and char

For storing text, you have the String type. Strings are immutable in Java, which means that after a string is created, it cannot be changed. If it is extended, then a new string instance is created and the content of it is the compound string value. Strings in Java contain characters encoded in UTF 16.

public void showDetail() {
                  String myName = "John Doe";
                  String myLetter = "\n\nHi Mary! \n\nI was pleased to read your "
                                    + "invitation and I can assure that I will be there."
                                    + "\n\nKind Regards,\n" + myName;
                  char myInitial = myName.charAt(0);
                  System.out.println("MyInitial is: " + myInitial);

Strings can be declared using double quotes and concatenation can be done using the plus (+) sign. Chars are also UTF16 encoded, and chars build up Strings.

Here is the output of the code sample:

Hi Mary!

I was pleased to read your invitation and I can assure that I will be there.

Kind Regards,

John DoeMyInitial is: J

OOP in Java

When Java appeared, it was well known for its capabilities of Object Oriented Programming. Java supports OOP very well, it has inheritance, interfaces method overriding, abstract classes.

Interfaces can be declared using the interface keyword:

public interface TypeDetailShowable {
         void showDetail();

In this interface, we declared one method, showDetail() which does not return anything (void method).

Classes can implement interfaces. This can be done using the implements keyword:

package types.strings;

import main.interfaces.TypeDetailShowable;

public class StringsSample implements TypeDetailShowable {
         public void showDetail() {
                  String myName = "John Doe";

                  String myLetter = "\n\nHi Mary! \n\nI was pleased to read your "
                                    + "invitation and I can assure that I will be there."
                                    + "\n\nKind Regards,\n" + myName;

                  char myInitial = myName.charAt(0);
                  System.out.println("MyInitial is: " + myInitial);
In the code above we have the StringSample class declared, which implements the TypeDetailShowable interfaces. This means that all the methods defined in the interface have to be implemented in the class that implements the interface. Since our interface only had only one method (showDetail), we fulfilled this requirement.

Abstract classes can be created using the abstract class keywords:

package main.baseTypes;

import java.util.UUID;

public abstract class BaseEntity {
         private String id = UUID.randomUUID().toString();
         public abstract int getNumber();

         public String getId() {
                  return id;

         public void setId(String id) {
         = id;

This abstract class is one abstract method called getNumber().

package main.entities;

import main.baseTypes.BaseEntity;

public class Person extends BaseEntity {
         public int getNumber() {
                  Double randomNr = Math.floor(Math.random() * 100);
                  return randomNr.intValue();

The Person class extends the BaseEntity abstract class and implements the getNumber() method. This method creates a new random number between 0 and 100 and returns it.

                 Person p = new Person();
                  System.out.println("My ID is: " + p.getId());
                  System.out.println("My Number is: " + p.getNumber());

The output of the code goes something like this (on your machine it may change with each execution):

My ID is: 661879c3-5607-4b8e-a790-275bc10ec402
My Number is: 41

In this article we covered the basics of Java programming, we covered types in java we saw what are the value limits for each data type, we saw how real numbers should be used for calculations. The we covered OOP programming, abstract clases and how interfaces can be used plus we learned how to override methods.

Đã đăng 16 tháng 3, 2016

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Bài báo kế tiếp

5 outils gratuits pour rester productif