Java Fundamentals
Tags: java, programming
Published: 2023-11-28 14:36:36

Java, one of the most widely used programming languages, offers a robust foundation for building diverse applications. Whether you’re a newcomer to programming or transitioning from another language, understanding Java fundamentals is essential. In this blog post, we’ll explore the key concepts and features that form the bedrock of Java programming.

The first thing that is important to understand before exploring java, is the process of implementation. Let’s look at it step by step.

1. Write Java Code:
Create your Java source code (.java files) using a text editor or integrated development environment (IDE). Define classes, methods, variables, and other necessary components.

2. Save the file as a .java file.

3. Compile Java Code: Here the interesting part starts. You need to use javac (java compiler) to convert human-readable code to bytecode. What is bytecode? Bytecode is a special machine language native to the JVM.

4. Java Virtual Machine (JVM)
JVM is a runtime environment in which java bytecode can be executed. Tasks of JVM:

- Loading Code 

- Verifying Code 

- Executing Code 
 

Lastly, Debugging:

If issues arise during execution, use debugging tools provided by your IDE or additional logging statements in your code to identify and fix problems.

Besides JVM you probably heard about JRE-Java Runtime Environment and JDK-Java Development Kit. 
Basically, JRE is the implementation of JVM. To run any code in Java JRE is the minimum requirement. JRE contains a set of libraries that JVM uses at runtime.
JDK is a full-featured Software Development Kit. It contains JRE+Development Tools. Development tools are debugger, compiler, and JavaDoc. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjxbz8voh9xxwdp2tb7v.png)

## Java Data Types

There are two data types in java, primitive and non-primitive. Let's start with the first one. A primitive data type specifies the size and type of variable values, and it has no additional methods. 

There are 8 primitive data types in java:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m9fjhmw8glxo8tmlzi7.png)


In Java, non-primitive data types are also known as reference types or objects. These data types are instances of classes and are created dynamically, unlike primitive data types, which are statically allocated. For example, strings, arrays, classes, interfaces, etc, are non-primitive data types. 

The main difference between primitive and non-primitive data types is that non-primitive data types are objects with methods and can be more complex in structure. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbz6l955xbaxj3kkl67k.jpeg)

## OOP

Object-oriented programming (OOP) is a programming paradigm that arranges the design of software around objects, emphasizing data rather than functions and logic. An object is characterized as a data field possessing distinct attributes and behavior. The structural components of object-oriented programming encompass:

- **Classes:** These are user-defined data types functioning as blueprints for individual objects, specifying attributes and methods.
- **Objects:** Instances of a class created with precisely defined data, representing real-world entities or abstract concepts. Initially, the only object defined is the class description.
- **Methods:** Functions enclosed within a class that define the behaviors of an object. Each method in class definitions begins with a reference to an instance object, and the subroutines within an object are referred to as instance methods. Programmers use methods for reusability and to maintain functionality within a single object.
- **Attributes:** Defined in the class template, attributes signify the state of an object, with data stored in the attributes field. Class attributes are associated with the class itself.

Here are some examples of how these components look as a code. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3c826c9jomjbjfahgtq.jpeg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d34g1ruu6vh2cul4vj0.jpeg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/526843mmzqfwqq9dqhqe.jpeg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btka9hhyhu4mmr4e5lb2.jpeg)

Of course, one of the first things that you should know about OOP is the so-called 4 pillars of OOP. So what is it?

## 4 Pillars Of OOP

• **Encapsulation** involves keeping vital information within an object, restricting external access to a defined set of functions. This practice enhances program security by preventing unauthorized changes and unintended data corruption.

• **Abstraction** ensures that objects reveal only essential internal mechanisms to other objects, concealing unnecessary implementation details. This simplifies future modifications or additions for developers.

• **Inheritance** facilitates code reuse among classes, creating relationships and subclasses that enable the utilization of common logic while preserving a distinct hierarchy. This property streamlines data analysis, reduces development time, and enhances accuracy.

• **Polymorphism** allows objects to share behaviors, taking on multiple forms. By determining the relevant meaning or usage during execution, this practice minimizes code duplication. Child classes extend the functionality of parent classes, permitting different object types to pass through a unified interface.

Furthermore, let's get a little bit acquainted with loops and arrays:

**While loop**

The while loop loops through a block of code as long as a specified condition is true.

Syntax:

while(condition){
//code block to be executed
}

**For loop**

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.

Syntax:

for(statement1; statement2; statement3){
//code to be executed
}

**Arrays**

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Example:
String [] cars={"Volvo", "BMW", "Hyundai"}
int [] num={1,2,3,4}

Lastly, I would like to talk about the _accessors_ and _mutators _ for you to fully understand the fundamentals of java. 

**Accessors** 

An Accessor method, often referred to as a getter or get method, is a public method that returns a property of an object. Accessor methods adhere to a naming convention where a word, typically "get," is added at the beginning of the method name. These methods are employed to retrieve the value of a private field, and the returned data type corresponds to the data type of the associated private field.

Syntax:
public int getNumber()
{
    return Number;
}

**Mutators**

A Mutator method, often termed a setter or set method, is responsible for modifying or changing properties, exemplifying the principle of encapsulation. These methods, also referred to as modifiers, are easily identifiable as they begin with the word "set" and are declared as public. Mutator methods, in contrast to Accessor methods, do not have a return type. Instead, they accept a parameter of the same data type as their associated private field, using this parameter to set the value of the private field.

Syntax:
public void set Age(int Age) {
    this.Age = Age;
}