Skip to main content

Posts

Showing posts from May, 2015

Always Use StringBuilder while concatenating Strings within loops

A common tendency of Java programmers is to always concatenate Strings using + operator. Which is actually very good, and simplifies the code by improves readability, since we would have to use StringBuilder.append(String), if the single + operator was not allowed. In fact if we look in byte code generate from such concatenation style, we will see a StringBuilder being used to perform the action. Check the JSL:    JLS Now , the point is, although this facility, you should not use the + operator in loop concatenation. Why? A new  StringBuilder  Object will be constructed at every single loop iteration (with initial value of str) and at the end of every iteration there will be concatenation with initial String (actually  StringBuilder  with initial value of  str ). So you need to create StringBuilder by yourself only when you work with String concatenation in loop. Let us procuce the evidence First, run this code, and see how long it takes to be executed: Now, bellow is th

Understanding What Instrumentation is

It would be possible to change the Java bytecode of your classes? It would be possible to create new procedures and change the body of his methods, even at runtime? Yes, the answer is yes, this is possible! Thanks to the APIs lowest level or higher level as java.lang.instrumentat and Apache BCEL among others.   For better transparency, you can always automatically generate code and add it to their classes. There are several frameworks. The java developer may be adding a simple enhancer code in their applications, which is relatively easy to do, with little intrusion into your creative process. Byte code enhancement Programs written in Java to compile a form of code called bytecode. The idea is through instrumentation, inject byte code to add services and still maintain transparency. For example, most implementations use an enhancer JDO bytecode. Another excellent example was a project at IBM where test scenarios were created dynamically through bytecode instrumentation. Often