Skip to main content

Posts

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

java programming questions and answers for beginners-part1

  Java coding test questions and answers  Objective Type Q&A(part1) Array indexes start with?--> A: zero Which keyword is used to create a class in Java?--> A: class To declare an array in Java, define the variable type with? --> A: [ ] Which keyword is used to import a package from the Java API library?--> A: import Why include comments in Java programs?--> A: clarity of code Which keyword is used to return a value inside a method? --> A: return Which statement is used to stop a loop?--> A: break When does method overloading is determined?--> A: At compile time Which concept of Java is achieved by combining methods and attributes into a class?--> A: Encapsulation compile time polymorphism is one type of polymorphism in Java programming. Method overriding is a combination of inheritance and polymorphism? --> A: true The value of a string variable can be surrounded by single quotes --> A: false In Java, it is possible to inherit attribu...

Why Multiple and Hybrid Inheritances-Java Does not Support

    Why Multiple&Hybrid Inheritances-Java Doesn’t Support-How to solve it Multiple and Hybrid inheritance There are other two inheritance concepts multiple and hybrid inheritance.These are not directly supported in java.It is not possible for a child class to inherit two parent class properties through inheritance.In order to achieve this two techniques we have to implement interfaces concepts.This can be achieved through the interface concept,  called multiple inheritance.The combination of any two inheritance is called hybrid inheritance. multiple inheritance represantation Interface Interfaces,like abstract classes and methods,have a lot of functionality.Two keywords are used interface and implements .one of the differences between class and interface is that an interface cannot be instantiated,the keyword new can create only an instance of class. sample program-Interface Example program interface A { void show1(); } interface B { void show2(); } cl...