DEV Community

Sona Grigoryan
Sona Grigoryan

Posted on

Introduction to Java (Java fundamentals, Data Types, Intro to OOP)

Hello everyone, today I am going to introduce you to the Java programming language. The topics that I am going to discuss today include:

  1. Java Fundamentals a. Compiled vs Interpreted languages b. Java as a compiled language c. Java Virtual Machine d. Java Development Kit
  2. Data Types in Java a. Differences between Primitive and Non-Primitive Types b. Primitive types c. Non-Primitive Types
  3. Introduction to Object Oriented Programming a. OOP b. 4 pillars of OOP 1) Inheritance 2) Encapsulation 3) Abstraction 4) Polymorphism

Let's start right away!

  1. Java Fundamentals

a) Compiled vs Interpreted languages.

Programming languages can be compiled or interpreted.
In compiled languages the whole program is translated to machine code and than executed by the processor while in Interpreted languages the program is executed directly without being compiled first. Interpreted programs run slower than the compiled program. Additionally, since the programs in compiled languages are translated first into machine code, which is specific to the architecture of the computer, the compiled programs can only be run on the computers they were compiled on.
Some exampled of compiled and interpreted languages.
Compiled languages : C++, Java, etc.
Interpreted languages : JavaScript, Python, etc.

b) Java as a compiled language

Java is a compiled language, but since it is platform independent (I will talk about this in a little but) the compilation is executed in two stages instead of one, where first the code is translated to Java bytecode and later run and executed through JVM (again will be discussed later in the post).

c) Java Virtual Machine

At first Java was designed to be run on a specific OS, but that proved to be a problem. Later JVM (Java Virtual Machine) was created. With the help of JVM the Java code can be executed on any platform if there is JVM installed. To conclude let's say that JVM has two primary functions: 1. to allow Java programs to run on any device or operating system (known as the "write once, run anywhere" principle) and 2. to manage and optimize program memory.

d) Java Development Kit

IF you want to develop and run a software application in Java, you will need to install JDK (Java Development Kit). This is the overall composition of JDK:

Image description

JDK contains some Developments Tools to help you and JRE (Java Runtime Environment) to execute the code. JRE in turn contains the JVM and some classes libraries to help with the development. If you have a simple program and not a software application it is enough to download the JRE without downloading the whole JDK.

  1. Data Types in Java

a) Primitive vs Non-Primitive Types

I will list the characteristics of first Primitive types and than Non-primitive types. The points are structured as contrasting, and so highlight the core differences.

Primitive Types

. Primitive types store simple values directly.
. They are predefined by the language and not composed of other types.
. They have a fixed size in memory.
. A primitive type has always a value.
. Primitive types cannot be used to call methods to perform certain operations
. A primitive type starts with a lowercase letter.

Non-primitive (reference) Types

. Reference types are more complex and are composed of primitive or other reference types.
. they can dynamically grow or shrink in size (e.g., with ArrayList)
. non-primitive types can be null.
. Non-Primitive types can be used to call methods to perform certain operations
. non-primitive types start with an uppercase letter.

b) Primitive types

8-bit signed integer (-128, 127)
16-bit signed integer (-32768, 32767)
32-bit signed integer (-2^31, 2^31-1)
64-bit signed integer (-2^63, 2^63-1)

Example: 745963

All the types above are used to represent integers, however the amount of memory they take is different, hence the value range is different for each of them.

32-bit floating-point.
64-bit floating-point.

Example: 1.00713

The types above are used to represent floating point numbers and depending on the type reserve different amount of memory.

16-bit Unicode character.

Example: ‘s’ ‘@’

The char type is used to represent the Unicode characters (from letters to symbols) and take up 16 bits in the memory.

unsigned 8 bits representing true or false values

Example: true, false

The boolean is used to in programing to represent true and false values. True and false are the only values that boolean can take.

c) Non-primitive types

For Non-primitive types I will only discuss Strings, Arrays and Classes, but there are many other types for example Interface, ArrayList, etc.


Strings is a collection of char-s (characters).

“Hello, world!”

Array is an ordered, enumerated collection of elements. However in Java, Arrays only can contain one type of elements, that is can be all Strings, ints, Arrays, etc. In programming the elements of an Array get unique numbers (indexes) and the count starts from 0 instead of 1.

[“school”, “search”, “sun”]
[1130, 613, 528, 304]

0 1 2 3
arr = [1130, 613, 528, 304]
arr[2] = 528


A class is an abstract blueprint that creates more specific, concrete objects. Classes often represent broad categories, like Dog or Human or Chair, that share attributes. These classes define what attributes an instance of this type will have, like color. Additionally classes can also contain methods that perform some action and are specific only to that object.


Class Marker {
    String color = “blue”;
    write (text){}

Marker MyMark = new Marker();
Enter fullscreen mode Exit fullscreen mode

In this example we first defined the class of Marker and than created a new instance of these class (object).

  1. Introduction to OOP

a) OOP

Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (called classes), which are used to create individual instances of objects.

Basically, in OOP languages the complex code is divided into simple, reusable parts (classes) to help structure the program.

b) 4 pillars of OOP

These are some concepts that is important to understand when we talk about OOP.

1) Inheritance

Inheritance allows a class (subclass) to inherit the properties and behaviors of another class (superclass).

Let's look at the example to understand better:

Class Dog {
    int numOfLegs = 4;
    String color = “black”;
    bark (){}

Class Hasky extends Dog{
    String color = “white”;

Enter fullscreen mode Exit fullscreen mode

When we want to create a new class that should share the same attributes and methods of some other class to avoid repeating the same code twice we can extend the new class to an already existing parent class and so, inherit all it's properties and methods. We can overwrite those attributes if they are different for the child class, or we can add some new attributes and methods in addition to the inherited ones.

2) Encapsulation

Encapsulation is the bundling of data and methods that operate on the data into a single unit known as a class.

Encapsulating means that we are isolating some piece of code in a class and this allows us to first make the code simpler and second to make our code more secure. In classes we can specify if we want the attribute or method to be public (accessibly outside of the class) or private (only accessibly inside the class).

Here is an example to better understand encapsulation.

Class Car {
    public String color = “white”;
    public int milesDriven = 10000;
    public stir(){}
    private engineWork() {}
Enter fullscreen mode Exit fullscreen mode

3) Abstraction

Abstraction involves simplifying complex systems by modeling classes based on the essential properties and behaviors they share. It focuses on what an object does rather than how it achieves its functionality.

Basically to make the User Interface as simple as possible some pieces of code that we are not interested in can be abstracted away and so kind of hided. It is still accessible, but to make everything easier it is hided behind a more simple interface.

Here is and example:

Class ParkingLot {
      checkin() {}
Enter fullscreen mode Exit fullscreen mode

Here in these example calvulateFee() and cleanSpot() is abstracted away since we do not need to necessarily engage with each one of them individually.

4) Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables a single interface to represent different types or forms.

Let's look at the example below:

Class Marker {
    String color = “green”;
Enter fullscreen mode Exit fullscreen mode

Objects like triangle, circle, square can all be passed to these function. It is possible, even though they belong to different classes, because they all share some common properties and belong to a common base class.

This is all, thank you all for reading, I hope it was helpful to you!

Top comments (0)