Tuesday, January 14, 2020

Java others


Difference between an Integer and int in Java with Examples

In Java, int is a primitive data type while Integer is a Wrapper class.
  • int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.
  • Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
  • Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
How Can we can call the method of Base class in derived class?
through Super constructor calling - this.driver

What is difference between throws and throw ?-

Throws -
1)It is used with method signature .

Public void readFile() throws IOException
{
}

2) It is implicit exception where one do have to use the try catch function.

3)It is used to declare and exception.
4). If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.
5)throws is used to declare and exception
6)throws can be used to handle multiple exception.

Throw -
1)It is used inside the method .

package testpack;

import org.testng.annotations.Test;

public class Throwexample {
    int Age;
  
  
    public void validateAdult(int Age)

    {
try {
    if(Age<18)

    {  
    throw  new ArithmeticException ("Error not an Adult");
    }
else
    {
System.out.println("Welcome to adulthood");
    }
}
  
catch (ArithmeticException e)
    {
e.printStackTrace();
    }
  
    }
  
    @Test
    public void throwmethod()
    {
    validateAdult(12);
        validateAdult(22);
      
    }

}
2) It is explict exception where one do have to use the try catch function .(for good practice it should be used with in the try block)


3). If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.

4)throws can be used to handle Single exception only.


Function of e.printStackTrace().

The printStackTrace() method of Java.lang.Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.
The first line of output shows the same string which was returned by the toString() method for this object means Exception class name and later lines represent data previously recorded by the method fillInStackTrace().






----

No comments:

Post a Comment