Thursday, September 24, 2009
Test Your Skills
1) Integer a[]=new Integer[10];
2)Object b[]=new Integer[10];
3)Object c=new Integer[10];
Just think it.Don't try it in your java editor.
Actually all statements are right.Specially I want to mention
about third statement .It is valid because Object class is a base
class for Array Class too.
Tuesday, September 15, 2009
Singleton Vs Static class
A design pattern that is used to restrict instantiation of that class to a single instance. A singleton is responsible for creating and maintaining its unique instance, and must define a public member that grants clients a global point of access to the unique instance.
Static class:
A class which has all data members as static.Static class members can be used to separate data and behavior that is independent of any object identity. It can be used when there is no data or behavior in the class that depends on object identity
Advantage of "Singleton" over the "Static Class" are
- Singletons can implement interfaces and inherit from other classes
- A singletons can be lazy loaded. Only when it is actually needed. That's very handy if the initialization includes expensive resource loading or database connections.
- Singletons offer an actual object.So we can pass it as parameters as well.
- Singletons can be extended into a factory. The object management behind the scenes is abstract so it's better maintainable and results in better code.
Friday, September 4, 2009
Why we need an immutable object?
First one:
class A{
private StringBuffer name;
public void setName(StringBuffer Name){
name=Name;
}
public StringBuffer getName(){
return name;
}
}
class B{
A a=new A();
StringBuffer temp=new StringBuffer("Anai");
a.setName(temp);
System.out.println(a.getName());
temp.append('a');
System.out.println(a.getName());
}
output:
Anai
Anaia
It is completely against "encapsulation" concept.It is mainly because of mutable class(StringBuffer).
Second one:
see the immutable class which i mentioned it on my previous post.Suppose you want to maintain table of past data which never going to be changed then this kind of implementation will be helpful.
Main Advantage of Immutable is ThreadSafe
How to make an object immutable?
- Do not provide any methods such as setters or any methods that may alter the properties of that object. If you do, make sure they return a new instance of your object.
- All properties and methods of that object are to be marked as final. Marking the methods as final is extremely important. We don’t want anyone carelessly overriding our functions that may compromise our immutable behavior( or making class as final is enough).
- All properties of that object are to be marked as private
public final class BudgetData{
private float _budget;
private String _name;
private Date _date;
public (float budget, String name, Date date){
_budget = budget;
_name = name;
_date =new Date(date.getTime());
}
public final (float getBudget() {
return _budget;
}
public final String getName() {
return _name;
}
public final Date getDate() {
return new Date(_date.getTime());
}
}
Thursday, September 3, 2009
Are Strings really immutable?
e.g:
Normal :
String lc = "lowercase";Now it may look immutable.But you can modify it with the following snippet:
lc.toUpperCase();
System.out.println(lc);
Field stringValue = String.class.getDeclaredField("value");
stringValue.setAccessible(true);
stringValue.set(orig, orig.toUpperCase().toCharArray());
This is because of SecurityManager is not turned on.Though
you have an immutable class it is mutable unless the security manager
is turned on.
Wednesday, September 2, 2009
Immutable
Immutable means unchangeable. In Java, when an object is defined as being immutable it means that once it has been initialized its state cannot be changed.
Primitive data types (i.e., int, short, long, byte, char, float, double, boolean) can be made immutable by using the "final" keyword. Once they have been assigned a value it cannot be changed.
Basically String class is an immutable class.StringBuffer is an mutable class.Follwing e.g. will explain it in details.
String changeString = "hello ";
changeString = changeString + "world";
System.out.println(changeString);
The output as expected is:
hello world
You might think the program is simply changing the state of the String object to append "world" but it's not that simple. As Strings are immutable and cannot have their values changed, what happens is a new String object is created with a state of "hello world" and assigned to the changeString variable.
Tuesday, September 1, 2009
Arraylist Vs Vector
But Vector is synchronized one.So its a thread safe.