Tag Archives: object

Interesting Questions


Why map is not a collection?

collection acts upon values. map acts on key value pair

Why the finalize method is protected?

Finalize method is for the JVM to execute before GC.imagine if finalize is public then any object can call directly which is not what we wanted. If you have some open connection you can write cleanup codes in the finalize method in your class and leave the rest of the work to the JVM to call the finalize method.

The finalize method is made protected for the subclass to override it or any finalize operations.

Can a finalize method be called for twice?

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the <code>finalize</code> method causes the finalization of this object to be halted, but is otherwise ignored.

why the clone is protected?

clone is protected because it is something that ought to be overridden so that it is specific to the current class.It’s protected so you don’t use the one in object (it’ll just throw an exception anyway). They want you to override it in a class, then you make it public

How to convert BigDecimal to Integer?


BigDecimal b = new BigDecimal("123");

Integer i = new Integer(String.valueOf(b));

Why Serializable and Cloneable is not having anything?

Serializable and Cloneable are called marker or tag interface because they tag all derived class on purpose. Java has a default clone() method in Object class which should not be applied for all the class. So whenever user creates a class the object is not cloneable. The object is cloneable only when it has the marking of Cloneable interface. if any class implements Cloneable then it is understood by the JVM that the object is eligible for cloning and Object by default provides the cloning method this also provide the class to implement their own clone operation by making it protected

The class is marked Serializable for the JVM to understand that the class can be serialized

 

Advertisements

Producer and Consumer Problem


Self explanatory Producer and Consumer problem

Producer.java

package com.producer;

import java.util.ArrayList;

import java.util.Random;

public class Producer implements Runnable{

//Engine Batch ID reference

ArrayList engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Producer(ArrayList engineList) {

this.engineList = engineList;

}

/*

* Produce Engines

*/

public void produce(){

//Random number for engine number

str = ""+Math.abs(new Random(System.currentTimeMillis()).nextLong());

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//Add the Engine to the batch

engineList.add(str);

//Notify all other threads that the new engine is added to the BATCH

engineList.notifyAll();

}

//Print Engine Number

System.out.println("Produced :: ENGINE CHASIS NO ["+ str + "]");

}

@Override

public void run() {

System.out.println("Production started");

//10 Engine is to be produced

for(int i=0;i<10;i++){

try {

//Produce the engine

this.produce();

//Sleep for 2 seconds for the next operation

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Consumer.java


package com.producer;

import java.util.ArrayList;

public class Consumer extends Thread{

//Engine Batch ID reference

ArrayList<String> engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Consumer(ArrayList<String> engineList) {

this.engineList = engineList;

}

/*

* Consume Engines from the BATCH

*/

public  void consume(int index) throws InterruptedException{

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//if the Batch has no engines

while(engineList.size() == 0){

//then wait for some time until you get a new engine to consume

engineList.wait();

}

//If the engine is deployed to the BATCH

if(engineList.size() >= 0){

//The consume the engine from the respective position

str = engineList.get(index);

}

}

//Print Engine Number that it has been consumed

System.out.println("Consumed :: -------> ENGINE CHASIS NO ["+ str + "]" );

}

@Override

public void run() {

//10 Engine has to be consumed

for(int i=0;i<10;i++){

try {

//Produce the engine

this.consume(i);

//Sleep for 3 seconds for the next operation

Thread.sleep(3000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Main.java


package com.producer;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) throws InterruptedException {

//ArrayList to store the produced engines - BATCH

ArrayList<String> engine = new ArrayList<String>();

//Producer taking the engine BATCH ID Reference

Producer producer = new Producer(engine);

//Consumer taking the reference of the engine BATCH ID Reference

Consumer consumer = new Consumer(engine);

//Threads

Thread prodThread = new Thread(producer);

Thread conThread = new Thread(consumer);

//Starting the production

prodThread.start();

//Start Consumption

conThread.start();

}

}


Testable code


Found a good article on writing testable code

http://www.methodsandtools.com/archive/archive.php?id=103

hashcode() contract with equals()


the hashCode() method in the Object class has some specific contract with the equals() method.. Do you know that?

let’s see why these methods are there in the Object class

hashCode() – the hash code method is used to return the hash code value for the object,  this is an excerpt from the Javadoc.

This method will be used to compute or locate the object in the memory. If the object is moved to a different location during garbage collection, the hash code remains the same to locate the object. This is implemented in Object class by mapping the memory address to an integer.

The below code in Object class maps to a native implementation of the above statement


public native int hashCode();

Common Misconceptions

  • hashCode() is not unique as we suppose.
  • hashCode() is not something which is derived from some value within the object or a random number.
  • hashCode() is unsigned.

some of the implementation of this in JDK can be got from some-interesting-hashcode-values

equals() – This method is used to compare the objects.

This is method has to be used for non-null objects. Some common properties for equals() method are

  • reflexive -x.equals(x) should return true (x==x)
  • symmetryx.equals(y) , y.equals(x) should return true (x==y, y==x)
  • transitivex.equals(y) , y.equals(z) , x.equals(z) should return true (x==y,y==z, x== z)
  • consistency– when y.equals(x) is called multiple times it should provide consistent result
  • nullCheck x.equals(null) should return false
public boolean equals(Object obj) {
 return (this == obj);
 }

Now, what is the contract between these two methods?

  • when equals() between two objects return’s true then hashCode() should always return the same value for both objects.
  • when equals() returns false then hashCode() need not be the same but its desirable.
  • how many times the methods are invoked it should return the same result unless the data used in the methods are modified.

its customary to override hashCode() method whenever equals() method is overridden.

lets have an example of the implementation of the equals() and hashCode() method

 @Override
 public boolean equals(Object obj) {
 // TODO Auto-generated method stub

 if (obj instanceof hashEqualsText) {
 return true;
 }
 return false;
 }
<pre>

the equals method checks if the object is an instance of the another object and returns the output and hashCode method is made to return 1 as the hash code value

 @Override
 public int hashCode() {
 // TODO Auto-generated method stub
 return 1; //some value
 }

find the complete analysis of the hashCode() and equals() method contract

 public class hashEqualsText{

 public static void main(String[] args) throws CloneNotSupportedException {

 hashEqualsText x = new hashEqualsText();
 hashEqualsText y = x;
 hashEqualsText z = new hashEqualsText();

 Object object = new Object();

 String str = "vinod";
 String strSame = "vinod";
 String strDifferent = "vinodAndOthers";

 Boolean bool = true;
 Boolean boolSame = true;
 Boolean boolDifferent = false;

 Integer intt = 123456789;
 Integer inttSame = 123456789;
 Integer inttDifferent = 2222222;

 Long longg = 12121212121212122L;
 Long longgSame = 12121212121212122L;
 Long longgDifferent = 11111111111111111L;

 Character chr = 'a';
 Character chrSame = 'a';
 Character chrDifferent = 'z';

 //hashCode contract
 System.out.println("-----------------hashCode contract-----------------");

 System.out.println("Overrided hashCode of x::"+x.hashCode()+" - Original hashCode of x::" +System.identityHashCode(x));
 System.out.println("Overrided hashCode of y::"+y.hashCode()+" - Original hashCode of y::" +System.identityHashCode(y));
 System.out.println("Overrided hashCode of z::"+z.hashCode()+" - Original hashCode of z::" +System.identityHashCode(z));

 //equals contract
 System.out.println("-----------------equals contract-----------------");

 System.out.println("reflexive - x.equals(x) should return true :: "+x.equals(y));
 System.out.println("symmetric - x and y, x.equals(y)  should return true if and only if y.equals(x) returns true :: "+x.equals(y) + "< -- >"+y.equals(x));
 System.out.println("transitive - x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true :: "+x.equals(y)+"==="+y.equals(z)+"==>"+x.equals(z));
 System.out.println("null check - x.equals(null) should return false always:: "+ x.equals(null));

 //for objects
 System.out.println("-----------------for same Objects-----------------");

 System.out.println("x == y has the value true :: "+(x == y));
 System.out.println("x.equals(y) has the value true :: "+(x.equals(y)));
 System.out.println("x.hashCode() == y.hashCode() has the value true :: "+(x.hashCode() == y.hashCode()));

 //for different objects
 System.out.println("-----------------for different Objects-----------------");

 System.out.println("object hashcode value :: "+(object.hashCode()));
 System.out.println("x == object has the value false :: "+(x == object));
 System.out.println("x.equals(object) has the value false :: "+(x.equals(object)));
 System.out.println("x.hashCode() == object.hashCode() has the value false :: "+(x.hashCode() == object.hashCode()));

 //for same String
 System.out.println("-----------------for same String-----------------");

 System.out.println("str hashcode value :: "+(str.hashCode()));
 System.out.println("strSame hashcode value :: "+(strSame.hashCode()));
 System.out.println("str == strSame has the value true :: "+(str == strSame));
 System.out.println("str.equals(strSame) has the value true :: "+(str.equals(strSame)));
 System.out.println("str.hashCode() == strSame.hashCode() has the value true :: "+(str.hashCode() == strSame.hashCode()));

 //for different String
 System.out.println("-----------------for different String-----------------");

 System.out.println("str hashcode value :: "+(str.hashCode()));
 System.out.println("strDifferent hashcode value :: "+(strDifferent.hashCode()));
 System.out.println("str == strDifferent has the value false :: "+(str == strDifferent));
 System.out.println("str.equals(strDifferent) has the value false :: "+(str.equals(strDifferent)));
 System.out.println("str.hashCode() == strDifferent.hashCode() has the value false :: "+(str.hashCode() == strDifferent.hashCode()));

 //for same Boolean
 System.out.println("-----------------for same Boolean-----------------");

 System.out.println("bool hashcode value :: "+(bool.hashCode()));
 System.out.println("boolSame hashcode value :: "+(boolSame.hashCode()));
 System.out.println("bool == boolSame has the value true :: "+(bool == boolSame));
 System.out.println("bool.equals(boolSame) has the value true :: "+(bool.equals(boolSame)));
 System.out.println("bool.hashCode() == boolSame.hashCode() has the value true :: "+(bool.hashCode() == boolSame.hashCode()));

 //for different Boolean
 System.out.println("-----------------for different Boolean-----------------");

 System.out.println("bool hashcode value :: "+(bool.hashCode()));
 System.out.println("boolDifferent hashcode value :: "+(boolDifferent.hashCode()));
 System.out.println("bool == boolDifferent has the value false :: "+(bool == boolDifferent));
 System.out.println("bool.equals(boolDifferent) has the value false :: "+(bool.equals(boolDifferent)));
 System.out.println("bool.hashCode() == boolDifferent.hashCode() has the value false :: "+(bool.hashCode() == boolDifferent.hashCode()));

 //for same Integer
 System.out.println("-----------------for same Integer-----------------");

 System.out.println("intt hashcode value :: "+(intt.hashCode()));
 System.out.println("inttSame hashcode value :: "+(inttSame.hashCode()));
 System.out.println("intt == inttSame has the value true :: "+(intt == inttSame));
 System.out.println("intt.equals(inttSame) has the value true :: "+(intt.equals(inttSame)));
 System.out.println("intt.hashCode() == inttSame.hashCode() has the value true :: "+(intt.hashCode() == inttSame.hashCode()));

 //for different Integer
 System.out.println("-----------------for different Integer-----------------");

 System.out.println("intt hashcode value :: "+(intt.hashCode()));
 System.out.println("inttDifferent hashcode value :: "+(inttDifferent.hashCode()));
 System.out.println("intt == inttDifferent has the value false :: "+(intt == inttDifferent));
 System.out.println("intt.equals(inttDifferent) has the value false :: "+(intt.equals(inttDifferent)));
 System.out.println("intt.hashCode() == inttDifferent.hashCode() has the value false :: "+(intt.hashCode() == inttDifferent.hashCode()));

 //for same Long
 System.out.println("-----------------for same Long-----------------");

 System.out.println("longg hashcode value :: "+(longg.hashCode()));
 System.out.println("longgSame hashcode value :: "+(longgSame.hashCode()));
 System.out.println("longg == longgSame has the value true :: "+(longg == longgSame));
 System.out.println("longg.equals(longgSame) has the value true :: "+(longg.equals(inttSame)));
 System.out.println("longg.hashCode() == longgSame.hashCode() has the value true :: "+(longg.hashCode() == longgSame.hashCode()));

 //for different Long
 System.out.println("-----------------for different Long-----------------");

 System.out.println("longg hashcode value :: "+(longg.hashCode()));
 System.out.println("longgDifferent hashcode value :: "+(longgDifferent.hashCode()));
 System.out.println("longg == longgDifferent has the value false :: "+(longg == longgDifferent));
 System.out.println("longg.equals(longgDifferent) has the value false :: "+(longg.equals(longgDifferent)));
 System.out.println("longg.hashCode() == longgDifferent.hashCode() has the value false :: "+(longg.hashCode() == longgDifferent.hashCode()));

 //for same Character
 System.out.println("-----------------for same Character-----------------");

 System.out.println("chr hashcode value :: "+(chr.hashCode()));
 System.out.println("chrSame hashcode value :: "+(chrSame.hashCode()));
 System.out.println("chr == chrSame has the value true :: "+(chr == chrSame));
 System.out.println("chr.equals(chrgSame) has the value true :: "+(chr.equals(chrSame)));
 System.out.println("chr.hashCode() == chrSame.hashCode() has the value true :: "+(chr.hashCode() == chrSame.hashCode()));

 //for same Character
 System.out.println("-----------------for same Character-----------------");

 System.out.println("chr hashcode value :: "+(chr.hashCode()));
 System.out.println("chrDifferent hashcode value :: "+(chrDifferent.hashCode()));
 System.out.println("chr == chrDifferent has the value false :: "+(chr == chrDifferent));
 System.out.println("chr.equals(chrDifferent) has the value false :: "+(chr.equals(chrDifferent)));
 System.out.println("chr.hashCode() == chrDifferent.hashCode() has the value false :: "+(chr.hashCode() == chrDifferent.hashCode()));

 }

 @Override
 public boolean equals(Object obj) {
 // TODO Auto-generated method stub

 if (obj instanceof hashEqualsText) {
 return true;
 }
 return false;
 }

 @Override
 public int hashCode() {
 // TODO Auto-generated method stub
 return 1;
 }
 }

The output is as follows

-----------------hashCode contract-----------------
Overrided hashCode of x::1 - Original hashCode of x::7214088
Overrided hashCode of y::1 - Original hashCode of y::7214088
Overrided hashCode of z::1 - Original hashCode of z::15020296
-----------------equals contract-----------------
reflexive - x.equals(x) should return true :: true
symmetric - x and y, x.equals(y)  should return true if and only if y.equals(x) returns true :: true< -- >true
transitive - x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true :: true===true==>true
null check - x.equals(null) should return false always:: false
-----------------for same Objects-----------------
x == y has the value true :: true
x.equals(y) has the value true :: true
x.hashCode() == y.hashCode() has the value true :: true
-----------------for different Objects-----------------
object hashcode value :: 16130931
x == object has the value false :: false
x.equals(object) has the value false :: false
x.hashCode() == object.hashCode() has the value false :: false
-----------------for same String-----------------
str hashcode value :: 112212784
strSame hashcode value :: 112212784
str == strSame has the value true :: true
str.equals(strSame) has the value true :: true
str.hashCode() == strSame.hashCode() has the value true :: true
-----------------for different String-----------------
str hashcode value :: 112212784
strDifferent hashcode value :: -1181071670
str == strDifferent has the value false :: false
str.equals(strDifferent) has the value false :: false
str.hashCode() == strDifferent.hashCode() has the value false :: false
-----------------for same Boolean-----------------
bool hashcode value :: 1231
boolSame hashcode value :: 1231
bool == boolSame has the value true :: true
bool.equals(boolSame) has the value true :: true
bool.hashCode() == boolSame.hashCode() has the value true :: true
-----------------for different Boolean-----------------
bool hashcode value :: 1231
boolDifferent hashcode value :: 1237
bool == boolDifferent has the value false :: false
bool.equals(boolDifferent) has the value false :: false
bool.hashCode() == boolDifferent.hashCode() has the value false :: false
-----------------for same Integer-----------------
intt hashcode value :: 123456789
inttSame hashcode value :: 123456789
intt == inttSame has the value true :: false
intt.equals(inttSame) has the value true :: true
intt.hashCode() == inttSame.hashCode() has the value true :: true
-----------------for different Integer-----------------
intt hashcode value :: 123456789
inttDifferent hashcode value :: 2222222
intt == inttDifferent has the value false :: false
intt.equals(inttDifferent) has the value false :: false
intt.hashCode() == inttDifferent.hashCode() has the value false :: false
-----------------for same Long-----------------
longg hashcode value :: -1634700041
longgSame hashcode value :: -1634700041
longg == longgSame has the value true :: false
longg.equals(longgSame) has the value true :: false
longg.hashCode() == longgSame.hashCode() has the value true :: true
-----------------for different Long-----------------
longg hashcode value :: -1634700041
longgDifferent hashcode value :: 653330616
longg == longgDifferent has the value false :: false
longg.equals(longgDifferent) has the value false :: false
longg.hashCode() == longgDifferent.hashCode() has the value false :: false
-----------------for same Character-----------------
chr hashcode value :: 97
chrSame hashcode value :: 97
chr == chrSame has the value true :: true
chr.equals(chrgSame) has the value true :: true
chr.hashCode() == chrSame.hashCode() has the value true :: true
-----------------for same Character-----------------
chr hashcode value :: 97
chrDifferent hashcode value :: 122
chr == chrDifferent has the value false :: false
chr.equals(chrDifferent) has the value false :: false
chr.hashCode() == chrDifferent.hashCode() has the value false :: false