Posts

Wrapper Classes

Wrapper Class: The primitive data type in java is not object. Some of built-in-classes handle only objects so to convert primitive to object we use wrapper class. object to primitive :  // called un-boxing   byte b = obj.byteValue(); primitive to object : // refer as a boxing byte b; Byte obj = new Byte(b); Numeric objects to String objects String str = obj.toString(); String object to Numeric objects Byte obj = Byte.valueOf(str); String object to primitive number byte b = Byte.parseByte(str); System.out.println(Byte.MAX_VALUE); System.out.println(Byte.MIN_VALUE); System.out.println(Byte.SIZE); Character class: Constructor: Character chObj  = new Character('c'); Boolean Class: Boolean obj = new Boolean(); Auto-Boxing : primitive type is automatically converted to object without any casting. Integer obj = 200; //auto-boxing; int i = obj   //auto-unboxing

String and StringBuffer Classes

# String in java is object. Strings are immutable we cannot change the string after creation. # both String and StringBuffer classes are final. #String provide different types of constructors: 1) String str =" its string "; 2) String str = new String(); // its a empty string( str is a object, String str = null , str is not an object) 3) String str = new String( "its string "); 4 String str2 = new String (str); 5) char ch[] = { 'a', 'b', 'c'};     String str2 = new String (ch); 6) String str2 = new String (char ch[], int offset, init count); # String Operations : String concatenation : "+" to concat strings . we can concat string with another data type like a int so first it will convert to string then it will add with string. Character extraction :  char charAt(int index);  char[] toCharArray(); boolean b1 = str.equals(str2); boolean b1= str.equalsIgnoreCase(str2); int  b =          str.compareToIgnoreCase(st...

Exception Handling

throwable is the  sub class of object class . and it have two sub class: a) Exception b) Error  Error should not handle its system related which id not be able to recoverable like Assertion errors, virtual memory errors( outofmemoryerror, stackoverflowerror) Exception can be handle at run time and a appropriate action can be taken. exception like Arithmetic , NullPointer, IOExecption etc. Exception handling is managed by five keywords: try, catch, throw , throws and finally. monitorerd code should be inside try block and try block should have {} if it have zero or more statements. followed by one or more catch statements or one finally statements or both but one of them is necessary after try block. At most one catch block executed irrespective catch blocks and program is continue to that try statement which thrown error during execution and then control goes to finally block if it is present in block.finally block is executed even error/exception handle or no...

Interface # java

To implement a interface, a class must create the complete set of methods  defined by the interface. method of interface is implicit are abstract public variable of interface is implicit are public. static and final means we cant change  the value of interface variable value. Variables as a object reference we can create which will be called at run time. using interface object we can methods and it will find definition at run time. interface demo { method (); } class test implements demo { method () { System.out.println("interface demo"); } public static void main (String[] arg) { demo obj = new test(); obj.method(); } } Interface can access only these methods which is declare in interface interface variable cannot access the other methods and variables. if class doesn't define methods of implemented interface then it should be define abstract( only abstract class can have abstract methods) An Interface can extends another interface then im...

Inheritance in java

Extending a class: To inherit a class class sub_class extends  base_class {} Every class is sub-class of object class (implicitly extending every  class we can use or extend its methods.). object class : have following methods protected Object clone() throws CloneNotSupportedException public boolean equals(Object obj) protected void finalize() throws Throwable public final Class getClass() public int hashCode(): reference of object  public String toString() ic final void notify() public final void notifyAll() public final void wait() public final void wait(long timeout) public final void wait(long timeout, int nanos) private members of a super class is not accessible we can access using some methods . Up-Casting:  its happen implicitly, this is like widening conversion of primitive types  super class_object = sub class object. super class object can access only super class methods. super class object dont have any idea what its s...

What we should know about Class #java

Class is a user defined data type, using data type we can  create variables of that data type similarly we can create objects of that class, So instance of that class, object id a variable of user defined data type. modifiers class <class name> { Class  body ...//body of class  can consist data members and methods. }   Modifiers : that control accessibility of class , data members and methods. Class :            a) public            b) if no modifier  is defined then by default is package. but cant define explicitly. ( class can be  abstract or final) Members: a) public, private, protected The memory for object is always dynamically allocated using new operator. Variables in java: a) Local variable : inside  a method b) Instance (non static) variable : outside a  method c) static variable : use dot (.) to access variables, dot operator in java is similar to ...

Creational Design Pattern: Factory Design Pattern

Creational Design Pattern : Designing pattern related to creation of a object or instance of Class. 1) Factory Design Pattern: When we have a super class which have many sub class and we have to return instance of a sub class according to input. Following are the main part of factory design pattern: # super class: super class may be interface , abstract class or a normal java class. # sub classes: # factory class: # super class: public abstract class Computer { public abstract String getRAM(); public abstract String getHDD(); public abstract String getCPU(); @Override public String toString(){ return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU(); } } # sub classes: Let’s say we have two sub-classes PC and Server with below implementation. public class PC extends Computer { private String ram; private String hdd; private String cpu; public PC(String ram, String hdd, String cpu){ this.ram=ram; this.hdd=h...