Tag Archives: Java

Java, J2EE, Core Java, Spring, Hibernate, Database, Apache Maven, Ant, Struts, Swings, Dojo, Angular JS, JQuery

Java Training

Optimism Technology posting some goods tips for their readers. Below are few


Java Tip of the Day: 21 March 2018

For better performance of your application, use StringBuilder rather than the string concatenation operator (+)
and avoid using StringBuffer (use StringBuilder )
(1)Because String is immutable and it will create lot of garbage objects in memory for each (+) operation
(2)StringBuffer is ThreadSafe, but disadvantage is that due to this it is slow. (Practically most of the time you don’t need thread safety.)

Tip of the day: 17 March 2018
20-50-500 Rule

20: No package should have more than 20 classes.
50: No method should have more than 50 lines of code.
500: No class should have more than 500 lines of code.

Tip of the day (Web/JSP/Servlet): 09 Feb 2018

Redirect Versus Forward

1. Redirect sends the request back to browser and all previous request context and parameters are lost and a new request will be send to redirected page.
Forward does the decorator work for the request and can add more parameters to the request and send to the forwarded page.
2. Redirect is slower than Forward
3. Redirect will show the redirected page URL in address bar. Forward will not.
4. On refresh, only redirected page will be called not the page which has redirected i.e. if the flow is from A to B then on refresh only B will be called.
But in case of forward, if the flow is A to B then on refresh First A and then B will be called in the same sequence as occurred before.
5. Redirect can be used to send the request to another running web application. Forward will work within a web application only.
6. Redirect is done when servlet’s service method get completed i.e. statements after redirect call will execute first and after completion of service method redirect will occur.
But forward does the work of a method call i.e. the statements after forward call be executed when the forwarded page has done its work.
Example with a code snippet.
FirstServlet.java

System.out.println(“Before Next Page”);
response.sendRedirect(“NextServlet”);
System.out.println(“After Next Page”);

In NextServlet.java
System.out.println(“Next Servlet called….”);

In this case following will be the output:
Before Next Page
After Next Page
Next Servlet Called

I.e. in case of redirect first servlet ‘s service method will complete and then next servlet is called..
Now if we could have used forward instead of sendRedirect then following will be the output
Before Next Page
Next Servlet Called
After Next Page

Visit our blog : http://javasociety.blogspot.in/2009/09/redirect-and-forward.html

Tip of the day (JDBC): 07 Feb 2018

Prefer PreparedStatement over Statement
Generally we use PreparedStatement for SQL statements that take parameters. But, these can also be used with repeatedly executed SQL statements that do not accept parameters.
Benefits:
1. It allows you to write dynamic and parametric query.
2. It is faster than Statement in Java (because of pre-compilation, DBMS does not have to recompile the SQL statement and prepared an execution plan)
3. It prevents SQL Injection in Java

SQL Tip of the day: 06 Feb 2018

The difference between ‘Union’ and ‘Union all’ is that Union all will not eliminate duplicate rows, instead it fetches all rows from all tables as per query and combines them into a resultSet. And that is the reason that ‘Union All’ is faster than ‘Union’

Design Tip of the day: 04 Feb 2018

Use composition over Inheritance (for loose coupling & flexibility)

Java Tip of the day : 04 Feb 2018

It’s always better to return empty collection instead of null (where return type is any collection)