Showing posts with label Immutable Class. Show all posts
Showing posts with label Immutable Class. Show all posts

Thursday, October 27, 2011

Example for Customized Immutable Class


public final class MyString
{

private final char[] value;

private int length;

public MyString()
{

length=0;
value=new char[length];
}

public MyString(char c[])
{

length=c.length;
value=new char[length];
System.arraycopy(c, 0, value, 0, length);
}

public String toString()
{

return new String(value);
}

public MyString concat(MyString s)
{

char temp[]=new char[length+s.length()];
System.arraycopy(value, 0, temp, 0, length);
System.arraycopy(s.value, 0, temp, length, s.length());
MyString str=new MyString(temp);
return str;
}

public int length()
{

return length;
}
}