DEV Community

Armen
Armen

Posted on

Small journey into the world of Java

Introduction

Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once and run anywhere, meaning that compiled Java code can run on all platforms that support Java without recompiling.

Fundamentals of Java

What do I mean by saying compiled Java code? Imagine you have a friend who speaks English, and you speak French. You want to give them a message. You write the message in French, and your friend translates it into English so he can understand. When you write a Java program, it is not yet something that the computer can run directly. It needs a translator, and that’s where the JVM (Java Virtual Machine) comes in. The JVM is like the friend who translates your Java code into something the computer can understand. This process is called compilation. It’s like taking your message in French and turning it into English, so your friend can understand. In Java, we use a special program called a “compiler” to do this. The compiler takes the human-readable Java code and turns it into Bytecode. Now, bytecode is like a universal language that the JVM understands. It’s not specific to any computer or operating system. It’s like turning your English message into a language that people can understand worldwide. Once the bytecode is ready, the JVM takes this bytecode and translates it into instructions that the computer can execute. After this, if a proper JVM is installed on the computer, any Java bytecode can be run.

Java compilation

Types in Java

In Java, several types can be used to define different kinds of data. Here are some of the main types in Java:

Primitive Types:

  • int: Represents integers

  • long: Represents long numbers (has a very big range)

  • float: Represents about 6 decimal digits

  • double: Represents about 15 decimal digits

  • byte: Represents a very small number (from -128 to 127)

  • short: Represents a small number (from -32,768 to 32767)

  • char: Represents single character

  • boolean: Represents true or false values

Reference Types or Non-primitive Types:

  • String: Represents a sequence of characters (text)

  • Array: Represents a collection of indexed elements of the same type

  • Class: Represents a blueprint for creating objects

  • Interface: Represents a collection of abstract methods

Custom Types (User-defined):

  • You can create your own types using classes and interfaces

Types in Java

Differences between primitive and non-primitive types

The main differences between primitive and non-primitive data types are:

  • Primitive types are predefined in Java. Non-primitive types are created by the programmer and are not defined by Java (except for String).

  • Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.

  • A primitive type always has a value, while non-primitive types can be null.

  • A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.

Difference of data types

OOP (Object-Oriented Programming)

As the name suggests, Object-Oriented Programming or OOP refers to languages that use objects in programming, they use objects as a primary source to implement what is to happen in the code. Object means a real-world entity such as a pen, car, or computer. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. OOP simplifies software development and maintenance by providing some concepts:

  • Encapsulation

  • Abstraction

  • Inheritance

  • Polymorphism

These concepts are called “4 pillars of OOP”. Now, let's understand what these concepts are and how they work.
Firstly, encapsulation in Java is a process of wrapping code and data into a single unit; for example, in the real world, a car is an object, right? When we park the car in the garage and close it, that’s encapsulation. In Java, we can create a fully encapsulated class by making all the data members of the class private.
Secondly, abstraction in Java hides the implementation details and shows only functionality to the user. In the real world, think of a DVD player as an object. This DVD player has a complex logic board inside and only a few buttons outside that we interact with. We press the play button and don’t even care what happens inside. All that complexity is hidden from us. This is an abstraction.
Thirdly, inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class.
Lastly, polymorphism in Java is a concept by which we can perform a single action differently. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many, and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in Java by method overloading and method overriding.

Top comments (0)