Sunday, March 17, 2013

Java :: HashMap Vs HashTable



 HashMap Vs HashTable

There are several differences between HashMap and Hashtable in Java:
  1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  2. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  3. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Since synchronization is not an issue for you, I'd recommend HashMap.
Reference: 
http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable

Monday, February 25, 2013

Java :: String ValueOf Vs String Concatenation

String ValueOf Vs String Concatenation

From stack over flow I found bellow answer, as conclusion:
"Never use (+ "") to convert values to string, instead use String.valueOf."




public void foo(){
int intVar = 5;
String strVar = intVar+"";    
}
This approach uses StringBuilder to create resultant String
public void foo();
  Code:
   0:   iconst_5
   1:   istore_1
   2:   new     #2; //class java/lang/StringBuilder
   5:   dup
   6:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   9:   iload_1
   10:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(I)Ljava/lan
g/StringBuilder;
   13:  ldc     #5; //String
   15:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
   18:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/la
ng/String;
   21:  astore_2
   22:  return

public void bar(){
int intVar = 5;
String strVar = String.valueOf(intVar);
}
This approach invokes simply a static method of String to get the String version of int
public void bar();
  Code:
   0:   iconst_5
   1:   istore_1
   2:   iload_1
   3:   invokestatic    #8; //Method java/lang/String.valueOf:(I)Ljava/lang/Stri
ng;
   6:   astore_2
   7:   return

Saturday, February 23, 2013

Java :: How to override equals

How to override equals

All what you need to do is add the following piece if code in your object:


@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
YourObject rhs = (YourObject ) obj;
if (!rhs.getId().equals(getId())) // For Example
return false;
return true;
}


Have a nice day.

Wednesday, February 13, 2013

Java :: Variable Initialization

Variables initialization Advantages (In / out Constructor)

You can initialize a variable using the following:

  • Inside a constructor:

public Class Test() {
public int var;
  public Test()
{
    var = 10;
  }
}

  • Outside a constructor
public Class Test() {
         public int var = 10;
}


Advantages
Inside Constructor
Outside Constructor
One initialization even if you have multiple constructors

X
Can add exception handling to the initialization
X

More Readable

X
Faster Class Load
-
-


No major difference between both, developer use what you want!!
There is another types as static block will talk about later.

Have a nice day

Tuesday, January 29, 2013

Development Tips :: Responsibilities for Senior Java Developer

Senior Java Developer Responsibilities

A senior Java developer is responsible for developing applications using the Java programming language. A developer in this position is also responsible for overseeing the execution of software development from the conceptual phase to the testing phase. Because this is a senior level position, he may also be responsible for mentoring junior developers as well as working with customers and handling research groups.

Read more: Senior Java Developer Job Description | eHow.com http://www.ehow.com/about_6601671_senior-java-developer-job-description.html#ixzz2JLvCcnbV


I don't think there is an agreement over the job description for the Senior Developer, you can see that a company in Toronto asks for the following:

  • At least 10 year experience in high availability software and system implementation based on J2EE architecture and design patterns
  • Must have experience with asynchronous communications and networks protocols and concepts
  • Oracle 9i or 10g
  • Ant Scripting or Maven
  • Experience with PKI and Security concepts
  • Excellent communication skills
  • Team player and willing to mentor junior developers
  • Process oriented
  • Worked previously in structured environment where process has been followed and respected.

While in New York they asks for the following:

  • - 5+ years experience in J2EE Development 
  • - 3+ years designing UI and J2EE applications
  • - Understanding of standard J2EE UI patterns
  • - Must be able to work off hours when necessary to support production jobs or resolve production issues
While in Sofia,Bulgaria:
  • Software developer with more than 3 years commercial experience
  • Deep knowledge in JAVA technology and OOP principles
  • Understanding and experience with basic design patterns and reusable software design approaches
  • Experience with SWT
  • Excellent communication skills and fluent in English
  • GUI design skills in an IDE context
While in India:
  • Education: (UG –B.Tech/B.E Any Graduate - Any Specialization) AND (PG –Post Graduation Not Required). 
  • Experience: 3-6 years
While in Sydney:
  • 8+ years of overall experience. 
  • Experience with XML, XSLT XPath, and XQuery. 
  • Experience with Spring and Hibernate or IBatis as ORM.
  • Understanding of EDI. 
  • Abilities to drive complex technical design across system domain boundaries. 
  • Experience developing and architecting object-orientated applications using one or more of the following programming languages: J2EE, Spring WebLogic/WebSphere, Application server, Oracle (SQL-PL/SQL). 
  • Demonstrated proficiency in designing distributed applications in which multiple operating systems, languages, and vendor middleware interoperate.
I think that years of experience can't reflect the real experience of the person, yes off course the senior developer should have a years of work experience to say he is a senior but I think 1 year of experience for a developer is better than 3 years for another.

Adding special technologies like knowing Hibernate for example as a condition for recruitment is nonsense from my opinion, as a developer i think i could develop any kind of technology if I have the concept with experience in other related technologies.

Have a nice day.

Monday, January 28, 2013

Java :: String Builder VS String Buffer

String Builder VS String Buffer

StringBuffer is synchronized, StringBuilder is not, So off course string builder is faster.

And applying the following example using i7 processor and 8 GB ram:


public static void main(String args[]) {
long startTime = System.currentTimeMillis();
//StringBuffer concat = new StringBuffer();
StringBuilder concat = new StringBuilder();
for (int i = 0; i < 1000000; i++) {
concat.append(i);
}
long endTime = System.currentTimeMillis();
System.out.print("length: " + concat.length());
System.out.println(" time: " + (endTime - startTime));
}

will returns the following:

Using String Builder 47 Milliseconds.
Using String Buffer  57 Milliseconds.

Soooo, why to use the "String Buffer"??

as mentioned string buffer is synchronized, and that's means what???
It means that string buffer during appending will have to do a new task which is checking and making sure that only one instance is appending to the buffer at the same exact moment, so if you felt that your system will face that case -appending at the same time- you SHOULD use string buffer.

Have a blessed day :)