Java: Basics

Java: Basics

30 April, 2021

Java is a General Purpose, Object Oriented Programming Language heavily used in Enterprise projects.

Java Naming Conventions

Following a standard naming convention would make a project codebase more readable and maintainable. Following are some of those standards used to name different things in Java.

Classes and Interfaces


interface Bicycle
class MountainBike implements Bicycle

interface Sport
class Football implements Sport



void changeGear(int value);
void speedUp(int increment);
void applyBrakes(int decrement);




java virtual machine

class loader subsystem

responsible for three activities


class loader reads .class file, generate the corrosponding binary and sace it in the method area.

for each .class file, JVM stores:

JVM stores these information inside the Heap memory.

Information about a class can be accessed using multiple classes JVM provides. Example:

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
  public static void main(String[] args) {
    Student s1 = new Student();

    // getClass returns an object of type `Class`
    Class c1 = s1.getClass();

    // get all method names
    Method m[] = c1.getDeclaredMethods();
    for (Method method : m)

    // get all field names
    Method f[] = c1.getDeclaredFields();
    for (Method field : m)

class Student {
  private String name;
  private int roll_no;

  public String getName() { return name; }
  public void setName(String name) { = name; }

  public int getRollNo() { return roll_no; }
  public void setRollNo(int roll_no) { this.roll_no = roll_no; }



Data types

Non-primitive data type is also known as reference data type



class Test {
  int count = 100;

Types of Variables

  1. Local Variable:
    • definded within a method, constructor or a block
    • scope of local variable is within the declaraion block
  2. Instance Variable:
    • decared inside a class
    • destroyed when the class instance is get destroyed
    • modifiers can be used to restrict the access of a variable
    • initialization is not mandatory. default value is 0.
    • can be accessed only by creating an object
  3. Static Variables
    • declared with static keyword within a class outside of any methods
    • can have only one copy of Static variable, irrespective of how many objects were created
    • create at the start of program. and destroed when the execution is finished
    • initialization is not mandatory. defaults to 0.
    • trying to access static variable through an object would throw an warning, but won’t halt the execution. compiler wil replace the object name with class name
    • static variables can be accessed without initializing an object


class Loop {
  public static void main(String[] args) {
    // while
    while (true) {

    // for loop: the classic for loop

    // enhanced for loop
    // when there's no need for element index,
    // enhanced for loop can be used
    String array[] = {"Jon", "Snow"};

    for (String s : array) {

    // do while: similar to other languages


Strings in Java can be declared in two ways. Strings are baked by a char array internally. Hence, they are immutable. All strings are terminated with \0(null character).

class StringExample {
  public static void main() {
    String str1 = "String"; // stores in constant pool
    String str2 = new String("kaboom"); // stores in heap

    // to store a string from heap to constant pool,
    // `intern` method.
    String str3 = str2.intern();

Some classes that provides more flexible ways to work with string:

String Methods


class Arr {
  public static void main(String args[]) {
    int myArr[] = new int[20]; // array of int of length 20

Jagged Array

a jagged array is an array of arrays such that member arrays can be of different sizes.

class Jagged {
  public static void main(String args[]) {
    int arr[][] = new int[3][];
    arr[0] = new int[10];
    arr[1] = new int[12];
    arr[2] = new int[15];

Clone an Array

there are multiple methods for cloning an array.

Shallow Clone

does not clone, just set the memory location

int[] arr = new int[10];
int copy[] = new int[arr.length];

copy = arr;

Iterate and Copy

int[] arr = new int[10];
int copy[] = new int[arr.length];

for (int i = 0; i < arr.length; i++) {
  copy[i] = arr[i];

using Clone()

int[] arr = new int[10];
int[] copy = arr.clone();

using Arrays.copyOf()

import java.util.Arrays;

int[] arr = {1, 2, 3, 5, 10};
int[] copy = Arrays.copyOf(arr, 5); // copy first 5 element

using Arrays.copyOfRange()

import java.util.Arrays;

int[] arr = {1, 2, 3, 5, 10};
int[] copy = Arrays.copyOfRange(arr, 2, 4); // 2 is index, and 4 is the length

Array to String

an array can be converted to string using Arrays.toString() or StringBuilder.append()

import java.util.Arrays;

int[] arr = {1, 2, 3, 5, 10};

StringBuilder str = new StringBuilder("Hello");
char[] world = { ' ', 'W', 'o', 'r', 'l', 'd' };


Array equality

to check whether two arrays are equal, Arrays.equals() method can be used: Arrays.equals(arr1, arr2);. Similarly for nested arrays, use Arrays.deepEquals(arr1, arr2);

Rules for method overloading

  1. access modifiers can allow more, but not less, access than the overriden method. trying to convert a protected method to private would result in compile-time error
  2. methods declared as final cannot be overriden
  3. static methods cannot be overriden
  4. private method cannot be overriden
  5. overriden method must have same return type (or should be a sub-type)
  6. constructors cannot be overriden
  7. exception handling
    1. subclass overriden method cannot throw checked exception if superclass method does not throw any exceptions
    2. if superclass method throws an exception, subclass overriden method can throw the same exception or subclass exception. there is not problem is subclass method does not throw any
Overriding Overloading
runtime polymorphism compile-time polymorphism
is about same method, same signature but different class connected through inheritance is about same method but different signature


exception is an unwanted event, which occurs during the execution of program that disrupts the normal flow of the instructions.

an error indicates a serious problem that a resonable application should not try to catch. an exception should be catched when possible.

Exceptions and Errors are subclass of class Throwable.

Unchecked Exceptions and Errors are the ones that won’t be checked during the compile time. whereas, Checked exceptions will always be checked. In Java, all exceptions except for RuntimeException are Unchecked. All errors are unchecked.

Checked exceptions must be handled.

Throwing an Exception

throw new ArithmeticException("/ by 0");

In order to write a custom exception, one must extend either Throwable or one of the subclasses of Throwable. generally, people extend Exception class.

Handling Exceptions

there are two ways to handle an exception:

// using `throws`
public void sleep() throws InterruptedException {

// using try and catch
public void sleep() {
  try {
  } catch (InterruptedException e) {


Java supports Multi-Threading. By default, Java runs everything on a single thread. That means, if there’s any task that takes some time to process, codes after that task have to wait.

Multi-Threading can be achieved in two ways in Java.

// extending the thread class
class Demo extends Thread {
  // todo: tasks

// elsewhere
Thread t1 = new Demo();

when implementing the Runnable interface, thread can be created by passing an instance of the class to Thread object’s constructor.

class Demo extends SomeOtherClass implements Runnable {
  public void run() {
    System.out.println("hello world");

// elsewhere
Demo d1 = new Demo();
Thread t1 = new Thread(d1);

Lambda Function

Lambda expressions are similar to methods, but they don’t need a name, and can be implemented right in the body of a method.

Lambda functions can be compared to Fat Arrow functions in JavaScript

// lambda expression
ArrayList<Integer> n = new ArrayList<Integer>;

n.forEach(n -> { System.out.println(n); });

Lambda functions can be stored in a variable. Consumer interface can be used to store it.

import java.util.function.Consumer;

// elsewhere
Consumer<Integer> method = (n) -> { System.out.println(n); }

To use a lambda expression in a method, the method should have a parameter with a single-method interface as its type. Calling the interface’s method will run the lambda expression.

abdus s. azad
About the Author
Abdus is a Software Engineer working at Level. In his free time, he likes to play with Linux-based machines. Contact him at