• Post Reply Bookmark Topic Watch Topic
  • New Topic

own immutable class  RSS feed

 
T sandya
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
All of we know String is an immutable class.Like sting class funcionality,i want to write our own immutable class called "MYSTRING".this should work same as string class which is avaiable in lang package.How can do it.please help me anybody.

regards,
sandya.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't provide any methods that change the state of the object. There is nothing else that is special about immutable classes.

Why are you writing your own String class???
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if it's supposed to work just like String, then you can start with the source of the String class. The source is in a file called src.zip, which comes with all Sun JDKs.

Immutability takes different forms. String objects can't be changed once they're created, but the String class has several methods that work with the content of a String; it's just that they create a new String object which they then return.

The key is not to provide any direct access to fields, and to keep private all methods that alter the state of the object.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
semantics:
- the class itself is immutable because it's declared final.
- instances of it are immutable because the class lacks any means to change the state of those instances.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen T Wenting:
semantics:
- the class itself is immutable because it's declared final.
- instances of it are immutable because the class lacks any means to change the state of those instances.


- the class is immutable because all of its operations are referentially transparent.
- there is no such thing as 'changing state' - only observing some change to
(what is often described as) "the world" (which may or may not include operations on java.lang.String) - which the String type does not expose on itself or any other part of "the world", and therefore, can be said to be immutable.

The need for a FAQ entry defining immutability has just lifted in priority.
 
T sandya
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
so,according to above replies if i write my own immutable class "mystring",can i use this "mystring" class instead of String class which is in java.lang package.

regards,
sandya.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16046
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and no. Things like this will not work:

The compiler has a special treatment for class String, and there's no way to tell the compiler to treat your own class exactly the same way that it treats the standard String class.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also can't pass instances of your class to methods that expect a java.lang.String.

This is by design, for security reasons.

What are you trying to accomplish?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, a quick google on "referentially transparent" found many things on functional programming but no real overlap with object oriented thinking and immutable.

The definitions I saw mostly said we can replace an expression with its value with no effect. For example, anyplace the code says "sum(2,3)" we could use "5" instead.

I get how you're using it in relation to immutable, but is that your own application of the term or should I be looking elsewhere?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Tony, a quick google on "referentially transparent" found many things on functional programming but no real overlap with object oriented thinking and immutable.

You're right - the term applies to FP. There are papers out there* that clarify what OO really is (ignoring the hyperbole). In short, a static grouping of functions i.e. OO is just a collection of functions (or not). One might argue about the benefits and drawbacks of this static grouping - I argue that it caters entirely for human conceptualisation and spatial cognition limitations (which is fine) at the expense of "software" (which is not fine hence my rants).

Consider String.charAt(int). This is a function:
char charAt(String, int)
This function is referentially transparent and is directly analogous to the OO equivalent. In fact, this can be said for all functions of java.lang.String therefore we say it is immutable. One might argue that since the function takes a type String as its first parameter it is still "OO", however, this is just a higher-order function in disguise.

* Benjamin C. Pierce and David N. Turner. Simple type-theoretic foundations for object-oriented programming. Journal of Functional Programming, 4(2):207-247, April 1994.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someday, on another thread, I'd be interested in your definition of "software" since it doesn't seem to include a lot of programs I'm familiar with.

I wish I could attribute this properly but one of the folks involved with Simula and/or Smalltalk, discussing the creation of OO and what it means, said "We thought all we did was move the data from the stack to the heap. It didn't seem like such a big deal." or something to that effect. It didn't make me think they had much language theory in mind when they did it. But maybe he was just being cute for an interview.
[ August 18, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!