Thursday, 12 March 2015

FILE WRITER CLASS (Core JAVA)

·         This class is used to write characters into a file after writing all the characters stream should be flushed using flush() method. After performing all the write operations the stream should be closed using close() method
NOTE: Generally flush() and close() will be written inside finally block
·         The File Reader class is used to read characters from the file. These characters are read by using read()
·         Read() will read the characters and stores as array of characters

·         Take input from keyboard (resume)

IO PACKAGE (Core JAVA)

·         java provides IO package to write program which can handle file as well as directory of system file
·         File class is used for basic operations. For e.g.,
1.       creating directory or folder
2.       deleting directory or folder
3.       to check existence of directory or folder
4.       to get path of directory or folder
5.       to check permission of directory or folder
·         some other basic classes are also available in IO package such as:
1.       File writer File reader:  It is used to read and write single character internally
2.       Buffer Reader and Buffer Writer: It is used to read and write Buffer of characters
3.       Object Input Stream and Object OutputStraem: It is used to read and write java object and instance
4.       Binary Input Stream and Binary Output Stream: It is used to read and write binary characters and executable files
5.       File Input Stream and File Output Stream: It is used for general purpose and is basically used in selenium
FILE WRITER CLASS
·         This class is used to write characters into a file after writing all the characters stream should be flushed using flush() method. After performing all the write operations the stream should be closed using close() method
NOTE: Generally flush() and close() will be written inside finally block
·         The File Reader class is used to read characters from the file. These characters are read by using read()
·         Read() will read the characters and stores as array of characters

·         Take input from keyboard (resume)

GENERIC (Core JAVA)

·         It is used to store similar type of data
·         Whenever all elements of collection type are same then we go for generic
·         If a collection is declared with generic then we cannot store element apart from generic type
·         If collection is defined with generic then no need to downcast explicitly each element. Java does automatic down casting (implicitly) for that
ArrayList<string> l1 = new ArrayList<string>();
In this only string object can be stored.
Where l1 is an array list of string type then we can store only string object inside this array list

NOTE : Generic should be used when ever similar types of objects are stored in collection

Collection API (Core JAVA)

·         It is group of java classes and interfaces. It is used for storing and managing dissimilar data
LIST
·         List is a type of collection
·         List elements are stored in index based
·         List is a auto index collection
·         List allows duplicate values
·         List allows null values
·         The elements of list are referred through index number. For e.g. Question paper
QUEUE
·         Queue is a type of collection
·         In queue elements are stored without index (it is not a index collection)
·         Queue is implemented in FIFO (First IN First Out)manner
·         Queue allows duplicate elements
·         Queue doesn’t allow null values. For e.g. Ticket Queue
SET
·         Set is type of collection
·         Set is not a index collection
·         Set doesn’t allows duplicate elements
·         Set allows null values. For e.g. Currency Collection
MAP
·         MAP elements are stored in form of key and value pair, where key should be unique  and value can be duplicated
·         Value can be referred using key. For e.g. Key and Lock
·         Each MAP type classes provide 2 methods
(1)    Keyset
(2)    Values
·         Keyset method returns set of keys whereas values method returns the collection of values
PRIORITY QUEUE
·         In Priority queue whenever an element is added, it compares and sort with the existing elements
·         Priority queue is auto sorted queue
·         Whenever we display element in priority queue, the elements are displayed in random manner
·         Poll is method of priority queue, which will remove topmost element of queue
·         Whenever Poll method is executed, the queue size gets reduced by 1
·         Peak method of priority queue returns the topmost element of the queue
COLLECTIONS
·         “Collections“class contains several methods which can be operated on any type f collection for e.g. sort is a static method which is used to sort the element of collection. This method stores the sorted element back in the same collection type
ITERATOR
·         Collection API provides iterator to iterate each element of any collection type.
·         Iterator is an inter face which provides three important methods:-
1.       next():- this method is used to navigate to the next element and return the value of next element. if there is no next element the method returns an exception “no such element exception”
2.       hashNext():- it is used to check weather next element exist or not. If element exist return true else false
3.       remove(): it removes the element from the collection. Once element is removed, the previous element should point to the element which exist after the removed element
·         Using iterator object we can iterates only once the element of collection.
·         This iterator can be used to iterate list, queue or set type of collection.
·          Java collection also provides list iterator which can be used only list type of collection. this iterator provides two additional methods :
1.       previous(): it moves iterator to the previous element and return element value.
2.       hashprevious(): it returns true or false based on the existence of previous element.
NOTE:- both iterator and list iterator cannot be used for map

·         Whenever element of collection are read from any collection type ,it will be in object form(object type),it should be downcasted to respective class to access sub class members

Exception Handling (Core JAVA)

·         Whenever jvm executes program, it executes the statement line by line. If one of the statements generates abnormal condition then jvm terminates the execution.
·         When jvm finds abnormal condition and statement it looks for throwable type class which defines the condition. Once it finds a class create an instance of throwable and throws the object using “throws” keyword
·         Program has to catch the object in order to continue the execution. Otherwise the program will be terminated
·         To catch the exception object, java provides “try and catch” block, try and catch should be written together
·         Try block should contains the statements which may generates an exception and catch block should have argument of type throwable
·         Whenever an exception occur in try block, the corresponding catch block gets executed and program will continue the execution and try and catch block
·         If there is no exceptions in try block, then catch block will not be executed
·         A try block can have multiple catch block, in such cases catch block matching will happen in sequential order but only one catch block will be executed
·         If we have to develop single catch block which can handle any type of error and exception then the catch block argument should be throwable type
·         The exception is categorised into 2 category
(1)    Checked Exception
(2)    Unchecked Exception
·         Any exception type which a compiler can identify at the compilation time, these exceptions are known as Checked Exception. For e.g. Exception classes which are subclass to the exception type are comes under Checked Exception like I/O, SQL, File not found exception etc
·         Any exception which compiler fails to identify at the time of compilation are known as Unchecked Exception. For e.g. all classes which are subclass to error and subclass to runtime exception are categorised as Unchecked Exception
·         Unreachable Code: code which is not used. For e.g. when we use throwable catch(Throwable exp), it will not allow other catch to execute

·         Finally Block: Finally block gets executed irrespective of exception occurrence. Whenever we want to execute mandatory statement then we go for finally block. For e.g. opening the connection and closing the connection (closing stream of file) etc

Wrapper Classes: (Core JAVA)

·         Java provides wrapper class to convert primitive type to an object type
·         For every primitive type in java, we have wrapper classes
·         All wrapper classes are available in package “java.lang”. The numeric related wrapper classes are inherited from number class which is abstract class. All the abstract classes are final classes
·         In each wrapper class toString() method, equals() method and hashcode() method are overrided
·         Converting primitive type to object type is known as Boxing operation. This boxing operation is done by using wrapper classes
·         Converting a boxed object back to primitive type is known as unboxing operation
·         If the compiler does the boxing by its own then it will known as Autoboxing
·         Each wrapper class provides method to get primitive vale for e.g., int value ,float value etc

·         In an array of object type whenever any element is added all the data are casted to object type

ARRAY: (Core JAVA)

{              ArrayType [] arrayname; or
ArrayType arrayname[];
Arrayname= new ArayType[size];            } or
ArrayType[] arrayname= new ArrayType[size];
·         Array is a collection of similar type which has fixed size and it cannot store dissimilar type
·         Array can be initialize in 2 ways
(1)    By using dimensions
(2)    By using initialize
·         Syntax for initializing array using dimensions
ArrayType[] arrayname= new ArrayType[size];

·         Syntax for initializing array using initialize
ArrayType[] arrayname= { , , , };
·         Using array initialize, the elements are separated by ‘,’
·         We can refer each element of an array using index
·         The element of an array can be sorted by using sort method
·         Sort method is a public static method of array’s class
·         Array’s class belongs to a package java.util
·         sort method is overloaded method of arrays class. This method sorts the element of an array and stores the sorted element in the same array.
·         Array can be of 2 types
(1)    Primitive array- Boolean, int etc(PRIMITIVE DATATYPES)
(2)    Derived array- objects etc

·         If array of class type is created, then in that array we can store any type of object which is subclass to array’s class

Scanner Class (Core JAVA)

Scanner Class
·         Scanner src = new Scanner();                     - WRONG
·         Scanner src = new Scanner(System. in);                - RIGHT
int id= src.nextInt();
String UN =src.next();
Double salary = src.nextDouble();
·         Scanner class is used to read the input from the keyboard and this class is available in “java.util” package, hence we have to import this class from this package
·         Scanner class doesn’t have default constructor. It contains overloaded constructor. While creating an instance of Scanner class, we have to call a constructor which takes the input stream type as an argument
·         Inside the scanner class several non-static methods are available to read input from the keyboard. For example, next() method is used to read String data from the keyboard and this method returns String type value

·         nextInt():  It is used to read integer from keyboard and this method returns integer type and etc.

Palindrome Program: (Core JAVA)

public class Palindrome
{

       public static void main(String[] args)
       {            
System.out.println("Enter Word");
              Scanner src=new Scanner(System.in);
              String original=src.next();
              String reverse="";
              int length=original.length();
              for(int i=length-1;i>=0;i--)
              {
                     reverse=reverse+original.charAt(i);
              }
              if(reverse.equals(original))
              {
                     System.out.println("Pallandrome");
              }
              else
              {
                     System.out.println("Not pallandrome");
              }
}

}

String Class (Core JAVA)

·         String class is used to handle the String value in the program. This class is available in “package java.lang.String”
·         String class is final class, it can be inherited to any subclass
·         We can create object of String class in 2 ways:
(1)    Using new operands
(2)    Using double quotes
NOTE: From jdk 1.5 onwards, 2 additional classes are introduced to create String object.
(1) String Builder
(2) String Buffer
Both cases are Mutable Classes means object value can be changed. Using these 2 classes we can create String object by using new operand only.
·         For these 2 classes reverse method is also available.
NOTE: String class designs Immutable Classes. Once string object is created its value cannot be changed. If the object values are getting changed then it will create new object instead of pointing existing one.
·         When the objects are created using new operands, then new object is created in the memory even though object is already existing “It allows Duplicate objects
·         When String object is created by enclosing in double quotes, it creates new object only if the object is unique. Otherwise it reuses the existing object
·         The uniqueness of an object is identified based on the String value
·         String class is Immutable class. Once String object is created, its value cannot be changed
·         If the object values are changed, the it creates new object instead of changing existing one

·         In String class toString() is overrided to return the value of object

NOTE: From jdk 1.5 onwards, 2 additional classes are introduced to create String object
(1)    String Builder
(2)    String Buffer
Both classes are mutable classes, means that the object value can be changed. In these classes we can create String object by using new operand only

NOTE: For these 2 classes 1 additional method is also available by name reverse

Hashcode() (Core JAVA)

·         hashcode() of an object class is a public method of return type integer. Whenever this method is invoked the method returns hashcode value which is an integer value based on the object address.

·         A class can override a hashcode() in such cases original implementation will be lost and we will get overrided implementation.

Equals Method (Core JAVA)

·         Syntax
Public boolean equals (object obj)
{
------
------
Return value;
}
·         Equals method in object class is public method which returns Boolean type value. This method takes an argument of type object
·         Whenever this method is invoked on an instance, method compares the current object with pasted object based in the address of an object and return either true or false

·         If we compare object based on object field, then equals method has to be override. While overriding the current object field can be referred by using “this” keyword. The received object member should be first downcasted then its member should be referred.

Object Class - System Class

·         System is a class available in java.lang package
·         Java.lang package is imported to every java class by default, hence no need to import explicitly
·         System class contains 2 Static reference variable, which are:
(1)    in-is type of input stream
(2)    out-is type of print stream
·         print stream object contains several methods used to print characters on the standard output device or monitor
·         input stream contains members or methods used to read from standard input device
·         Object class is super most class in java where every class has to inherit members of object class. This class is available in package java.lang
·         The toString() of object class is public method of return type String, whenever this method is invoked, the method returns String representation of object
·         The String representation of an object will be displayed in following format:
“fullyqualifiedclassname@hexadecimaladdress”
Whenever reference variable is printed, the toString() is implicitly invoked. Hence, the return value of toString() is displayed

·         A class can override toString() of object class. I such class the object will be override in the implementation class and the original implementation will be lost