Tutorials Point, posted 22h 57m ago

Java Cloning

public class Tester {
   public static void main(String[] args) throws CloneNotSupportedException {
      //Scenario 1: Shallow Copy
      B b1 = new B();
      A a1 = new A();
      a1.a = 1;
      a1.b = 2;

      b1.aObject = a1;
      b1.a = 1;
      //Print a1 object
      printObject(b1.aObject, "b1.a", b1.a);

      //clone operator copies the reference 
      B b2 = b1.clone();
      //b1.aObject.a and b2.aObject.a are now pointing to same object
      //modify b2.aObject.a and changes will reflect in b1.aObject.a
      b2.aObject.a = 3;
      printObject(b1.aObject, "b1.a", b1.a);
      printObject(b2.aObject, "b2.a", b2.a);

      //Scenario 2: Using Deep Copy
      C c1 = new C();
      A a3 = new A();
      a3.a = 1;
      a3.b = 2;

      c1.aObject = a3;
      c1.a = 1;
      //Print a1 object
      printObject(c1.aObject, "c1.a", c1.a);

      C c2 = c1.clone();
      //c1.aObject.a and c2.aObject.a are now pointing to different object
      //modify c2.aObject.a and changes will not reflect in c1.aObject.a
      c2.aObject.a = 3;
      printObject(c1.aObject, "c1.a", c1.a);
      printObject(c2.aObject, "c2.a", c2.a); 
   }

   private static void printObject(A a, String name, int value){
      System.out.println(name + ": [" + a.a + ", " + a.b + "]" + ", " + value);
   }
}

class A {
   public int a;
   public int b;
}

class B implements Cloneable {
   public int a;
   public A aObject;
   public B clone() throws CloneNotSupportedException {
      B b = (B)super.clone();
      return b;
   }
}

class C implements Cloneable {
   public int a;
   public A aObject;
   public C clone() throws CloneNotSupportedException {
      C c = (C)super.clone();
      c.aObject = new A(); 
      c.aObject.a = this.aObject.a;
      c.aObject.b = this.aObject.b;
      return c;
   }
}

Tutorials Point, posted 23h 6m ago

Java clone() method

public class Tester {
   public static void main(String[] args) throws CloneNotSupportedException {
      //Scenario 1: Using assignment operator to copy objects
      A a1 = new A();
      a1.a = 1;
      a1.b = 2;
      //Print a1 object
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");
      
      //assignment operator copies the reference
      A a2 = a1;
      //a2 and a1 are now pointing to same object
      //modify a2 and changes will reflect in a1
      a2.a = 3;
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");
      System.out.println("a2: [" + a2.a + ", " + a2.b + "]");

      //Scenario 2: Using cloning, we can prevent the above problem
      B b1 = new B();
      b1.a = 1;
      b1.b = 2;

      //Print b1 object
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");

      //cloning method copies the object 
      B b2 = b1.clone();
      //b2 and b1 are now pointing to different object
      //modify b2 and changes will not reflect in b1
      b2.a = 3;
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");
      System.out.println("b2: [" + b2.a + ", " + b2.b + "]");
   }
}
class A {
   public int a;
   public int b;
}

class B implements Cloneable { 
   public int a;
   public int b;

   public B clone() throws CloneNotSupportedException {
      B b = (B)super.clone();
      return b;
   }
}

Tutorials Point, posted 23h 30m ago

Java Unchecked exceptions

public class Unchecked_Demo {

   public static void main(String args[]) {
      int num[] = {1, 2, 3, 4};
      System.out.println(num[5]);
   }
}

Tutorials Point, posted 23h 37m ago

Java Checked exceptions

import java.io.File;
import java.io.FileReader;

public class FilenotFound_Demo {

   public static void main(String args[]) {		
      File file = new File("E://file.txt");
      FileReader fr = new FileReader(file); 
   }
}

Tutorials Point, posted 1d 0h ago

Java Chained exception

public class Tester {
   public static void main(String[] args) {
      try {
         test();
      }catch(ApplicationException e){ 
         System.out.println(e.getMessage());
      }
   } 

   public static void test() throws ApplicationException{
      try{
         int a = 0;
         int b = 1;
         System.out.println(b/a);
      }catch(Exception e){
         throw new ApplicationException(e);
      }
   }
}

class ApplicationException extends Exception {
   public ApplicationException(Exception e) {   
      super(e);
   }
}

Tutorials Point, posted 1d 0h ago

Java override private methods

class A {
   private void display(){
      System.out.println("A.display");
   }

   public void callDisplay(){
      System.out.println("A.callDisplay");
      display();
   }
}

class B extends A {
   private void display(){
      System.out.println("B.display");
   }

   public void callDisplay(){
      System.out.println("B.callDisplay");
      display();
   }   
}

public class Tester {
   public static void main(String[] args) {
      A a = new B();
      a.callDisplay();
  
      B b = new B();
      b.callDisplay();

      A a1 = new A();
      a1.callDisplay();
   } 
}

Tutorials Point, posted 1d 0h ago

Java Calling a method using null

public class Tester {
   public static void display(){
      System.out.println("display");
   }
   private void print(){
      System.out.println("print");
   }

   public static void main(String[] args) {
      //Scenario 1: 
      //Calling a method on null reference
      //causes NullPointerException
      try{
         Tester test = null; 
         test.print();
      }catch(Exception e){
         System.out.println(e.getMessage());
      }

      //Scenario 2:
      //Static method can be invoked
      //on a null object by using the casting expression
      ((Tester)null).display();
   }
}

Tutorials Point, posted 1d 0h ago

Java Callback using Interfaces

//Step 1: Create an interface for callback method
interface ClickEventHandler {
   public void handleClick();
}

//Step 2: Create a callback handler 
//implementing the above interface
class ClickHandler implements ClickEventHandler {
   public void handleClick() {
      System.out.println("Clicked");
   } 
}

//Step 3: Create event generator class
class Button {
   public void onClick(ClickEventHandler clickHandler){
      clickHandler.handleClick();
   }
}

public class Tester {
   public static void main(String[] args) {
      Button button = new Button();
      ClickHandler clickHandler = new ClickHandler();
      //pass the clickHandler to do the default operation
      button.onClick(clickHandler);

      Button button1 = new Button();
      //pass the interface to implement own operation
      button1.onClick(new ClickEventHandler() {
         @Override
         public void handleClick() {
            System.out.println("Button Clicked");
         }
      });
   }
}

Tutorials Point, posted 1d 0h ago

Java Callable and Future

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestThread {

   public static void main(final String[] arguments) throws InterruptedException,
      ExecutionException {

      ExecutorService executor = Executors.newSingleThreadExecutor();

      System.out.println("Factorial Service called for 10!");
      Future<Long> result10 = executor.submit(new FactorialService(10));

      System.out.println("Factorial Service called for 20!");
      Future<Long> result20 = executor.submit(new FactorialService(20));

      Long factorial10 = result10.get();
      System.out.println("10! = " + factorial10);

      Long factorial20 = result20.get();
      System.out.println("20! = " + factorial20);

      executor.shutdown();
   }  

   static class FactorialService implements Callable<Long> {
      private int number;

      public FactorialService(int number) {
         this.number = number;
      }

      @Override
      public Long call() throws Exception {
         return factorial();
      }

      private Long factorial() throws InterruptedException {
         long result = 1; 
         
         while (number != 0) { 
            result = number * result; 
            number--; 
            Thread.sleep(100); 
         }
         return result;	
      }
   }
}

Tutorials Point, posted 1d 0h ago

Java references

public class Tester {
   public static void main(String[] args) {
      Student student = new Student();        student.setName("Mahesh");
      System.out.println(student.getName());     }
}

class Student{
   String name;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

1 2 3 4 5 6 7 ... 1574 Next
Advertisements
Loading...