By Atharva Joshi

📆 Posted on 2021-5-27

Beginner’s Guide to Java ♨️ Part 1 of 4

These blogs will help you learn Java Programming & Concepts in a simple and effective way. If you have no prior knowledge in Java, you won’t face any difficulty. If you are experienced java developer, this blog will help you brush up the concepts.

JVM, JRE, JDK

This is how the magic happens, you write your logic aka code in a java file, its converted into class file so that the machine can read your logic and run it.

Briefly these points covers it all:

Memory allocation 🧠

So in the backgroud how the memory allcation works from your code. Brief pointers:



memory-allocation

Memory Allocation Representation


The below table gives an idea of various datatypes and range of values it can hold.

Data-TypeSizeRange
Byte8-bit signed-128 to 127
Short16-bit signed-32,768 to 32,767
int32-bit signed-2^31 to 2^31 -1
long64-bit signed-2^63 to 2^63-1
char16-bit unicode0 to 2^15-1
double64-bit IEEE 754 floating point-2^1074 to 2^1.74
float32-bit IEEE 754 floating point

OOPS — Encapsulation, Inheritance, Polymorphism, and Abstraction

Object Oriented Programming(OOP) is a programming concept that works on the 4 principles.

  1. Encapsulation

    Encapsulation is wrapping data(variables) and functionality(methods) together as a single unit. Functionalities mean “methods” and data means “variables”. Its all wrapped in is “class.” It is a blueprint or a set of instruction.

Class: A class is a blueprint or prototype that defines the variables and the methods. For example:

Class: Car

Data members or objects: Color, Type, Model, etc.

Methods: Stop, Accelerate, Cruise.


Object: Now, an object is a specimen of a class. Like in the above example my car is an object of the class Car.

Variable: can be local, instance and static. Local variables are declared inside the body of a method. Instance variables are declared outside method. They are object specific. Static variables are initialized only once and at the start of program execution. Static variables are initialized first, we will discuss static in detail later.

Method: methods are various functionalities, its nothing but set of code which is referred to by name and can be called (invoked) at any point in a program. You can pass multiple values to a method and it returns value(s).

Package: A Package is a collection of related classes. It helps organize classes into a folder structure and make it easy to locate and reuse them.

package com.example;
class Car {
    String color = "black"; //instance variable
    void accelerate() {
        int speed = 90; //local variable
    }
}
  1. Abstraction

    Abstraction is selecting data from a larger pool to show only the relevant details to the object. Here is a chart showing different access modifiers and how it restricts the data from a class.

modifiers
  1. Inheritance

    Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents.

class Developer{
  public void writeCode(){
  // writeCode method

}
class BackendDeveloper extends Developer{
  public void writeCode(){
  // writeCode method
  }
}
Class run{
  public static void main (String args[]){
    Developer developerObject = new Developer()
	// writeCode method in class Developer will be executed
    developerObject.writeCode();

    BackendDeveloper backendDeveloperObj = new BackendDeveloper();
    // writeCodemethod in class BackendDeveloper will be executed
    backendDeveloperObj.writeCode();
  }
}
  1. Polymorphism

    Polymorphism is a OOPs concept where one name can have many forms also knows as overloading. Dynamic Polymorphism is the mechanism by which multiple methods can be defined with same name and signature in the superclass and subclass also known as overriding.

    • Overloading is multiple methods in the same class with same name but different method signature.

    • Overriding deals with two methods, one in parent class and one in child class and both have same name and signature.

    • Subclass method overrides the method from super class.

    • In overriding sub classes access modifier must be greater than parent class E.g if we use public abc() in parent class and private abc() in sub class that will throw exception.

Static Class Loading and Dynamic Class Loading

Abstract Class and Interface

Java Packages

Here are some libraries available in java package to help code better. We will discuss them all eventually.
java packages

Constructor

Question: Can constructors be synchronized in Java?

No. Java doesn’t allow multi thread access to object constructors so synchronization is not even needed.

Question: Are constructors inherited? Can a subclass call the parent’s class constructor?

You cannot inherit a constructor. By overriding a superclasses constructor you would erode the encapsulation abilities of the language. By Super keyword we can call the parents class contructor.

Static

Static parent → Static child → Instance parent → Constructor parent → Instance child → Constructor child.

Final, Finalize and Finally

Object Class

Every class has Object as super class. It has the following non-final methods:

It has the following final methods:

Equals and HashCode

public class Tiger {
private String color;
private String stripePattern;
private int height;
public String getColor() {
 return color;
}
public String getStripePattern() {
 return stripePattern;
}

public Tiger(String color, String stripePattern, int height) {
 this.color = color;
 this.stripePattern = stripePattern;
 this.height = height;
}
@Override
public boolean equals(Object object) {
 boolean result = false;
 if (object == null || object.getClass() != getClass()) {
   result = false;
 } else {
   Tiger tiger = (Tiger) object;
   if (this.color == tiger.getColor() && this.stripePattern == tiger.getStripePattern()) {
       result = true;
       }
  }
return result;
}
@Override
public int hashCode() {
 int hash = 3;
 hash = 7 * hash + this.color.hashCode();
 hash = 7 * hash + this.stripePattern.hashCode();
 return hash;
}
}

Clone

Public Object Clone(){
Try{
Return super.clone();
}}
Public Object Clone(){
Try{
Object obj = (Object) super.clone();
Return obj;
}}

Don’t worry about the try statement, we will discuss in detail eventually.

Aggregation and composition

Primitive and Wrapper Type

A variable of a primitive type directly contains the value of that type. Java has eight primitive types: byte, short, int, long, char, boolean, float and double.

A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types and various other supporting, operational methods. It is slower to use the Object wrappers for primitives than just using the primitives. You’re adding the cost of object instantiation, method calls, etc. Each of Java’s eight primitive data types has a class dedicated to it like Byte, Short, Integer, Long, String, Boolean, Float and Double.

Autoboxing and Unboxing

Casting

byte → short → int → long → float → double

int i = 5; long j = i;

long j = 5;

int i = j; (THIS IS WRONG, it will give classCastException)

int i = (int) j;

You just completed part 1 👏👏

Thank you for reading!😀 I hope you enjoyed it and please share if you enjoyed it.