2

There are immutable types in Java. And if you pass an immutable object to a method, it won't be changed after calling that method even if it's value was changed in that method. I know that all primitives are immutable. And String is immutable too.
I wrote a simple code for checking that functionality in Java and it seems that wrapper classes for primitives are immutable too. Can anybody list all immutable types in java?

shift66
  • 11,760
  • 13
  • 50
  • 83
  • 3
    primitives aren't immutable. The fact that "they do no change after passing them to a method" is because they are passed by value, not due to being immutable. – amit Jan 18 '12 at 06:54
  • Similar question: http://stackoverflow.com/questions/5124012/examples-of-immutable-classes – Azodious Jan 18 '12 at 06:56
  • what would be the reason to know this list? – Dapeng Jan 18 '12 at 06:58
  • @Dapeng - you could memorize it and recite it at your next job interview. I bet they'd be impressed ... though not necessarily in a good way :-) – Stephen C Jan 18 '12 at 07:06
  • 4
    @amit - actually primitive values **are** immutable. The value `1` is always one, and there's nothing you can do to change its one-ness. – Stephen C Jan 18 '12 at 07:09
  • @Stephen C - as an interviewer myself, I would think "hmmm, what's wrong with this candidate ... " – Dapeng Jan 18 '12 at 07:13
  • There is an infinite number of immutable types in Java, only a finite (but still very large) number of which will ever be typed into a text editor. So no, I won't list them. – Tom Crockett Jan 18 '12 at 07:35
  • @pelotom - Actually, unless you live in a different Universe that has a (literally) infinite number of states, there can only be a finite number of Java types ... :-) – Stephen C Jan 18 '12 at 08:11
  • @StephenC - Interesting idea, denying the reality of a concept because it is not finitely representable. By the same logic do you deny the existence of infinitely many integers, just because you can't write them all down? If there aren't infinitely many, tell me the largest :-) – Tom Crockett Jan 18 '12 at 08:26
  • @pelotom - fair enough. I wasn't being entirely serious. But my point is that while there are an uncountable number of Java types possible in theory, in practice the number of actual types that have been written is bounded by the number of Java programmers. And if you include generated classes, the bound depends on the number of computers that are capable of doing the generation. – Stephen C Jan 18 '12 at 11:51
  • @StephenC - I realized you weren't completely serious, I just enjoy pedantic jousting :D Re. "in practice...", that's what I meant by "only a finite (but still very large) number of which will ever be typed into a text editor." Also to be _completely_ pedantic, even in theory there's only a countable infinity of possible Java types! – Tom Crockett Jan 18 '12 at 16:04

3 Answers3

5

it won't be changed after calling that method even if it's value was changed in that method

has nothing to do with immutable.

void change(SomeType st) {
    st.setValue(123); 
}

will change the real thing. While

void change(SomeType st) {
    st = new SomeType(123);
}

won't. The difference here is whether you are changing the reference or operating on the object referenced.

String and those wrapper class are called immutable because there is absolutely no member function like setValue that could change that object.

Hope you understand this. You may comment and ask freely if you have any more doubt.

Haozhun
  • 6,331
  • 3
  • 29
  • 50
1

No, thats impossible.

Everyone can add his own immutable types and i am pretty sure each different version of java introduced some new ones

Peter
  • 5,728
  • 20
  • 23
  • Yes I know, I've searched in google. I want to know all known immutable types. For example Integer. Everybody can use this type. It seems that URI is immutable too. Is it ritght? – shift66 Jan 18 '12 at 06:59
  • @Ademiban: Where does it end? Everybody can use Apache commons as well, should immutables there be listed as well?> – amit Jan 18 '12 at 07:04
  • javadoc has all that info. from the URI javadoc: Instances of this class are immutable. so yes URI is immutable – Peter Jan 18 '12 at 07:05
  • I thought there is something special in immutable. @Gene explained why they are called so. Thank you all. – shift66 Jan 18 '12 at 07:09
0

Any Class thats purpose to hold a primitive value, that are provided in jdk should pass this list.

Example - Integer, Long, Double, String and so on.

Amit Gupta
  • 577
  • 4
  • 14
  • In Java, primitive type are enumerable: byte, short, int, long, char, float, double, bool. Primitive and immutable are two totally different things. – Haozhun Jan 18 '12 at 07:03
  • @Gene, You misunderstood the statement. Any Class thats a representation of primitive values... i hope now you get the point. – Amit Gupta Jan 18 '12 at 07:07
  • I still didn't get your point. Pardon me. However, the downvote is locked due to time out and I can't change it now. Sorry. – Haozhun Jan 18 '12 at 07:11
  • no issues... friend this is good and will help me to frame my Answer more comprehensive and clear. – Amit Gupta Jan 18 '12 at 16:02