Morgan Stanley Interview Question for Java Developers


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
9
of 9 vote

An object is considered immutable if its state cannot change after it is constructed.

Advantages :
1. Simple and Reliable Code.
2. Useful in concurrent Applications since they cannot change state, they cannot be corrupted by thread interference or present in inconsistent state.
3. Decreased overhead in Garbage Collection.
4. Reduction in code needed to protect objects in concurrent applications.

JDK Examples : String, Integer

public class Mydate {
    private final Date date; // Declare the variable as final

    public Mydate(Date date) {
        this.date = new Date(date.getTime());
    }
    public getDate() {
        return new Date(date.getTime()); // Need to return a copy instead of original object
    }
    
     // Note: No Setter method for date
}

- vinayknl.61 February 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

+1, Good answer.

Why do you say using immutable objects reduces the garbage collection overhead, though? Since you have to discard an immutable object and create a new one every time you want the value to change, I would generally expect the effect on garbage collection to be somewhat the opposite.

- eugene.yarovoi February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

With immutable objects, we can avoid circular references and make GC's life easier.

I found the answer from Brian Goetz's article.

In most cases, when a holder object is updated to reference a different object, the new referent is a young object. If we update a MutableHolder by calling setValue(), we have created a situation where an older object references a younger one. On the other hand, by creating a new ImmutableHolder object instead, a younger object is referencing an older one. The latter situation, where most objects point to older objects, is much more gentle on a generational garbage collector. If a MutableHolder that lives in the old generation is mutated, all the objects on the card that contain the MutableHolder must be scanned for old-to-young references at the next minor collection. The use of mutable references for long-lived container objects increases the work done to track old-to-young references at collection time.

- vinayknl.61 February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Best answer.

- xankar March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Immutable object are the best candidate for Key's for hashing based map and set collections

- mathukolappan June 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Well Immutable Objects are those whose value cannot be changed.. it is used basically to increase the security of the the system.

- hprem991 January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To be precise, they are reusable and de facto thread safe.

- Saurabh January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Saurav and Rage:

Nyc points and implementation. But can you elaborate about its reusability.

- techieDeep January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@techieDeep
suppose your application needs several such objects(hundreds of thousands) , so for increasing the throughput of your application , instead of creating an object everytime , you can look into already existing objects table and assign an 'equal' object to it.
Precisely same is done in case of String class in java. Infact, it was the main reason to make String immutable.

- Saurabh January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.

And here is the answer to your question other question:

public class Mydate {
    private final Date date;
    public Mydate(Date date) {
        this.date = new Date(date.getTime());
    }
    public getDate() {
        return date;
    }
}

Since the date object passed in the constructor was created elsewhere you cannot sheild it from being changed unless you create a copy of it and use that in this class

- Rage January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rage:

Do we have to take care not to make date instance/reference available outside the object through getDate().

I mean what if the returned date object is modified outside....I know we cannot re-initialize as it is declared final, but date can be manipulated?

- Sai January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes , but instead creating a new Class for storing Date, we could use do that in our constructer itself.

// both name and date are marked final
immutable(String name,Date date)
{
 this.name = name;
// create own copy, initialize it for the date object given , could have used copy constructer if any.
this.date = new Date(date.getTime());
}

- Saurabh January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sai: references of final object are always mutable. To make date immutable via getDate change the return type to final Date instead of Date

- Rage January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Rage: you can't set the return type of a method to be final.

public final Date foo() { ... } does something very different than what you seem to think. It does not return an immutable object; rather, it seals the method from extension by subclasses. The "final" here is not part of the return type (the return type is still Date). I think that maybe you're equating "final" with "const" in C++, and they are not equivalent.

Your initial answer is not correct because you class is entirely mutable: I can change its state by calling yourObject.getDate().set(myNewTime). Simply creating a defensive copy on construction does not make your object immutable, but only makes there be no reference to the internals of the class on object instantiation. If you later leak references to the internals of your class (like your getDate() method does), the class is still mutable. What you need is a defensive copy both on construction and on returning internals. Copy-on-construction ensures that there are no external references to the internals of the class at the time the object is initialized. Copy-on-get ensures that no such references come to exist at a later time.

Assuming Date has a copy constructor, the full solution would be as follows:

public class ImmutableDate
{   
    //the final here isn't strictly necessary, because this field isn't
    //externally visible anyway
    private final Date date;
    public ImmutableDate(Date date) 
    {
        this.date = new Date(date);
    }
    public Date getDate() 
    {        
        return new Date(this.date);    
    }
}

- eugene.yarovoi January 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree with @eugene,yarovoi. We have to make defensive copy in both contructors and getters.

- Sai January 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this implementation:

public final class ImutableDate extends Date {

	private static final long serialVersionUID = 0xFEEDF00DL;
	public ImutableDate(long time) {
		super(time);
	}
	@Override
	public void setTime(long time) {
		throw new IllegalStateException("Cannot modify Imutable date object");
	}
}

- HG February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@HG: that works too, though I tend not to prefer that kind of approach, largely because illegal uses cannot be detected until runtime (the compiler can't catch them). The technique is very useful if you must be able to accept an ImmutableDate anywhere that you can accept a Date, however.

- eugene.yarovoi February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@HG: Collections.unmodifiableMap(...) in Java uses this approach to immutability, actually.

- eugene.yarovoi February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, the getter makes the Data variable mutable.

Getter should be:

public getDate() {
        return new Date(date.getTime());
    }

- xankar March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@eugene.yarovoi agree with your comment on runtime exception

- HG February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

immutable objects can note be changed using methods. we can only use these objects. we can not change them because there is no set methods. it leads to simple and reliable code.
public class Data{
private Date date;

public Data(date){ //date is set to the object and cannot change anymore
this.date=date;
}
public getDate(){ //this helps to take the value inside the object
return date;
}
//please note that there is no way to change the value in the object..thats why it is called a //immutable object
}

- Pheonix February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If Date is a mutable reference type (as seems to be the case here), your getDate() method leaks the reference and allows instances of the Data class to be modified.

- eugene.yarovoi February 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes... the Date Object should be a constant..otherwise it can be changed..
so the corrected definition should be like this..
public class Data{
private Final Date date;
....
public void getDate(){
....

- Pheonix February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Marking it as final doesn't make it a constant. It just means you can't assign to the member, a goal which was already being accomplished by the use of the private keyword and the lack of a setter. You can't reassign what object the "date" field points to, but the object itself can still be changed. When getDate() returns the instance of the Date class, that instance can still be modified.

For ways of solving this problem, see my and HG's comments to Rage's answer.

- eugene.yarovoi February 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)saves memory (if String s ="abc" is created then another string s1="abc" is created , internally s1=s; happens
2)for thread safety

- jnaveen771 May 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below link has a nice explanation:-

hxxp://howtodoinjava.com/2012/10/28/how-to-make-a-java-class-immutable/

- hulk May 03, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More