Srujan kumar Gulla
Tuesday, April 16, 2013
JIT compiler - Nice Idea
Taking the best of two worlds - results in interpreting(converting to machine code) machine independent byte code at the cost of dynamic compilation (generates optimized code based on target machine) + caches translated code to minimize performance degradation
Monday, March 25, 2013
Why is char[] preferred over String for passwords?
String strPwd="Java1234";
char[] charPwd= new char[]{'J','a','v','a','1','2'
,'3','4'};System.out.println("String password: " + strPwd);
System.out.println("Character password: " + charPwd);
Wednesday, March 14, 2012
Singleton pattern is tricky
1) Singleton that every body knows
class Foo {
private static Helper helper = null;
public static Helper getHelper() {
if (helper == null) {
helper = new Helper();
}
return helper;
}
private Foo(){}
}
Analyzing point 1: Pros: Good for single threaded application
Cons: Gets real bad in multithreaded programs. Ex: Threads 1,2 will start at the same time and create two instances defeating the purpose. Proof: try syso(getHelper());
2) Basic fix:
class Foo {
private static Helper helper = null;
public static synchronized Helper getHelper() {
if (helper == null) {
helper = new Helper();
}
return helper;
}
private Foo(){}
}
Analyzing technique 2:
Pros: Wow!! fixes the cons of technique 1
Cons: Expensive. Imagine 50 Threads in your application and every time they invoke getHelper you pay the cost of synch and reduces application speed.
3) Lazy Fix:
class Foo {
private static Helper helper = new Foo();
public static Helper getHelper() {
return helper;
}
private Foo(){}
}
Pros: Fixed it for MultiThreading. Loading static pieces once owned by class
Cons: UnNecessary creation of objects if not needed. Imagine you have a lot of classes with this fix and you want to create objects on meeting certain conditions, but this will all objects irrespective of conditions met
More techniques coming soon!!!
Friday, February 17, 2012
Index for a table - Factors to consider
Creating an index Consider indexing keys that are used frequently in WHERE clauses.
Consider indexing keys that are used frequently to join tables in SQL statements.
Choose index keys that have high selectivity. The selectivity of an index is the percentage of rows in a table having the same value for the indexed key. An index's selectivity is optimal if few rows have the same value. Note: Oracle automatically creates indexes, or uses existing indexes, on the keys and expressions of unique and primary keys that you define with integrity constraints. Indexing low selectivity columns can be helpful if the data distribution is skewed so that one or two values occur much less often than other values.
Do not use standard B-tree indexes on keys or expressions with few distinct values. Such keys or expressions usually have poor selectivity and therefore do not optimize performance unless the frequently selected key values appear less frequently than the other key values. You can use bitmap indexes effectively in such cases, unless the index is modified frequently, as in a high concurrency OLTP application.
Do not index columns that are modified frequently. UPDATE statements that modify indexed columns and INSERT and DELETE statements that modify indexed tables take longer than if there were no index. Such SQL statements must modify data in indexes as well as data in tables. They also generate additional undo and redo.
Do not index keys that appear only in WHERE clauses with functions or operators. A WHERE clause that uses a function, other than MIN or MAX, or an operator with an indexed key does not make available the access path that uses the index except with function-based indexes.
Thursday, February 16, 2012
Truth about Java string pool and Intern
Sunday, February 12, 2012
Java string pool and intern
intern()
on a String has the effect of moving it out from the heap into the permanent generation, and you risk running out of PermGen space.