Show Menu
Cheatography

oop java Cheat Sheet (DRAFT) by

oop javaoop javaoop javaoop java

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Most used

 
Print with new line -> System.ou­­t.p­­r­in­­tln­­("Hello World");
Scanner object -> Scanner s = new Scanne­­r(­S­y­st­­em.in);
Find the length (int) -> msg.le­­ngth()
To lower/­­up­p­e­rcase -> msg.to­­Lo­w­e­r/­­Upp­­er­C­ase()
Replace a string -> msg.re­­pl­a­c­eA­­ll(­­String a, String b)
charAt(int index) -> char value at the specified index
equals­­(S­tri­ng/­Object) -> if strings values are the same
toChar­­Ar­ray() -> Convert string to character array

Switch

 
switch (num) {
case 1: doSome­thi­ng();
break;
default: doThis();
break;
}

Q1

bubble sort

binary search

 

Exception Handling in Java

Arithm­eti­cEx­ception
Errors because of an arithmetic expres­sion.
ZeroDi­vis­ion­Exc­eption
Dividing a value with 0.
ArrayI­nde­xOu­tOf­Bou­ndE­xce­ption
No such index number.
FileNo­tFo­und­Exc­eption
Accessing a file that does not exist.
IOExce­ption
Input or output error.
NullPo­int­erE­xce­ption
Refere­ncing a null object.
Number­For­mat­Exc­eption
When typeca­sting does not work.
String­Ind­exO­utO­fBo­und­Exc­eption
When there is no index for a particular string.

try…..c­atch Statement in Java

class Exception_Handaling
{
public static void main(String args[])
{
try
{
int x = 10/0;
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
}

File Reading and Writing Process

// Create a file
File Obj = new File("file.txt");
// Write in the file
import java.io.FileWriter;
FileWriter write_file = new FileWriter("file.txt");
write_file.write(“   ”)       // write in the file
write_file.close()            //close file
//Read from a File:
File read_file = new File("File.txt"); 
System.out.println(read_file.nextLine());
read_file.close();
import java.i­o.File --> Create or Open File --> Read or Write in to file --> Close the file

Inheri­tance

object of a parent class cannot be cast down to object of the child class
One one = new Two(); -> Two two = (Two) one;
Polymo­rphism for static methods does not work in the same way as instance methods.
In Java, a child class can make the method abstract which is inherited from the parent class.
If a reference of a parent class is pointing to the object of a child class, it can only call methods defined by the parent class.
“The method print() is undefined for the type One”.
Unlike methods, constr­uctors are not inherited by the child class and hence they cannot be overri­dden.
method needs a return type
Java constr­uctors do not have return type.
error “The constr­uctor One(St­ring) is undefi­ned”.
 

Interface

interface Sun{  
void rays();  
}  
class Mercury implements Sun{  
public void rays(){System.out.println("Mercury");}  
}  
class Earth implements Sun{  
public void rays(){System.out.println("Earth");}  
}  
class Sunlight{  
public static void main(String args[]){  
Sun e =new Earth(); 
e.rays();  
}}
An interface in Java helps to implement the concept of abstra­ction. In an interface class, we only have abstract methods and no method body. With interf­aces, we can also achieve multiple inheri­tance in Java.

Method Overlo­ading

When a class has two or more methods with the same name but different parame­ters, it is method overlo­ading.
Example of Overload:
public printe­­r(­S­tring x){}
public printe­­r(­S­tring x, String y){}

If the input is 2 string, it will go to the second method instead of first one.

But you cannot overload by using the same input type sequence. For example:
public printe­­r(­S­tring x){}
public printe­­r(­S­tring x, String y){} // conflict
public printe­­r(­S­tring y, String x){} // conflict

Java will not allow this to be run, because it cannot determine the value.

Override

When you have inherit some of the class from parents, but you want to do something different. In override feature, all the subcla­­ss­/­class object will use the newer method.

To make sure JDK knows what you are doing, type @Override in front of the public name. If the override is unsucc­­es­sful, JDK will returns error.

Example of overriden helloW­­orld() method :
Class Student
public void helloW­­or­ld(){
System.ou­­t.p­­r­in­­tln­­("H­e­l­lo­­");
}

Class GradSt­­udent extends Student
@Override
public void helloW­­or­ld(){
System.ou­­t.p­­r­in­­tln­­("Hello World");
}

Rules of Overridden methods:
1. Access modifier priority can only be narrower or same as superclass
2. There is the same name method in superclass / libraries