Skip to main content

Posts

Abstract Factory Design Pattern ex Recharge

package com.freerecharge.agents; public interface DTHRechargeAgent {             float recharge(String acountNo, String pkg, float amount); } ----------------------------- : package com.freerecharge.agents; public interface MobileRechargeAgent {             boolean recharge(String mobileNo, String pkg, float amount); } package com.freerecharge.agent.factory; import com.freerecharge.agents.DTHRechargeAgent; import com.freerecharge.agents.MobileRechargeAgent; public abstract class AbstactAgentFactory {             // factory method for MobileRechargeAgent component interface             public MobileRechargeAgent newMobileRechargeAgent() {              ...

Why String class is final

5 Reasons of Why String is final or Immutable in Java Why String class is made Immutable or Final in JavaThough true reasons of why String class was made final is best known to Java designers, and  apart from that hint on security by James Gosling, I think following reasons also suggest Why String is Final or Immutable in Java. 1) String Pool Java designer knows that String is going to be most used data type in all kind of Java applications and that's why they wanted to optimize from  start. One of key step on that direction was idea of storing String literals in String pool. Goal was to reduce temporary String object by sharing them and in order to share, they must have to be from Immutable class. You can not share a mutable object with two parties which are unknown to each other. Let's take an hypothetical example, where two reference variable is pointing to same String object: String s1 = "Java"; String s2 = "Java"; Now if s1 changes t...

Reflection

Reflection API: ================================= -> Reflection API is used for reading a class at execution time, loading its byte code     and creating its object and further calling its datamembers and member methods. -> Reflection API contains all information about our class that is what types of modifiers,     constructors, inner classes, methods etc... -> Reflection acts as Mirror

String & StringBuffer & StringBuilder

**** String **** -> String is immutable  ( once created can not be changed )object.    The object created as a String is stored in the  Constant String Pool  .    Every immutable object in Java is thread safe ,that implies String is also thread safe .    String can not be used by two threads simultaneously.    String  once assigned can not be changed. **** StringBuffer  *** -> StringBuffer is mutable means one can change the value of the object .    The object created through StringBuffer is stored in the heap .    StringBuffer  has the same methods as the StringBuilder ,    but each method in StringBuffer is synchronized that is StringBuffer is thread safe . ****** StringBuilder **** -> StringBuilder  is same as the StringBuffer ,    that is it stores the object in heap and it can also be modified .    The main difference between ...

Immutable_Object & Class

** What is immutable class..?? ================================= -> Immutable class is a class which once created, it’s contents can not be changed. -> Immutable objects are the objects whose state can not be changed once constructed.    e.g. String class. -> All wrapper classes in java.lang are immutable –    String, Integer, Boolean, Character, Byte, Short, Long, Float, Double. *** Why use immutable class..?? ===================================== -> Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".    If one reference variable changes the value of the object, it will be affected to all the reference variables.    That is why string objects are immutable in java. -> In case of String object, just because of SCP a single object is referred by multiple references.    by using one reference if we are allowed to change the content the...

What is Framework

What Is Framework: ====================================== -> A framework is a platform for developing software applications. -> It provides a foundation on which software developers can build programs for a specific platform. -> For example, a framework may include predefined classes and functions that can be used to process input,    manage hardware devices, and interact with system software. -> A framework is similar to an application programming interface (API), though technically a framework includes an API.

ClassNotFound & ClassNoDefFound

-> It is an exception. It is of type java.lang.Exception.    where as It is an error. It is of type java.lang.Error. -> It occurs when an application tries to load a class at run time which is not updated in the classpath.    where as It occurs when java runtime system doesn’t find a class definition,which is present at compile time, but missing at run time. -> It is thrown by the application itself. It is thrown by the methods like Class.forName(),    loadClass() and findSystemClass().    where as It is thrown by the Java Runtime System. -> It occurs when classpath is not updated with required JAR files.    where as It occurs when required class definition is missing at runtime. **** ClassNotFoundException *** Ex. public class MainClass {     public static void main(String[] args)     {         try         {          ...