Skip to main content

Posts

Thread Life Cycle in Java

 Life Cycle of Thread in Java Programming In java, thread is a class. We know that class consists of a number of methods, variables, constructors etc. Here also the thread class consists of a number of methods. In order to access those methods first create an object for thread class. Thread life cycle has five states. From the creation up to the terminating the thread will move in five different states; these are called thread life cycles . Whenever a thread is created it is moved to a new state.In other words, the state where a thread is created and it is not running is called the newborn state . Whenever a thread is ready to execute the task it moves to runnable state, but is not executed currently.when the start() method is called on the new born thread, it will be in runnable state . After the runnable state, if the thread gets CPU access, it moves into the running state . If the CPU, completes without interrupting the process, it will move to a stop state. If any I/O inter...

Multithreading in java

Explanation of multithreading in java programming What is multithreading in java in simple words? Multithreading Multithreading is a powerful programming tool that makes it possible to achieve concurrent execution of multiple units of a program.In another word it is Concurrent execution of several parts of the same program at same time(This will improve CPU utilization). Advantages of multithreading in java Multi threading support for java includes thread creations,thread prioritizing,thread scheduling,resource locking (thread synchronization)and establishing inter thread communication. What is thread and how to create a thread in java? A thread can be defined as a process in execution within a program. A thread is a lightweight process and it is an individual part of a running program. Group of threads is called a program.All these threads will be working concurrently. There are two ways to create a thread. 1 By extending the thread class 2 By implementing the runnable interface

How does try catch finally work in java

 program for 'try catch finally' in java programming Java program using try catch finally block  public class Divide2  { public static void main(String args[]) { System.out.println("\nPROGRAM EXECUTION START HERE\n"); int NUM1,NUM2,RESULT; try { NUM1=Integer.parseInt(args[0]); NUM2=Integer.parseInt(args[1]); RESULT=NUM1/NUM2; System.out.println(NUM1+"/"+NUM2+"="+RESULT); } catch(Exception e) { System.out.println(e); } finally { System.out.println("\nTHE FINALLY BLOCK ALWAYS EXICUTED\n");  } System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n"); } } observe the program and its output carefully

Explanation of 'try catch finally' block in java programming

 try-catch-finally block in java programming try block To handle a run time error and monitor the results,we simply enclose the code inside a try block.If an exception occurs within the try block,it is handled by the appropriate exception handler ( catch block) associated with the try block.A try block should have one (or more) catch blocks  or one finally block or both.Exception handlers are put in association with a try block by providing one or more catch block directly after the try block Syntax of try catch and finally block try { //java statements(risky code)      }      catch(Exception e)      {      //handled code      }     finally     {          //cleanup code          } catch block A catch block is specified by the keyword catch followed by a single argument within parentheses().A catch block is a group ...

Objective type questions and answers part-2

 Exception Handling Questions and Answers in Java Programming  Objective Type Q&A(part 2) 1:  An exception is an abnormal condition that arises in a block of statements during program execution 2: In java,exceptions are the sub-classes of the built-in class throwable 3: In java an exception is an object that describes an abnormal condition that has occurred in a piece of code. 4: Exception class is sub-classed to create a user-defined exception. 5:Exceptions of the RuntimeException type are automatically defined for java programs. 6: When using multiple catch statements,if the exception sub-classes are not placed before their super-classes unreachable code error occurs. 7: A method specifies the exceptions that are not handled by it using the throws keyword 8: throw keyword is used to throw an exception explicitly 9: OutOfMemory is an example off Error type of exception

Java program and output-Exception handling

E xception handling exercises in java program with output A common exception:divided by zero program1 public class Divide { public static void main(String args[]) { System.out.println("\nPROGRAM EXECUTION START HERE\n"); int NUM1,NUM2,RESULT; NUM1=Integer.parseInt(args[0]); NUM2=Integer.parseInt(args[1]); RESULT=NUM1/NUM2; System.out.println(NUM1+"/"+NUM2+"="+RESULT); System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n"); } } Using try catch block: program2 public class Divide1 { public static void main(String args[]) { System.out.println("\nPROGRAM EXECUTION START HERE\n"); int NUM1,NUM2,RESULT; try { NUM1=Integer.parseInt(args[0]); NUM2=Integer.parseInt(args[1]); RESULT=NUM1/NUM2; System.out.println(NUM1+"/"+NUM2+"="+RESULT); } catch(Exception e) { System.out.println(e); } System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n"); } } observe the both programs and ...

Exception Handling in java programming

 Exception Handling and types of exception handling in java programming Exception handling in java programming Exception handling means avoiding abnormal termination of the program.Different types of error occur in our program,such as user errors,logic errors or system errors.When an exception occurs, it makes further execution of the program impossible.Java offers an excellent exception handling mechanism which is easy to understand and implement.Whenever an error occurs during the execution of a program,it throws an exception.If the exception is not handled properly, the program execution  is terminated. What is an exception handling mechanism in java? Exception handling is a mechanism that enables programs to detect and handle errors before they occur rather than allowing them to occur and suffering the consequences. Handling Exception In Java  Exception handling in java is accomplished by using  five keywords. try,catch,throw,throws and finally . Java exception ...

Acronyms to remember in Java programming

Acronyms to remember in Java programming Introduction to java programming-very important theory notes

Java main method explanation

Explantion and Importance of main() method in java programming Explanation of main() method in java programming The entry point of executing a Java program is the main method. We know that Java programs work in oops concepts. Java programs are treated as objects and classes. If there is more than one class in a program,The program should be named using the class containing the main() method,because main() method is the entry point of Java program. java main method explanation Syntax of main method public static void main ( String args[] ){   } Importance of main function in java programming Here the public is an access modifier.There are different modifiers available in java If a function is declared public, the scope of the function is the entire program. Static keyword is the keyword where we can directly access the method without object.If the method is static , then such method can be accessed directly.Here void is a return type.Every method has a return type.When writ...

Difference Between Overloading and Overriding in Java programming

   overloading vs overriding in java What is polymorephism? Implementing same thing in different way is called polymorphism It divide into two category  1 compile time→ this can be  achieved by implementing method overloading  2 Run time—> this can be  achieved by  implementing method overriding Difference between overloading and overriding Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters

Runtime Polymorphism-Method Overriding in javaprogramming

 Runtime polymorphism will be implimented by the help of inheritance-Method Overriding method overriding Declaring a method in the subclass which already exists there in the parent class is known as method overriding.To override a method in a class, another method is created in its subclass that has the same signature(name,return type and parameter list) as the original method. Remember the following  point If the method is static, that method cannot be overridden If the method is declared in 'final' keyword that method cannot be overridden Importance of ‘ super’ keyword The super keyword is used to access immediate parent class(base class) properties If methods, variables and constructor are given the same name in the base class and the derived class, the base class property can be accessed using the super keyword.The super keyword should be used in the derived class only.Cannot be used in the main method Method overriding-sample program class Parent { void display()...

polymorphism and Overloading in java programming

 Overloading Concepts in java-polymorphism and Overloading in java programming  Overloading what is method overloading in java? The process of defining methods with the same name but different functionalities is called method overloading. In other words, with the same name and different parameters. This is not supported in c language. For example,an overloaded draw() method can be used to draw anything,such as a circle,square,triangle and so on. how many types of overloading in java? Types of overloading In java there are three types of overloading constructor overloading ---> Default constructor(without any arguments)Parameterized constructor(with some arguments) method overloading ---> Method Without any argument and method with arguments operator overloading ---> The + symbol  can be implemented in two ways one is  addition operator and another one is concatenation operator,same symbol used to perform two purposes.It is called operator overloading P...