Java Cheatsheet
Java Cheatsheet
Class can have only public and default access. Public class needs to be in same name java file. Single java file can contain more then one non public class but can have only one public class. A public class can be seen by all classes from all package A class with default access can be seen only by classes within the same package. Java file with no public class have no naming restriction. Class can also have final and abstract & strictfp non access modifiers. An abstract class can not be inistiated. A final class can not be subclassed. A class can not be both final and abstract. Class visibility can be seen in 3 parameter If a class can extend another class? If a class can create instance of another class? If a class can access methods and variable of another class?
Constructor Cheatsheet
Constructor is invoked when a new object is created. Constructors can also be overloaded but it can not be overridden. Every class has at least one constructor. If user doesnt provide any JVM will provide a default no arg constructor. Abstract class also has constructor. Constructor must have the same name as class. Constructor cant have a return type. If a method with the same name as class has return type will be treated as normal member method and not constructor. Constructor can have any access modifier(All). Default constructor is a no arg constructor which calls the no arg constructor of super class. In case super class doesnt have any no arg constructor then it will throw a run time exception. In case where a class has default constructor, its super class needs to have a no arg constructor. First statement of a constructor can be either this or super but can not be both at the same time. If coder doesnt write any this or super call then compiler will add a call to super. Instance member can be accessible only after the super constructor runs. Interfaces do not have constructors. Constructor are not inherited. Hence can not be overridden. Constructor can not be directly invoked. It will be invoked(Implicitly) when a new object is created or a call by other constructor.
Inner classes defined within method are method local inner class. Method local inner class can not access method local variable. Final & Abstract are the only modifiers available to method local inner class. Ananymous inner class dont have any name. Inner classes having Static modifier are known as Static inner class. Static nested class can not access non static member of outer class.
String Cheatsheet
String Object is used for holding String of text. Part of java.util package. Strings can be added by using concatenation Operator(+). Characters use single quotes( ) as delimiter while Strings use double quotes( ) as delimiters.
String objects are immutable. Value of the string object can not be changed once assigned. String class is final means method of the String class can not be overriden. All String literals are added to String pool by JVM. String have method length() while Array have a property length to check the length. To overcome the immutable property of String StringBuffer and StringBuilder comes into picture.
Thread Cheatsheet
Thread can be created by extending Thread Class and overriding the run() method. Thread can also be created by Implementing Runnable interface and then calling Thread constructor that takes Runnnable argument. start() method can be called on Thread Object only once. Once start() method is called on Thread Object it becomes thread of execution. Before start() method called it is said to be in new state and is not considered alive. There is no guarantee of order in which Thread will get executed after getting started. We can influence the order in which Thread get executed by setting its(Thread) priority(1-10). A running thread may enter blocked/waiting state by wait(), sleep or join() call. A runnning thread may enter blocked/waiting state because it cant acquire the lock of a synchronized block of code. A dead thread can not be started again.
Interface Cheatsheet
Interface are 100% abstract class(Implicitly). Interfaces can be implemented by any class from any inheritance tree. All methods in Interfaces are abstract. Interface can have constants, these constants are public, static and final(Implicitly). Interface methods are implicitly public & abstract. Class implementing an interface can also be an abstract class. An abstract class which is implementing an interface need not implement all abstract method. A class can extend more then one Interface. Interfaces can not extend a class or implement an Interface.
Interface can extend another Interface. A non abstract class which is implementing an Interface needs to follow some rules . This class needs to provide concrete implementation of all abstract method. All rules of Overriding needs to be followed. It must maintain the exact signature of method.
Serialization Cheatsheet
Serialization interface needs to be implemented in order to make object serialized. Transient instance variable doesnt serialized with Object state. If Super class implements Serializable then sub class are also Serializable automatically. If Super class is not serializable then when sub class is de serialized then super class constructor will be invoked.
Overloading Cheatsheet
Using the same method name but with different argument is called overloading. Constructors can also be overloaded Overloaded methods must have different argument set. Overloaded methods may have different return type. Overloaded methods may have different access modifier. Overloaded methods may throw different exception broader or narrow no restriction Methods from super class can also be overloaded in subclass. Polymorphism applies to overriding not Overloading Which overloaded method will be invoked is decided on compile time on the basis of reference type.
Overriding Cheatsheet
Constructor can not be overriden. Overriding methods must have the same argument set. Overriden methods must have same return type. These return type can also be the subclass(covariant return). Overriden method can not have more restrictive access modifier. Overriden method can not throw new or broader exception(Checked).
Overriden method can throw any unchecked exception. Final methods can not be overriden. Private methods are not inherited to subclass hence it can not be overriden in subclass. Polymorphism applies to overriding. Object type determines which overridden method will be invoked and that will be decided at theruntime.
Exception Cheatsheet
Checked & unchecked exception are two type of Exception. Checked exception are those exception which are subtype of Exception class but excluding classes which extends Runtime exception. Subtype of Error and Runtime Exceptions comes under unchecked Exception. Finally block will always be invoked in all condition. System.exit() is the only way when finally block will not get executed coz in that case JVM will shut down. Custom Exception can also be created by extending Exception class. Catch block should be order in the form of most specific to most general. Otherwise compiler will complain for unreachable code.
Local Variables dont get default value, hence local variable need to be initiaited before it can be used.
Instance Variable Type Default Value boolean false byte (byte)0 short (short) 0 int 0 long 0L char u0000 float 0.0f double 0.0d Object null
Tags: instance variable, instance variable default value, java instance variable
Primitives Cheatsheet
TO DO