Posts

Exception Handling

Image
Agenda Introduction An unexpected unwanted event that disturbs normal flow of the program is called Exception. It is highly recommended to handle exceptions and the main objective of Exception handling is graceful termination of the program. Exception handling doesn't mean repairing an exception. We have to provide alternative way to continue rest of the program normally. This is the concept of Exception handling. Runtime stack mechanism For every thread JVM will create a runtime stack. Each and every method call performed by the thread will be stored in the corresponding stack. Each entry in the stack is called stack frame or activation record. After completing, every method call the corresponding entry from the stack will be removed. After completing all method calls, the stack will become empty and that empty stack will be destroyed by JVM just before terminating the thread. Default exception handling Exception hierarchy Customized exception handling by using try-catch Control f

Collections in Java

Image
Overview Array An Array is an indexed collection of fixed number of Homogenous data elements. The main advantage of arrays is we can represent multiple values by using single variable so that readability of the code will be improved.  Limitations of Arrays Arrays are fixed in size i.e. once we create an array there is no chance of increasing or decreasing the size based on our requirement. Due to this, to use arrays concept compulsory we should know the size in advance which may not be possible always. Array can hold only homogenous data type elements. Student[] students = new Student[ 1000 ] ; students[ 0 ] = new Student( "Vim Payne" , 18 , 20 , 540 ) ; /* Prompts a compile time error: Incompatible types found: Teacher required: Student */ students[ 1 ] = new Teacher( "Vim Payne" , 18 , "St. Theressa School" , "9A" ) ; We can solve the above problem by using Object type arrays. Object[] students = new Object[ 1000 ] ; students[ 0 ] = new

Flow Control in Java

Image
Overview Flow Control describes the order in which the statements will be executed at runtime. There are three types of Flow Control statements that controls the flow of execution. Selection Statements If-else Statements Switch Statements Iterative Statements while loop do-while loop for loop for-each loop Transfer Statements break continue return try-catch-finally assert Reference

OOPS Concept in Java

Image
Overview Most import aspects of OOPS Data Hiding.  Outside person can't access our internal data directly or our internal data should not go out directly, this OOP feature is nothing but Data Hiding. After validation or authentication outside person can access our internal data. By declaring data member (variable) as private we can achieve Data Hiding. e.g. public class Person { final private String name ; final private int age ; public Person (String name , int age) { this . name = name ; this . age = age ; } public String getName () { // Validation return name ; } public int getAge () { // Validation return age ; } } The main advantage of Data Hiding is security.  e.g.  After providing proper username and password, we can able to access our Gmail inbox information. Even though we are valid customer of the bank, we can able to access our account information but we can't access other's account