> For the complete documentation index, see [llms.txt](https://blog.indrajitvijayakumar.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.indrajitvijayakumar.in/java/introduction-to-java.md).

# Introduction to Java

Java. An object-oriented programming language so popular in the tech industry that it is the **second most popular programming language** in the whole world, only after C. Also, around **95% of enterprises use Java for building their applications.** So, no wonder that Java is a pretty useful programming language to learn and use, as it has been battle-tested and emerged as a stable and versatile programming language for building all kinds of different software.

### Birth of Java

Java was developed by **James Gosling at Sun Microsystems** and the first version was released on **23rd January 1996.** Initially Java was very slow, but subsequent releases made it much faster. Today, *Java is considered to be a very fast language - as fast as C and Rust, and much faster than other programming languages.*

> <mark style="background-color:blue;">Note:</mark> Though Java is fast, it **consumes a lot of memory.**

## But how does Java work?

Before learning how Java works under the hood, we need to know about **two important components** of Java:

* <mark style="background-color:purple;">Java</mark> <mark style="background-color:purple;">**compiler**</mark> - The compiler will check your java source code for errors and if none, will convert your source code to **bytecode.** One important thing to note is that the **generated bytecode is platform independent.**
* <mark style="background-color:purple;">JVM</mark> <mark style="background-color:purple;">**(Java Virtual Machine)**</mark> - The JVM is used to convert bytecode generated by compiler to machine code that the underlying operating system can understand and execute. JVM is **platform dependent** which means we have different JVMs for Windows, Linux etc.
  * *But why do we need different JVMs for different platforms?*

    Since the JVM must translate the bytecode into machine code, and the machine code depends on the operating system being used, it is clear that the JVM needs to be platform (operating system) dependent.

With these two components in mind, let's see how Java works.

<figure><img src="/files/xxKxtgCJXppiTyKSwygD" alt=""><figcaption><p>The way Java works</p></figcaption></figure>

I hope the above diagram gave you a brief understanding of how Java works. In short:

1. You write the source code in Java.
2. Next, you try compiling the source code using a Java compiler. If there are any errors, then it will throw an error. Else it will compile the source code to **Java bytecode.** The generated bytecode is platform independent.
3. Now, when you want to execute your source code in some other device, the JVM takes the bytecode generated in the previous step, converts it into **machine code** that the device can understand, and then executes and provides the output.

## Code structure in Java

In Java, **everything goes in a class.** Every Java application has to have at least one **class,** and at least one **main** method (just one main per application and not one main per class).

* A source code file (file with .java extension) typically contains a single class definition. A class represents a *piece* of your application and acts as a **blueprint for an object.**
* A class can contain **instance variables and methods.**
* **Instance variables represent the state** of the class.
* **Methods represent the behaviour** of the class. Methods contain a set of statements to perform some business logic.

```java
// Here is an example of a Dog class
public class Dog {
    // These are the instance variables
    String breed;
    int age;
    
    // This is the constructor method
    public Dog(String breedOfDog, int ageOfDog) {
        breed = breedOfDog;
        age = ageOfDog;
    }
    
    // These are the methods
    public void bark() {
        System.out.println("Woof woof!");
    }
    
    public void printBreedAndAge() {
        System.out.println("Breed of dog: " + breed);
        System.out.println("Age of dog: " + age);
    }
}
```

***

I hope you got a good introduction to Java; it's working and code structure. In the next post, we will delve more into the object-oriented features of Java. Until then, cheers! 👋
