TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Core Java Tips
Discussion started by Raghuveer Rawat , on 29 January 11:53 AM
Do you have tip which can make java developers life easier then post here in reply to this discussion?
Tip can be programming tip, performance tip, best practice, utility function, and similar things.

Please don't repeat a tip which is already posted and also please give proper description with examples for your tip.
Replies
You will need to be a member of this group first before you can post a reply.
Rajesh Pawar
What is the difference between final, finally and finalize in java?

final – it is used for constant, method or class declaration. When used with constants, their values can be changed later, when used with methods, they can't be overridden by subclasses, final classes cant' be derived or inherited by other classes.

finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

finalize() – method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.
Monday, 17 May 2010 10:34
 
Rajesh Pawar
What is the difference between for loop and Iterator?

There are two ways to traverse collections: (1) with the for loop and (2) by using Iterators.
so when do you use each...

for loop can be used when you just want to loop through the collection whereas use Iterator instead of the for loop when you need to:

* Remove the current element. The for loop hides the iterator, so you cannot call remove. Therefore, the for loop is not usable for filtering.
* Iterate over multiple collections in parallel.

Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.
Monday, 17 May 2010 10:31
 
Deepika Negi
Difference between String, StringBuffer and StringBuilder

String is immutable (they can't be modified) whereas StringBuffer and StringBuilder objects are mutable (it means they can change their values).

The main difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

Criteria to choose among String, StringBuffer and StringBuilder

1. If your text is not going to change use a string Class because a String object is immutable.
2. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
3. If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
Thursday, 08 April 2010 10:48
 
Deepika Negi
This function will change a string in upper case into lower case string with first character of string in upper case.

public String changeUpperCaseToLowerCase(String input){

if(input == null || input.trim().equals(""))
return null;

StringBuilder output = new StringBuilder();
String lowercase = null;
String []tokens = input.trim().split("\s");
for(int i = 0; i < tokens.length; i++){

lowercase = tokens[i].toLowerCase();
char upperCase = Character.toUpperCase(lowercase.charAt(0));
lowercase = upperCase + lowercase.substring(1);
output.append(lowercase).append(" ");
}
return output.toString().trim();
}
Thursday, 08 April 2010 10:30
 
Deepika Negi
How to convert java.util.Date to java.sql.Date for use in JDBC...

public java.sql.Date getSQLDate(java.util.Date date){

if(date != null)
return new java.sql.Date(date.getTime());
else
return null;

}
Tuesday, 02 February 2010 11:41
 
Raghuveer Rawat
Java 5 for loop syntax..

List empList = getEmployeeList();
for (Employee emp : empList)
{
// now use emp object; no casting needed
String empId = emp.getEmpId();
}
Friday, 29 January 2010 11:58
 

TechFundu Polls

Which Laptop is the best value for money?

Dell - 33.3%
Gateway - 0%
Toshiba - 11.1%
Sony - 7.4%
Lenovo - 7.4%
Acer - 7.4%
Apple - 3.7%
HP - 22.2%
Others - 7.4%

Total votes: 27
The voting for this poll has ended on: 31 Dec 2010 - 00:00
Follow us on Twitter