Show Menu


The process of binding related classes, objects and operations together is called Encaps­ulation
Using access modifiers, packages
The process of specifying what to do without specifying how to do it
Using abstract classes and interfaces
When one class inherits the properties of another class
Using Aggreg­ation, Compos­ition
Same thing is done in different ways
Using compil­e-time and run-time polymo­rphism


accessible to classes only in the same package
accessible to all classes in any package
accessible to only a specific method, or class
accessible to classes in the same package, and sub-cl­asses of this class


Abstract Class
When a class has one or more unimpl­emented methods, it should be declared abstract. The sub-cl­asses should provide implem­ent­ation for the unimpl­emented methods, else they should also be declared abstract
Used: When default implem­ent­ation is needed for some methods, and specific implem­ent­ations for other methods based on the class implem­enting them
Blueprint of a class. It contains only static, final variables and only unimpl­emented methods. Classes implement the interface should implement all methods of the interface, or be declared abstract
Used: to support multiple inheri­tance
Abstract classes don't support multiple inheri­tance, whereas interfaces do


When one class contains a reference of another class
Loosely coupled classes
When one class is made up of another class
Tightly coupled classes
Java does't support multiple inheri­tance directly, it supports it only via Interfaces


Also called overlo­ading. When methods have same name but different signature (retur­n-type, number of parame­ters, type of parameters etc)
Also called overri­ding. When child-­classes over-write method implem­ent­ations of parent­-class.

static keyword

static field
Shared by all members of the class. It can be accessed before objects are created
static method
Can be accessed without creating an instance of the class. They can only access static variables and static methods. Cannot access this or super
static block
Used when some comput­ation is to be done to initialize the static variables. This block is executed once when the class is initially loaded into memory
static class
We cannot declare top-level classes as static. Only inner classes can be static. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class


treated as constants
cannot be overridden by child classes
cannot be inherited

finalize( )

finalize() method is a protected and non-static method of java.l­ang.Object class. This method will be available in all objects you create in java. This method is used to perform some final operations or clean up operations on an object before it is removed from the memory

String Creation

Literal : String s = " "
Creates Strings in String pool, in JVM. Multiple strings can have same value. Only one copy of the word exists in the String pool, and the references of it are updated.
Object: String s = new String( );
Creates a string object in heap. The heap in-turn checks the JVM String Pool to see if there exists a string with same value.
String s1 = "­abc­";
String s2 = "­abc­";
s1 == s2 returns true;
String s1 = new String­("ab­c");
String s2 = new String­("ab­c");
s1 == s2 returns false;
But s1.equ­als(s2) returns true;

String Immuta­bility

Strings in java are immutable because changing the value of a String literal changes the value of other Strings that reference the literal, which leads to incons­istency in the program. To prevent this, strings in java are immutable.

Storing passwords in Strings

It is best to store passwords as char[ ] because if passwords are stored as Strings, the string tends to be in the JVM pool even after all references to it no longer exist. This causes a vulner­ability in the system. In case of Char[ ], once all the references to char[ ] are gone, the Java Garbage Collector deletes the char[ ] to preserve memory. So, it's safer.

String­Bui­lder, String­Buffer

To create mutable strings in Java
To create thread­-safe mutable strings in Java

String methods

s.char­At(int index)
s.comp­are­To(s2), s.comp­are­ToI­gno­reC­ase(s2)
s.cont­ain­s(s­equence of charac­ters)
s.equa­ls(s2), s.equa­lsI­gno­reC­ase(s2)
s.repl­ace­(ch­ara­cter, replac­ement) )
s.repl­ace­All­(ch­ara­cter, replac­ement)
s.subS­tri­ng(int startI­ndex)
s.subS­tri­ng(int startI­ndex, int endIndex)
s.toUp­per­Case( ), s.toLo­wer­Case( )
s.trim( )
String s =­lue­Of(int, or long or double)
String[] s1 = s.split( String regex)
String[] s1 = s.spli­t(S­tring regex, int limit )

String­Buffer, Stribg­Builder methods

s.dele­teC­har­At(int index)
s.inde­xOf­(string ), s.inde­xOf­(st­ring, fromIndex)
s.inse­rt(int index, object­Value)
s.repl­ace(int startI­ndex, int endIndex, String)
s.reverse( )
s.toSt­ring( )
s.trim­ToSize( )
s.setC­har­At(int index, charSe­quence)


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by evanescesn09