Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inserting In a List

Reply
Thread Tools

Inserting In a List

 
 
markspace
Guest
Posts: n/a
 
      04-03-2013
On 4/3/2013 3:46 PM, Joerg Meier wrote:

> final Point p = new Point(0, 0);
> p.move(1, 1);
>
> Are you really trying to say that you believe that the final keyword has
> made p immutable,


Nope, p is clearly mutable here.

> public class A {
> final int b = 123;
> }


OK, as far as it goes. But see below.

> But of course, again, you cannot make an mutable OBJECT immutable simply by
> creating a reference to it that is decorated with final.


Yup, you can. This class is also immutable:

public class Stooges {
private final ArrayList<String> stooges = new ArrayList<>(3);
{ stooges.add("Larry"); stooges.add("Curly"); stooges.add("Moe");}

public String getStooge( int stooge ) {
if( stooge < 1 || stooge > 3 ) throw new IllegalArgumentException();
return stooges.get( stooge-1 );
}
}

Now as long as I haven't made some syntax or other simple error, all
instances of Stooges are immutable under the section of the JLS I
quoted. More over, each one is thread safe in all circumstances and
does not need synchronization to make it thread safe.

You may know this yourself, but the way you wrote the bit I quoted made
is sound like final fields have no special semantics associated with
them with respect to immutable objects (like the stooges ArrayList I
used above), when in fact they do. Although not in the fashion you
implied with p above, of course.

> Again, I'm pretty certain that you already know all of the above and we are
> just having a communication breakdown.


I think so.


 
Reply With Quote
 
 
 
 
Joerg Meier
Guest
Posts: n/a
 
      04-03-2013
On Wed, 03 Apr 2013 16:10:46 -0700, markspace wrote:

> On 4/3/2013 3:46 PM, Joerg Meier wrote:
>> But of course, again, you cannot make an mutable OBJECT immutable simply by
>> creating a reference to it that is decorated with final.

> Yup, you can. This class is also immutable:


> public class Stooges {
> private final ArrayList<String> stooges = new ArrayList<>(3);
> { stooges.add("Larry"); stooges.add("Curly"); stooges.add("Moe");}
>
> public String getStooge( int stooge ) {
> if( stooge < 1 || stooge > 3 ) throw new IllegalArgumentException();
> return stooges.get( stooge-1 );
> }
> }


But it's immutable because you don't expose mutable functionality, not
because of the final keyword. Let me make a small alteration that will make
your class mutable, without removing the final keyword:

public class Stooges {
private final ArrayList<String> stooges = new ArrayList<>(3);
{ stooges.add("Larry"); stooges.add("Curly"); stooges.add("Moe");}

public String getStooge( int stooge ) {
if( stooge < 1 || stooge > 3 ) throw new IllegalArgumentException();
return stooges.get( stooge-1 );
}

public ArrayList<String> getStooges() {
return stooges;
}
}

There, and suddenly it's mutable.

> Now as long as I haven't made some syntax or other simple error, all
> instances of Stooges are immutable under the section of the JLS I
> quoted. More over, each one is thread safe in all circumstances and
> does not need synchronization to make it thread safe.


Yours would be immutable with or without the final keyword. It's kind of a
bad example, because ArrayList itself is mutable. Should have wrapped it in
Collections.unmodifiableList at least, but of course then it could still
have mutable contents (although of course not in the example, as String is
both immutable and final).

> You may know this yourself, but the way you wrote the bit I quoted made
> is sound like final fields have no special semantics associated with
> them with respect to immutable objects (like the stooges ArrayList I
> used above), when in fact they do. Although not in the fashion you
> implied with p above, of course.


final can be used to create a class that is immutable, but so can other
Java mechanics (such as private fields with no setter). While final
certainly lends itself to design of immutable classes, there really aren't
any special semantics associated with it any more than with the private
keyword.

>> Again, I'm pretty certain that you already know all of the above and we are
>> just having a communication breakdown.

> I think so.


Yeah, I figured halfway into my post that perceived misconception on your
part was too severe for it to actually be one. Nevertheless it seems a
worthwhile topic to discuss and maybe it will help someone else reading it
understand things better.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
 
 
 
Arved Sandstrom
Guest
Posts: n/a
 
      04-03-2013
On 04/03/2013 07:54 PM, Joerg Meier wrote:
> On Wed, 03 Apr 2013 18:09:36 -0300, Arved Sandstrom wrote:
>
>> If we're going to be pedantic I might as well add that I wouldn't myself
>> say that Java has ArrayLists or HashMaps either. I consider "Java" to be
>> the language. If we're going to talk official libraries you'd have to
>> refer to a specific version of J2SE/Java SE or J2EE/Java EE, for example.

>
> You can feel free to say whatever you want, but lets be realistic: the
> majority of people will say "Java has" for stuff in the JCL, and "Java
> doesn't have" for stuff not in the JCL. And I am 99% certain that if I had
> asked you any of the examples above, you would have said "No, Java doesn't
> have a val data type", and you would have says "Yes, Java has a hash map,
> it's java.util.HashMap".
>
> You can be pedantic all you want, but I would argue that "stuff in the JCL"
> is the definition used by the vast majority of people and really the only
> one that makes much sense.

[ SNIP ]

I'm probably not most people. I can do very little of my professional
Java work using just the Java language and only official Java platform
libraries. So I value 3rd party offerings very highly.

As for the questions, what are Java primitives if not value types? And I
very likely wouldn't say that Java has a hash map, honestly; it's too
ingrained in me that "Java" is the language only. I think similarly with
every other language + platform I can think of: C# is a .NET language,
the .NET framework is actually what supplies the libraries. Haskell is a
language, GHC and other environments supply many libraries. Etc.

But I'll concede that I'm not the vast majority of people. A lot of
people in that vast majority have also been often wrong when they
conflate an API supplied by the Java platform and an implementation
thereof, and happily believe that "Java" is providing all of it.

AHS

 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      04-03-2013
On Wed, 03 Apr 2013 20:30:53 -0300, Arved Sandstrom wrote:

> On 04/03/2013 07:54 PM, Joerg Meier wrote:
>> On Wed, 03 Apr 2013 18:09:36 -0300, Arved Sandstrom wrote:
>>> If we're going to be pedantic I might as well add that I wouldn't myself
>>> say that Java has ArrayLists or HashMaps either. I consider "Java" to be
>>> the language. If we're going to talk official libraries you'd have to
>>> refer to a specific version of J2SE/Java SE or J2EE/Java EE, for example.

>> You can feel free to say whatever you want, but lets be realistic: the
>> majority of people will say "Java has" for stuff in the JCL, and "Java
>> doesn't have" for stuff not in the JCL. And I am 99% certain that if I had
>> asked you any of the examples above, you would have said "No, Java doesn't
>> have a val data type", and you would have says "Yes, Java has a hash map,
>> it's java.util.HashMap".


>> You can be pedantic all you want, but I would argue that "stuff in the JCL"
>> is the definition used by the vast majority of people and really the only
>> one that makes much sense.

> [ SNIP ]


> I'm probably not most people. I can do very little of my professional
> Java work using just the Java language and only official Java platform
> libraries. So I value 3rd party offerings very highly.


Using a lot of JavaEE, I'm right there with you. But I wouldn't say
RestEASY or Jackson are part of Java any more than I would say Firefox is
part of Windows.

> As for the questions, what are Java primitives if not value types? And I


val data types != value types, a val type is a variable type that can be
used like:

for (val file : fileList) { ... // val is of type File here }

Basically an inferred type.

> very likely wouldn't say that Java has a hash map, honestly; it's too
> ingrained in me that "Java" is the language only. I think similarly with


So you would say that Java has ArrayMap, but not a hash map. You need to
pick one side to disagree with me on, it seems

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-03-2013
On 4/3/2013 4:25 PM, Joerg Meier wrote:

> Yours would be immutable with or without the final keyword.


No no no. This is my point. The final keyword has special semantics
associated with it in that particular case. It works a bit like the
volatile keyword: all writes to that point are made visible. In the
case of a private final field, the writes are made visible to ALL
THREADS in the system. THAT is what makes instances of the class
Stooges immutable.

That's why no synchronization is needed. Which is huge, conceptually.
I'll stop there because I think this may be the whole point of
misunderstanding here.







 
Reply With Quote
 
Arne Vajh?j
Guest
Posts: n/a
 
      04-03-2013
On 4/3/2013 7:30 PM, Arved Sandstrom wrote:
> On 04/03/2013 07:54 PM, Joerg Meier wrote:
>> On Wed, 03 Apr 2013 18:09:36 -0300, Arved Sandstrom wrote:
>>
>>> If we're going to be pedantic I might as well add that I wouldn't myself
>>> say that Java has ArrayLists or HashMaps either. I consider "Java" to be
>>> the language. If we're going to talk official libraries you'd have to
>>> refer to a specific version of J2SE/Java SE or J2EE/Java EE, for
>>> example.

>>
>> You can feel free to say whatever you want, but lets be realistic: the
>> majority of people will say "Java has" for stuff in the JCL, and "Java
>> doesn't have" for stuff not in the JCL. And I am 99% certain that if I
>> had
>> asked you any of the examples above, you would have said "No, Java
>> doesn't
>> have a val data type", and you would have says "Yes, Java has a hash map,
>> it's java.util.HashMap".
>>
>> You can be pedantic all you want, but I would argue that "stuff in the
>> JCL"
>> is the definition used by the vast majority of people and really the only
>> one that makes much sense.

> [ SNIP ]
>
> I'm probably not most people. I can do very little of my professional
> Java work using just the Java language and only official Java platform
> libraries. So I value 3rd party offerings very highly.


I guess most do, but that does not make anything part of Java.

> As for the questions, what are Java primitives if not value types? And I
> very likely wouldn't say that Java has a hash map, honestly; it's too
> ingrained in me that "Java" is the language only. I think similarly with
> every other language + platform I can think of: C# is a .NET language,
> the .NET framework is actually what supplies the libraries. Haskell is a
> language, GHC and other environments supply many libraries. Etc.
>
> But I'll concede that I'm not the vast majority of people. A lot of
> people in that vast majority have also been often wrong when they
> conflate an API supplied by the Java platform and an implementation
> thereof, and happily believe that "Java" is providing all of it.


I believe there are precedence for that the owner of the
Java trademark may sue someone using the name Java for something
not having all the classes in the Java library (SUN vs MS).

So I would say that:

Java = Java language + Java library + JVM

Arne


 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      04-03-2013
On 04/03/2013 08:35 PM, Joerg Meier wrote:
> On Wed, 03 Apr 2013 20:30:53 -0300, Arved Sandstrom wrote:
>
>> On 04/03/2013 07:54 PM, Joerg Meier wrote:
>>> On Wed, 03 Apr 2013 18:09:36 -0300, Arved Sandstrom wrote:
>>>> If we're going to be pedantic I might as well add that I wouldn't myself
>>>> say that Java has ArrayLists or HashMaps either. I consider "Java" to be
>>>> the language. If we're going to talk official libraries you'd have to
>>>> refer to a specific version of J2SE/Java SE or J2EE/Java EE, for example.
>>> You can feel free to say whatever you want, but lets be realistic: the
>>> majority of people will say "Java has" for stuff in the JCL, and "Java
>>> doesn't have" for stuff not in the JCL. And I am 99% certain that if I had
>>> asked you any of the examples above, you would have said "No, Java doesn't
>>> have a val data type", and you would have says "Yes, Java has a hash map,
>>> it's java.util.HashMap".

>
>>> You can be pedantic all you want, but I would argue that "stuff in the JCL"
>>> is the definition used by the vast majority of people and really the only
>>> one that makes much sense.

>> [ SNIP ]

>
>> I'm probably not most people. I can do very little of my professional
>> Java work using just the Java language and only official Java platform
>> libraries. So I value 3rd party offerings very highly.

>
> Using a lot of JavaEE, I'm right there with you. But I wouldn't say
> RestEASY or Jackson are part of Java any more than I would say Firefox is
> part of Windows.
>
>> As for the questions, what are Java primitives if not value types? And I

>
> val data types != value types, a val type is a variable type that can be
> used like:
>
> for (val file : fileList) { ... // val is of type File here }
>
> Basically an inferred type.


OK, I misconstrued your use of the term "val". Sure, like "var" and
"val" in various languages. Yes, we don't have that in Java.

>> very likely wouldn't say that Java has a hash map, honestly; it's too
>> ingrained in me that "Java" is the language only. I think similarly with

>
> So you would say that Java has ArrayMap, but not a hash map. You need to
> pick one side to disagree with me on, it seems
>
> Liebe Gruesse,
> Joerg
>

I used the term "Java" initially because I do recognize that most folks
use it exactly the way you say, even though I don't. But you'll recall
that very quickly after I made my distinction.

So truthfully I'd have to say that Java has neither, not by my usage.

I don't actually beat people up over this, you know. It doesn't
exactly usually come up, someone just asks does anyone know a class or
classes that does such and such, and someone else suggests a library. If
it's 3rd party they'll say so, otherwise they just usually say it's
package such-and-such.

AHS
 
Reply With Quote
 
Arne Vajh?j
Guest
Posts: n/a
 
      04-03-2013
On 4/3/2013 6:41 PM, markspace wrote:
> On 4/3/2013 3:22 PM, Arne Vajh?j wrote:
>
>> The text you quote do not define immutable neither formal nor informal.
>>
>> It refer to the concept.
>>
>> If immutable is defined formally somewhere in the JLS it must be
>> somewhere else.
>>
>> Until we have a ref, then I can't see anything misleading.

>
>
> Are you serious, Arne?


Very.

> Did you read the section I linked to? I didn't
> quote the whole thing, it would be immense. I just quoted one line to
> show that the section did talk about immutability, not to definitively
> establish all the ins-and-outs of immutability in the JLS. There's no
> point in me copying what you can read yourself.


JLS referring to immutability is utterly irrelevant.

The question is whether JLS defines it.

Feel free to quote where it defines it.

Or stop making claims.

> Honestly I'm shocked at your response and I think you're missing the
> point by a wide margin. Are you trying to tell me that final fields are
> not involved in immutability in Java?


I was just pointing out that what you quoted from JLS did not
support your claim that JLS had a formal definition of immutability.

I find it a bit difficult to see why you think that implies
"final fields are not involved in immutability in Java".

Arne


 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      04-03-2013
On Wed, 03 Apr 2013 16:44:45 -0700, markspace wrote:

> On 4/3/2013 4:25 PM, Joerg Meier wrote:
>> Yours would be immutable with or without the final keyword.

> No no no. This is my point. The final keyword has special semantics
> associated with it in that particular case. It works a bit like the
> volatile keyword: all writes to that point are made visible. In the
> case of a private final field, the writes are made visible to ALL
> THREADS in the system. THAT is what makes instances of the class
> Stooges immutable.


Now I'm to being confused by you. final prevents any writes other than the
initial one. That initial write is not synchronized to other threads. If
you let an object instance get out that isn't fully constructed, then you
will get the usual synchornization issues, final or not. Don't believe me ?

Guess what this will print out:

public class FinalTest {
public static class Test {
public final String bla;

public Test() {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(200);
} catch (final InterruptedException e) {}
System.out.println(bla);
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {}
System.out.println(bla);
}
}).start();
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {}
bla = "1234";
}
}

public static void main(final String[] args) {
final Test test = new Test();
}
}

If I misunderstood, and you believe that structural changes to the
ArrayList would be visible to all threads, then you are still wrong, but
you're going to have to write this test case as I restrict myself to one
test case per post

> That's why no synchronization is needed. Which is huge, conceptually.
> I'll stop there because I think this may be the whole point of
> misunderstanding here.


The mere use of final does not remove the need for synchronization. Nor
does the mere lack of it dictate a need. The reason synchronization is not
needed with proper immutability is an effect from what final does - because
it can only be assigned once, once it is you can then let everyone play
with it, because you dont NEED to worry about writes - there will be none.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-03-2013
On 4/3/2013 4:56 PM, Arne Vajh?j wrote:
>
> I was just pointing out that what you quoted from JLS did not
> support your claim that JLS had a formal definition of immutability.
>


But I don't care if you believe. The link is for your sake.

> I find it a bit difficult to see why you think that implies
> "final fields are not involved in immutability in Java".


Er, I'm saying the opposite.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting into DB table with date from Generic List =?Utf-8?B?U3Jpbmk=?= ASP .Net 0 11-07-2006 04:33 AM
inserting into a list John Salerno Python 15 03-08-2006 06:02 AM
Linked list inserting items from two different funcion Kay C++ 1 09-03-2004 03:48 PM
Urgent:Inserting records from datagrid through drop down list Muhammad Usman ASP .Net 1 10-16-2003 01:39 PM
Inserting an empty element in a list (STL) Massimiliano Alberti C++ 3 09-24-2003 05:48 PM



Advertisments