Posts

Showing posts from February, 2015

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...

Creational Design Pattern : Singleton Design Pattern:

Creational Design Pattern : Designing pattern related to creation of a object or instance of Class. 1) Singleton Design Pattern: It allow to create only objects of a class. So user cant create more then one object of a class. this pattern allows only one instance of a Class in JVM Advantage: It is useful in caching, reader-writer problem, Creating a object of a class is very expensive... etc Disadvantage: it cannot use multithreading programming , concurrent processing , thread pool case ... etc. There are many ways to create singleton class to create only one instance available in JVM but following is common to all. # Private Constructor so no other class cannot create instance of Class. # Private  static variable of class that is only instance of the class. # Public static method that return the instance of class. a) Eager Initialization: This one is the easiest way to initialized a singleton class. draw back of  this  is it creates instance at loading...

Design Patterns: The Secret Weapon of Expert Developers

Design patterns are reusable solutions to common problems in software design. They represent best practices refined over time by experienced developers and provide a shared vocabulary for discussing architectural solutions. Design patterns are typically organized into three main categories, each addressing different aspects of software construction. The Three Categories of Design Patterns 1. Creational Design Patterns Creational patterns focus on object creation mechanisms, providing flexibility in how objects are instantiated while hiding the creation logic. Rather than instantiating objects directly using constructors, these patterns offer more controlled and appropriate ways to create objects based on the situation at hand. The five creational patterns are: Singleton Pattern ensures that a class has only one instance throughout the application and provides a global point of access to it. This is particularly useful for managing shared resources like database connections or con...

OOP Fundamentals

All programs have two things  data and processing part.  The procedural programming language: focus  on processing part means how to process. it based on top-down design approach. Object oriented programming language: focus on data part  means whom to process. It based on down-top design approach. Basic concepts of OOPS. # Abstraction : hiding details , build in data type also a example of abstraction which hide inside details. object oriented programming language abstracting using object and classes. ## Object : object is a instance of a class, where class is build in data type and object is a variable. Object have its own: a) identification b) property c) behaviors ## Class represents similar properties and behaviors. Its a blue print to create objects. # Encapsulation: Encapsulation is process to bind data and code together. it help to hide data by declaring member of class by private and protected. Java class support to bind code and data. ...

Key points related to control statement in Java

# In java 0 cannot use as false or integer val for true , So for conditional expression   like if , for, while and do statement should have valid Boolean  value. # Java have goto statement as reserved  key word  but not for use instead of we can use labeled break statement or labeled continue statement as both are civilized form of goto statement. # Java allow to use byte , char, double , float and String to use as switch variable . ex . String name = "ram"; switch(name) { case "ram" : } # Java provide for each loop which iterate through code       String  tem[] = {"ram","rama"," raja " , "raka ka"};       for(String  name : tem)       System.out.println("temp = " + name); # Break : break statement can be inside loop and switch case. but labeled break can be used for any block. out:     for (int i = 0; i < 3; ++i) {             System.out.println("...

Key points related to operators in java:

Key points related to operators in java: 1) modulus operator can be use on float like 3.2%3.1= 0.1 2) instanceof  used to test the class of an object boolen state = object_name  indtanceof class_name state = true ( object is instance of that class ) else  return false 3) Right shift with zero fill (>>>)  is a logical sift   int y = -32; int x = y>>>4; => 268435454 shift 4 bits right and fill left with  zero although  >> is a arithmetic shift will give -2 as result. 4) Type conversion in expressions  if expression have  char , short  or Byte(all are converted into int before evaluation   of expression)  => int if expression   have long as higher type => long if expression have float as higher type => float if expression have double as higher type => double 5) overflow : ex. like int 32 bit if result of any expression increase to more then it will keep least 32...

Array in java

One dimensional  array:  Declaration of one dimensional array:  type variable_name[];   int test[] ; or  int[] test; Memory Allocation: test = new int[5];  Or in one step int test[]  = new int[4]; or int test[] = {12, 33,33,66} Array length: int a = test.length; Two dimensional array int test[][]; int [][] test; test = new int[2][4]; int test [][] = new int[2][4]; array length of two D array int row = test.length; int columns = test[i].length; 

Reading Run Time Input in Java

 There are many way to take input from keyboard. a) Scanner Class b) Console Class c) DataInputStream Class  d)  BufferedReader Class e) System.in f) InputStreamReader a) Scanner Class : import.util.Scanner  create Scanner Class object to use methods of scanner class  Scanner sc = new Scanner (System.in)       or reading form file or a string for file  File text = new File( "C:/aa.txt"); Scanner sc = new Scanner ( text);  int a = sc.nextInt(); for int value  float a = sc.nextFloat(); double a = sc.nextDouble(); String a = sc.next(); or many methods to related to expression sc.nextLine(); skip line  sc.hasNext(); sc.findInLine("string" ); find a pattern in a line sc.findWithinHorizion("stinddd" , 10);    b) Console Class  : import.java.io.Console  Console con = System.console(); String name = con.readLine(); c) DataInputStream  text = DataInputStream(System.io); String...

Basics of java programing

Java is a pure Object oriented programming language. Some key points related to java programming language: 1) Sequence of java file:                                       a) Package name ( first line of your java file if package is there excluding comments. comments can be there before package name)                                       b) Import statement                                       c) Class / Interface definition 2) java class  from you want to start execution must have main which is defined like "public static void main( String args[])"  public: every one can access or can be call from outside.  static: main is class level method anyone can call without creating obje...

Introduction to Java

Java is an object-oriented programming  language which is platform independent.  Java can be used to develop  many types of applications. 1) Desktop applications 2) Applets 3) Distributed applications 4) Mobile applications 1) Desktop applications: Its like a program that runs on desktop. 2) Applets: Applets is an small application which is executed on web-browser within JVM. Applets are like a java program which reside on server. Applets  can't be executed like a standalone application. To execute a Applet we need to embedding it into HTML page. when browser download such a HTML page it subsequently load executable file which have applet executed on local machine. 3) Web application: Web applications run on server. When we access a URL we are accessing a web application. The main components of web applications are. 1) Java servlets 2) Java server pages (JSP) 3) HTML Java servlets are java programs, which runs on server and send the results to clie...