MainMenu

Home Java Overview Maven Tutorials

Tuesday 26 December 2017

Difference between Final, Finally and Finalize in Java



Hello friends,

In this article we will learn what is the difference between Final, Finallyand Finalize with suitable examples.

In java, where we want restriction, we use Final Keyword.
We can use Final keyword with class, Method and variables.
Final Class can't be Inherited.
Final method can't be overridden.
Final variable can't be changed.

Finally is a block and the code written inside the Finally block will be executed whether exception is handled or not.

Finalize is a Method and it is used to perform clean up processing just before object is garbage collected.

Example of Final in java


public class Finalandfinally
{
public static void main(String args[])
{
final int x = 10;
int y = x+10;
x = y; //here you will get the error "Remove final modifier of x"
System.out.println("New value of x is :" + x);
Test2 obj = new Test2();
obj.sum();
}
}
final class Test1
{
final public void sum()
{
System.out.println("Hello i am inside method sum");
}
}
class Test2 extends Test1 //Remove final modifier of Test1
{
public void sum() //"Remove final Modifier of Test1.sum(....)"
{
System.out.println("Hello i am inside method sub");
}
}


Finally block

The finally block always executes when the try block exits.
This ensure that the finally block is executed even if an expected exception occurs.

public class Finalandfinally
{
public static void main(String args[])
{
try
{
System.out.println("Enter your no");
Scanner obj = new Scanner(System.in);
int x = obj.nextInt();
System.out.println("Enter your second number");
int y = obj.nextInt();
int z = x/y;
System.out.println("Your result is :" +z);
}
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("I will be executed whether exception occur or not");
}
}
}

finalize Method

1. Finalize is called before Garbage collector reclaim the object.
2.Its last chance for any object to perform cleanup activity i.e. releasing any system resources held , closing connection if open etc.
3.finalize() method is defined in java.lang.Object class
4.It's the responsibility of developer to call superclass finalize method when we override finalize method.
5.There is no gurantee that finalize method is called because finalize() method is called just before the garbage collection process happens and we are not sure when garbage collection process happens.
6.Exception propagated by finalize() method are not propogated and are ignore by garbage collector.
7.we can force jvm to call finalize method by calling
System.run.Finalization()
Runtime.getRuntime().runFinalization()
8.finalize() method gets called only once by garbage collector.

import java.util.Scanner;
import org.apache.poi.util.SystemOutLogger;
class A
{
int i = 50;
@Override
protected void finalize() throws Throwable
{
System.out.println("From Finalize Method");
}
}
public class Finalandfinally
{
public static void main(String[] args)
{
//Creating two instances of class A
A a1 = new A();
A a2 = new A();
//Assigning a2 to a1
a1 = a2; //Now both a1 and a2 will be pointing to same object
//An object earlier referred by a1 will become abandoned
//Making finalize() method to execute forcefully
Runtime.getRuntime().runFinalization();
System.out.println("done");
}
}

Friday 22 December 2017

Sort the array without using Java functions


Hello friends,

In this article we will learn how to sort the Array with java Collection and Without using java function



So first let's discuss by using java Method :
Sort the array by using java function.

public class Arraysort
{
public static void main(String args[])
{
int[] ary = {25, 32, 1, 6, 11, 9, 17, 100, 95};
Arrays.sort(ary);
System.out.println("Sorted array is : %s", Arrays.toString(ary));
}
}

Note : By default array will be sorted in ascending order.

Sort the array in descending order :

public class Arraysort
{
public static void main(String args[])
{
int[] ary = {25, 32, 1, 6, 11, 9, 17, 100, 95};
Arrays.sort(ary, Collections.reverseOrder());
System.out.println("Sorted array is : %s ", Arrays.toString(ary) );
}
}

Sort the String array in Ascending & descending array

public class Arraysort
{
public static void main(String args[])
{
int[] ary = {"way2testing.com", "datastop.in", "onlineifsccodebank.com"};
System.out.println("Sorted array is : %s \n\n", Arrays.toString(ary) );
}
}

Sort the string array in decending order
public class Arraysort
{
public static void main(String args[])
{
int[] ary = {"way2testing.com", "datastop.in", "onlineifsccodebank.com"};
Arrays.sort(ary, Collections.reverseOrder());
System.out.println("Sorted array in decending order is : %s ", Arrays.toString(ary) );
}
}

Sort the String without using Java API

Sort the String without using Java API
public class SortString
{
public static void main(String args[])
{
char temp;
String modifiedString = "";
Scanner userstring = new Scanner(System.in);
System.out.println("Enter your String");
String str = userstring.next();
}
char[] chrary = str.toCharArray();
for(int i = 0; i<chrary.length; i++)
{
for(int j = 0; j<chrary.length; j++)
{
if(chrary[i]<chrary[j])
{
temp = chrary[i];
chrary[i] = chrary[j];
chrary[j] = temp;
}
}
}
for(int k = 0; k<chrary.length; k++)
{
modifiedString = modifiedString + chrary[k];
}
System.out.println("Your sorted string is : " +modifiedString);
}



How to sort an array without using sort function in java.

public class sortme
{
public static void main(String args[])
{
int[] ary = {25, 3, 29, 9, 15, 40, 90, 75};
for(int i =0; i<ary.length; i++)
{
for(int j =0; j<ary.length; j++)
{
if(ary[i]<ary[j])
{
temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
for(int k = 0; k<ary.length; k++)
{
System.out.println("Your sorted array is : " +ary[k]);
}
}
}

reverse the string and number in java without using functions


For Video :- Click Here



Hello friends,

In this article we will learn Reverse a String with and without using Java Functions :-

import java.util.Scanner;
public class Stringplay
{
public static void main(String args[])
{
String rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter your first string");
String ys = sc.nextLine();
int length = ys.length();
for(int i =length-1; i>=0; i--)
{
rev = rev + ys.charAt(i);
}
System.out.println("Your String is: " +ys);
System.out.println("reverse String is: " +rev);
StringBuffer sb = new StringBuffer(ys);
rev = sb.reverse().toString();
System.out.println("simple reverse String is: " +rev);
}
}



And here is your output:

Enter your first string
chandan singh
Your String is: chandan singh
reverse String is: hgnis nadnahc
simple reverse String is: hgnis nadnahc

Reverse a number without using Java Functions :-

public class Jtestnew
{
@Test
public void g()
{
int x = 123%10;
System.out.println(x);
int y = 123/10;
System.out.println(y);
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number");
int num = scn.nextInt();
int rnum = 0;
while(num!=0)
{
rnum = rnum*10 + num%10;
num = num/10;
System.out.println("Reversed number is: " +rnum);
}
}
}

Sunday 10 December 2017

How to Create WebDriver Instance for multiple class


Hello friends,

In this article we will learn how to create WebDriver Instance multiple classes:

So, we will create a WebDriver instance in one class & call it in multiple class
we have achieve this by 2 ways
1).By using extends
2).By using Singleton class

To know about singleton class Click Here

For Video :- Click Here



Method :1By extends Keyword

Step :1Create a class & initialize your driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MainDriver
{
public static WebDriver driver;
public static void createInstance()
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}


Step :2Create an another class & extend above driver class

public class Testcase1 extends MainDriver
{
public static void url()
{
MainDriver.createInstance();
driver.navigate().to("http://www.way2testing.com");
}
}


Step :3Create an another class & extend above driver class

public class TestCase2 extends MainDriver
{
public static void getTitle()
{
System.out.println(driver.getTitle());
}
}


Step :4Create an another class & extend above driver class

import org.testng.annotations.Test;

public class Excution
{
@Test(priority = 0)
public void getUrl()
{
Testcase1.url();
}
@Test(priority = 1)
public void getTitle()
{
TestCase2.getTitle();
}
}


If you will run this Exeution class , you can see that with one WebDriver Instance(driver), you are able to perform your all task.

Method :2By Singleton Class

Step :1Create a Singleton class & initialize your driver

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SingletonDriver
{
public static WebDriver driver = null;
public static void Initiallize()
{
if(driver == null)
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
public static void close()
{
driver.close();
}
public static void quit()
{
driver.quit();
}
}



Step :2Create a class for your test cases & execute singleclass method

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Testcases
{
@BeforeTest
public void initializeDriver()
{
SingletonDriver.Initiallize();
}
@Test(priority = 0)
public void openurl()
{
SingletonDriver.driver.navigate().to("http://www.way2testing.com");
}
@Test(priority = 1)
public void getTitle()
{
System.out.println(SingletonDriver.driver.getTitle());
}
}



So, by using above methods, you can create a WebDriver Instanse(driver) and can use it in your multiple classes.
Thanks :)