Cheatography
https://cheatography.com
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.out.println("Hello World");
Scanner object -> Scanner s = new Scanner(System.in);
Find the length (int) -> msg.length()
To lower/uppercase -> msg.toLower/UpperCase()
Replace a string -> msg.replaceAll(String a, String b)
charAt(int index) -> char value at the specified index
equals(String/Object) -> if strings values are the same
toCharArray() -> Convert string to character array
Switch
switch (num) {
case 1: doSomething();
break;
default: doThis();
break;
}
|
|
Exception Handling in Java
ArithmeticException |
Errors because of an arithmetic expression. |
ZeroDivisionException |
Dividing a value with 0. |
ArrayIndexOutOfBoundException |
No such index number. |
FileNotFoundException |
Accessing a file that does not exist. |
IOException |
Input or output error. |
NullPointerException |
Referencing a null object. |
NumberFormatException |
When typecasting does not work. |
StringIndexOutOfBoundException |
When there is no index for a particular string. |
try…..catch 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.io.File --> Create or Open File --> Read or Write in to file --> Close the file
Inheritance
object of a parent class cannot be cast down to object of the child class One one = new Two(); -> Two two = (Two) one;
|
Polymorphism 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, constructors are not inherited by the child class and hence they cannot be overridden. method needs a return type
|
Java constructors do not have return type. error “The constructor One(String) is undefined”.
|
|
|
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 abstraction. In an interface class, we only have abstract methods and no method body. With interfaces, we can also achieve multiple inheritance in Java.
Method Overloading
When a class has two or more methods with the same name but different parameters, it is method overloading. |
Example of Overload:
public printer(String x){}
public printer(String 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 printer(String x){}
public printer(String x, String y){} // conflict
public printer(String 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 subclass/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 unsuccessful, JDK will returns error.
Example of overriden helloWorld() method :
Class Student
public void helloWorld(){
System.out.println("Hello");
}
Class GradStudent extends Student
@Override
public void helloWorld(){
System.out.println("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 |
|