DEV Community

Cover image for Java recap - Espresso Guide ☕️
Aravind
Aravind

Posted on

Java recap - Espresso Guide ☕️

Hi there,

This post features the basic concepts with dumb examples for quick reference.

Table of Contents

 1. How to print ?
 2. How to get user input ?
 3. How write a class ?
 4. Wrapper Classes
 5. Inheritance
 6. Method overriding
 7. Final and Static
 8. interface
 9. Abstraction
 10. Exception handling
 11. File handling

How to print ?

public class Main {   
   public static void main(String[] args) {   
      System.out.println("Hello world");  
   }   
}  
Enter fullscreen mode Exit fullscreen mode

you can also use

  • System.out.print("text"); - prints with out newline at end )
  • System.out.printf("%d text" , 55); - inserts value in the printed message
System.out.printf("%d text" , 55); //replaces %d with 55
//similarly for char,String,boolean we have
System.out.printf("%c text" , 55);
System.out.printf("%s text" , "hi");
System.out.printf("%b text" , true);
Enter fullscreen mode Exit fullscreen mode

How to get user input ?

import java.util.Scanner;  // Import the Scanner class

class Main {
  public static void main(String[] args) {
   Scanner myObj = new Scanner(System.in);  // Create a Scanner object
   String input = myObj.nextLine();  // Read user input
  }
}
Enter fullscreen mode Exit fullscreen mode

How write a class ?

A class is a "blueprint" for creating objects

class Car {
   int noOfWheel  = 6;
}
Enter fullscreen mode Exit fullscreen mode

We can create an object of the class by

public static void main(String[] args){
   Car bmw = new Car();
}
Enter fullscreen mode Exit fullscreen mode

Classes with constructor

public class Car {
   // Instance Variables
   String name;
   int year;
   String color;

   // Constructor 
   public Car(String name,int year,String color){
      this.name = name;
      this.year = year;
      this.color = color;
   }
}
Enter fullscreen mode Exit fullscreen mode

Now the object can be created as

public static void main(String[] args)
   {
      Car bmw = new Car("bmw", 2022,"blue");       
   }
Enter fullscreen mode Exit fullscreen mode

Wrapper Classes

Wrapper classes are reference of primitive datatypes.
Integer is the wrapper class of primitive int.
Boolean - boolean
Character - char, etc

autoboxing - automatic converison of primitive to wrapper
Integer a = 123;
unboxing - conversion of wrapper to primitive
if(a == 123){} // here the java unboxes the primitive to compare the values


Inheritance

Inheritance allows one object acquires all the properties and behaviors of a parent object.

class Worker{  
 float salary=40000;  
}  
class MailMan extends Worker{  
 int bonus=10000;  
 public static void main(String args[]){  
   MailMan m=new Mailman();  //m will have all characters of Worker class in addition to its own properties.
}  
} 
Enter fullscreen mode Exit fullscreen mode

Method overriding

Overriding the method of superclass in subclass

class Vehicle{  
  void run(){System.out.println("Vehicle is running");}  //method in parent
}  

class Bike extends Vehicle{  
  void run(){System.out.println("bike is running");} // method in child
}  
Enter fullscreen mode Exit fullscreen mode

Final and Static

Final classes cannot be inherited.
Final variable cannot be modified.
The static used to share the same variable or method of a given class. It can be accessed without an object instance.

class Student{  
   String name;  
   static String college ="abc";//static variable   
}  

public class Test{  
 public static void main(String args[]){  

   Student.college = "efgh"; // the static variable can be accessed without an object. it also changes the particular variable for all objects of the class.

   Student s1 = new Student();
   s1.name = "rahul"  //object must be created to access non-static variable
 }  
} 
Enter fullscreen mode Exit fullscreen mode

interface

It specifies what a class has or must do.
Inhereted classes can have only one superclass while interface allows to implement methods from different interfaces.

interface sayHello{  
   void hello();  
} 
interface sayHi{  
   void hi();  
}   

public class say implements sayHello , sayHi {
   @override
   void hello(){ // something };
   @override
   void hello(){ // something };
}   
Enter fullscreen mode Exit fullscreen mode

Abstraction

The abstract keyword can be used with a class or method.
Abstract classes cannot have an object, but their subclasses can.

abstract class Bike{  
  void run();  
} 
Enter fullscreen mode Exit fullscreen mode

Abstract methods specifies the method name without the method body. These methods must be ovveriden in the subclass.

class Bike{  
  abstract void run();  //no method body
}

class Honda extends Bike{  
   void run(){
     System.out.println("running safely"); // override
   }   
}  

Enter fullscreen mode Exit fullscreen mode

Exception handling

Exception handling helps to avoid program intreption by catching errors.

public class test{  
  public static void main(String args[]){  
   try{  
      //code that may raise exception  
   }catch(ArithmeticException e){
      //to do - in incase of exception
   }finaly{
     //to do - finaly  
  }  
} 
Enter fullscreen mode Exit fullscreen mode

Exeception e catches all exeptions but,its good practice to handle each exception individually. e.printStackTrace prints the error details.

File handling

File class can be used to create a file abstraction and FileReader will help with reading from files.

import java.io.File;
import java.io.FileReader;
public class file {
    public static void main(String[] args) {
        File f=new File("new.txt");
        try{
        if(f.exists())
        {
            FileReader fr=new FileReader("new.txt");
            int var=0;
            var=fr.read();
            while(var!=-1){
                System.out.println((char) var);
            var=fr.read();
            }
        }
        }
        catch(Exception e){
            System.out.println("Exception catched");

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)