Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Nice Concepts


#####################################################################################################

Selection among overloaded methods is static (Compile Time), while selection among overridden methods is dynamic

List Vs Set

Set having only one remove(Object) method instead List having two overloaded remove methods 1) remove(int) 2) remove(Object)

You can get the importance of these overloaded remove method from below example

public class SetList {

public static void main(String[] args) {

Set<Integer> set = new TreeSet<Integer>();

List<Integer> list = new ArrayList<Integer>();

for (int i = -3; i < 3; i++) {

set.add(i);

list.add(i);

}

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

set.remove(i);

list.remove(i);

}

System.out.println(set + ” ” + list);

}

}

Output:

[-3, -2, -1] [-2, 0, 2]

#####################################################################################################

Polymorphism in JAVA will work only on methods, it will not work on instance level public variables.

package chap4;

public class A {
public A() {
System.out.println(“A” + name);
}

public String name = “Class A”;

public void print() {
System.out.println(“A”);
}
}

package chap4;

public class B extends A {
public B() {
System.out.println(“B” + name);
}

public String name = “Class B”;

public void print() {
System.out.println(“B”);
}
}

package chap4;

public class TestMain {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
System.out.println(a1.name + ” : ” + a2.name);
a1.print();
a2.print();
}
}

Output:

AClass A
AClass A
BClass B
Class A : Class A
A
B

#####################################################################################################

Avoid float and double if exact answers are required

Float and double perform binary floating-point arithmetic, which was carefully designed to furnish accurate approximations quickly over a broad range of magnitudes.

See the example below:

public static void main(String[] args) {

System.out.println(“Float / Double OperationResult: ”

+ (1.00 – 9 * .10));

System.out.println(“#################################”);

BigDecimal bd1 = new BigDecimal(“1.00”);

BigDecimal bd2 = new BigDecimal(“0.10”);

BigDecimal bd3 = new BigDecimal(“9”);

System.out.println(“BigDecimal OperationResult: ”

+ bd1.subtract(bd2.multiply(bd3)));

System.out.println();

System.out

.println(“Note: Disadvantages to using BigDecimal: it’s less convenient than using a primitive arithmetic type, and it’s slower & Memory Consuming.”);

System.out

.println(“Note: Go for int / long wherever it is possible to use.”);

}

#####################################################################################################

How HashMap works in Java

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: