• Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntactic Sugar?

 
Michael Stephenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Maurice Naftalin, My question is: Are Lambda expressions just another syntactic sugar recipe to overcome Java's lack of direct pointer support?


My reasoning:
Procedural languages support pointers to the data and executable segments in memory. C++ also implements pointers, but that is just a side effect of the underlying C language. We know that an OOP language is a runtime coupled with a compiler that implements the concepts of objects expressed as code syntax, but in the end all code is compiled, bound and linked into machine language instructions. Why does Java Community feel the need to implement every logical concept of pointer support as an abstract concept. I say keep the Java language simple and pure and do not try to implement the Digital Computer World with a single tool/language because it will be Javas undoing, what say you?

From the Java Tutorial:

"Ideal Use Case for Lambda Expressions

Approach 1: Create Methods That Search for Members That Match One Characteristic
Approach 2: Create More Generalized Search Methods
Approach 3: Specify Search Criteria Code in a Local Class
Approach 4: Specify Search Criteria Code in an Anonymous Class
Approach 5: Specify Search Criteria Code with a Lambda Expression
Approach 6: Use Standard Functional Interfaces with Lambda Expressions
Approach 7: Use Lambda Expressions Throughout Your Application
Approach 8: Use Generics More Extensively
Approach 9: Use Aggregate Operations That Accept Lambda Expressions as Parameters

..."
Can we get anymore abstract by the use of indirection in Java. Please tell me if I am off base, and need to reflect on my erroneous ways.

Michael Stephenson
Shawnee, Kansas

 
Campbell Ritchie
Marshal
Posts: 52557
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have taken the liberty of creating a new thread from your question because otherwise you would not be eligible for the book draw.

No, a λ is not syntactic sugar for anything. Consider the following code in a Swing app:-If you compile such code you will find the .class file for that anonymous class called Something$123.class. If you remember that action listener is a functional interface, you can convert that to a λBut there is no longer the anonymous class and the $123.class file will no longer be needed.

There was a discussion about a very similar topic recently, here.
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything in that list of tutorial chapters which has anything to do with direct pointer access -- although I'm not an expert on lambda expressions so perhaps you could point out (sorry!) where I'm missing the relationship.

As for keeping Java simple, it seems to me that there are a lot of situations where you can replace a big lump of "simple" code with loops and comparisons and arithmetic with a small piece of code which uses functional programming ideas and which is much more readable.

Or did you mean to imply that the Java designers have "pointer envy" and are reacting by implementing new features to distract the masses from the fact that Java doesn't allow you to access pointers directly? That's an interesting theory, sounds something like Freud would have made up.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Stephenson wrote: Hello Maurice Naftalin, My question is: Are Lambda expressions just another syntactic sugar recipe to overcome Java's lack of direct pointer support?




Lack of pointers: Except for primitive types, java uses exclusively pointers. Reference is a just a fancy name for pointer. Except for primitive types, there is only one way to pass arguments with java: by reference (e.g. using pointers). There is no way to pass an argument by value in java like it is the case in C. Funnily enough, my C++ teacher wanted us to pass everything by value, copying large objects on the stack at every method call and he had a pretty hard time to get on board with java.

Syntactic sugar: Most probably, in the last 10 years, Java consistently added support for constructs supported in other languages (ml, php, pick your own language) and I assume it was to make complaining people happy and that this can be viewed as some kind of marketing strategy.

I don't think Lambda expressions and other syntactic sugar of the past 10 years were needed that much. In fact they are so slow sometimes that they manage to make java as slow as some interpreted languages, thus making a broader range of programmers feel home.

Anyway this is just my humble opinion. I am ducking in advance given other posts I have read on JavaRanch ;-))

https://en.wikipedia.org/wiki/Syntactic_sugar#Criticism



 
Campbell Ritchie
Marshal
Posts: 52557
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief!

No, there is no such thing as pass by reference in Java®. What you are passing is the value of the pointer behind the reference, but as you imply there is no way to see that pointer. Actually it is probably a handle rather than a straight pointer.
No, as I have shown a λ is not syntactic sugar; although the result of execution is the same but the effect on program state (for the javac program) is very different. The idea of syntactic sugar is to write a simpler means of writing the same thing. The for‑each loop
for (String s : myArray) ...
is syntactic sugar forThe two would have the same output and at least I think create the same output from the javac program. Since a λ creates different output and may even create a different number of files, you cannot regard it as syntactic sugar.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Good grief!

No, there is no such thing as pass by reference in Java®. What you are passing is the value of the pointer behind the reference, but as you imply there is no way to see that pointer. Actually it is probably a handle rather than a straight pointer.
No, as I have shown a λ is not syntactic sugar; although the result of execution is the same but the effect on program state (for the javac program) is very different. The idea of syntactic sugar is to write a simpler means of writing the same thing. The for‑each loop
for (String s : myArray) ...
is syntactic sugar forThe two would have the same output and at least I think create the same output from the javac program. Since a λ creates different output and may even create a different number of files, you cannot regard it as syntactic sugar.


Hi Campbell,

I have have done triple indirection in assembly language back then because computers were so slow. Nowadays, we have fast computers but it seems that there is some kind of conspiracy going on where we need to make them go slower ;-)

https://en.wikipedia.org/wiki/Indirection

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15713
73
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Stephenson wrote:Are Lambda expressions just another syntactic sugar recipe to overcome Java's lack of direct pointer support?

Lambda expressions do not have anything to do with pointers, and the reason that they exist in Java has nothing to do with Java's lack of direct pointers.

Note that C++ also has lambda expressions (and also pointers, ofcourse).
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Michael Stephenson wrote:Are Lambda expressions just another syntactic sugar recipe to overcome Java's lack of direct pointer support?

Lambda expressions do not have anything to do with pointers, and the reason that they exist in Java has nothing to do with Java's lack of direct pointers.

Note that C++ also has lambda expressions (and also pointers, of course).


I fully agree. IMHO, lambda expressions shouldn't slow things down as much as a Scanner would as a completely random counter-example.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Except for primitive types, java uses exclusively pointers. Reference is a just a fancy name for pointer...

Ooof. No it ain't - except in the general sense that it tells the JVM where to find an object - although I'm pretty sure that the spec doesn't actually tell designers how it does that.
  • It doesn't allow arithmetic.
  • It doesn't allow access to raw memory.
  • It is not an alternative to some other form of access.
  • It cannot be cast.

  • What may be true is that many JVMs implement it as a pointer - although it's equally possible that it's actually a '**' -

    Winston
     
    A.J. Côté
    Ranch Hand
    Posts: 417
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    A.J. Côté wrote:Except for primitive types, java uses exclusively pointers. Reference is a just a fancy name for pointer...

    Ooof. No it ain't - except in the general sense that it tells the JVM where to find an object - although I'm pretty sure that the spec doesn't actually tell designers how it does that.
  • It doesn't allow arithmetic.
  • It doesn't allow access to raw memory.
  • It is not an alternative to some other form of access.
  • It cannot be cast.

  • What may be true is that many JVMs implement it as a pointer - although it's equally possible that it's actually a '**' -

    Winston


    Do you have any assembly experience?

    Otherwise, how can you look towards the inside?





     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A.J. Côté wrote:Do you have any assembly experience?

    Nope. But I do have several years of C, and a bit of C++; and I believe that parts of some major JVMs are still written in C.

    Otherwise, how can you look towards the inside?

    I don't understand the question. I simply said that a Java reference is not a pointer - except in the most general sense of the word - and I stand by it.
    Syntactically, it is completely different; and AFAIR, the JVM spec makes no stipulation as to how it must be implemented.

    Winston
     
    A.J. Côté
    Ranch Hand
    Posts: 417
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Nope. But I do have several years of C, and a bit of C++; and I believe that parts of some major JVMs are still written in C.

    Indeed it is. Even in assembly when we need to shave off mili-seconds. This ain't no big secret.

    https://en.wikipedia.org/wiki/Assembly_language
     
    Campbell Ritchie
    Marshal
    Posts: 52557
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A.J. Côté wrote: . . .
    I have have done triple indirection in assembly language back then because computers were so slow. Nowadays, we have fast computers but it seems that there is some kind of conspiracy going on where we need to make them go slower ;-)
    . . .
    What on earth does that mean? It looks like a non sequitur to me.
     
    A.J. Côté
    Ranch Hand
    Posts: 417
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    [What on earth does that mean? It looks like a non sequitur to me.


    https://en.wikipedia.org/wiki/Assembly_language

    https://en.wikipedia.org/wiki/Indirection

    That's what you have to know to build jvms and hotspots JIT.
     
    Campbell Ritchie
    Marshal
    Posts: 52557
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe, but what has it got to do with the question in this thread, even if OP did use the word indirection?
     
    Stephan van Hulst
    Bartender
    Posts: 6583
    84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A.J. Côté wrote:
    Campbell Ritchie wrote:
    [What on earth does that mean? It looks like a non sequitur to me.


    https://en.wikipedia.org/wiki/Assembly_language

    https://en.wikipedia.org/wiki/Indirection

    That's what you have to know to build jvms and hotspots JIT.


    Campbell called it a non-sequitur because your response had nothing to do with his.
     
    Michael Stephenson
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Good grief!

    No, there is no such thing as pass by reference in Java®. What you are passing is the value of the pointer behind the reference, but as you imply there is no way to see that pointer. Actually it is probably a handle rather than a straight pointer.
    No, as I have shown a λ is not syntactic sugar; although the result of execution is the same but the effect on program state (for the javac program) is very different. The idea of syntactic sugar is to write a simpler means of writing the same thing. The for‑each loop
    ... The two would have the same output and at least I think create the same output from the javac program. Since a λ creates different output and may even create a different number of files, you cannot regard it as syntactic sugar.


    Okay, I am sorry for not being exact in my writing descriptions, and my meaning is lost in the communication. I should have said Java does not support arbitrary data and procedure pointers, and we cannot allocate memory off the heap. Off course we loose out on all the fun of forgetting to deallocate said memory.

    In my mind's eye, I see Lambdas as a procedure pointer that can be passed around in your code. I call it Syntactic Sugar because we could achieve the same thing by passing an Object around that only contains just one overloaded method that implements the desired logic, thus the more concise code of a Lambda expressions is "syntactic sugar"? I get that the underlying byte code is different, but given the same data inputs we get the same desired output as you have said.

    Think of it this way, what is the difference between the C++ compiler and the C compiler? They both produce a working set of machine instructions. Would you say that C++ is just a big candy factory for simplifying object concepts in code coupled with a runtime that supports object instantiation and life cycle events? How would one implement their own business program that uses object oriented principles? How would one implement the concept of an object in C? Perhaps, a union of structures where some of the elements of the structures are procedure pointers all with an implied first parameter that points to the union itself. Each procedure pointer will point to a function that acts on the data stored in the structure pointed to by the first parameter passed to the function. Of course your union should have a uid so that you can reference it out of the tree you are going to add it to once one has allocated some memory of the heap and assigns the pointer to the union variable instance. Don't forget to include some meta-data in the tree so that one can make inferences about the union being pointed to in the tree. Just remember to clean up the memory pointed to in the tree when you are done with it.

    Now one will have to write a whole bunch of methods( I mean functions) that work on that tree, like:


    Okay, now just refactor the code and pull the tree creation and tree logic and put it into a library that any of program can bind to and then you will have a Runtime. Man that was a lot of work, I hope no one steels my idea (LOL).

    Union = Class, union variable = instance, Tree with functions and procedures = Runtime.


    C++ is Syntactic Sugar for Object Oriented Concepts ... Now, will the C++ compiler generate the exact same machine code that my C object concept code would? Off course not.




    Perhaps this is all semantics, but what we share as developers is a semantic model for communicating abstract ideas.
     
    Campbell Ritchie
    Marshal
    Posts: 52557
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is possible to pass a method reference as a λ, but you are not passing a simple pointer to that method.
    You can allocate memory on the heap by creating an object, but you don't know how much memory nor where it is. All these things are abstracted away from you. The heap is automated so you don't have to mess around with garbage collection.

    There is no such things as the “C compiler”. There are lots of different compilers, and C is less strictly defined than Java® so they may differ in their behaviour. I don't think I have enough time to discuss simulating object‑orientation in C, but it can be done with pointers to structs sent to functions. It is rather similar to what you suggest.
     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Michael Stephenson wrote:Okay, I am sorry for not being exact in my writing descriptions, and my meaning is lost in the communication. I should have said Java does not support arbitrary data and procedure pointers,

    And I'd say that it doesn't support pointers at all. Indeed, it was one of the founding precepts, if memory serves, to avoid all the problems associated with arbitrary "memory" pointers. There is no pointer arithmetic, no "address of" (&) operator, and no way to create a union (which I'd say is certainly syntactic sugar).

    Think of it this way, what is the difference between the C++ compiler and the C compiler? They both produce a working set of machine instructions. Would you say that C++ is just a big candy factory for simplifying object concepts in code

    I'm pretty sure that's how Bjarne saw it. I remember being quite surprised when I read about how surprised he was when the the whole C++ explosion happened.

    coupled with a runtime that supports object instantiation and life cycle events?

    Runtime? There isn't a "runtime" in C++ (at least there wasn't when I was using it); it's a classical CLG language. Of course it's many moons since I used it, so things may have changed...

    How would one implement the concept of an object in C? Perhaps, a union of structures where some of the elements of the structures are procedure pointers all with an implied first parameter that points to the union itself.

    Simple answer: Dunno, and never had to worry about it. All I ever remember it being referred to was as a "superset" of C that allowed the definition of classes, but how it does it I have no idea. I'd care to bet that hidden preprocessing and macros are heavily involved, though (about the only thing I miss since I moved from C to Java).

    Perhaps this is all semantics, but what we share as developers is a semantic model for communicating abstract ideas.

    Right, but there's a big leap from the way C and C++ does that to the way Java does it.

    In C/C++, because of its direct correlation to machine code, you have vast libraries of header files with gobs of repeated code to cover the basic differences between Intel and Motorola architecture; and also to deal with syntactic differences between operating systems.
    In Java you don't; you have a VM that hides all that "translation" from you, and the closest "primitive" you get is an int, which is is a 32-bit signed, big-endian integer, regardless of what machine or OS it's run on. Therefore, to try and make comparisons between the two, or to try and define how a particular aspect (like a reference) is implemented, in terms of the other, is kind of pointless.

    All I can say is that I'm a true convert. I'm glad I don't have to worry about that stuff any more and, in the words of Blackadder: if I never see another make file as long as I live, it'll be fifty billion years too soon.

    Winston
     
    Michael Stephenson
    Greenhorn
    Posts: 3
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [quote=Campbell Ritchie
    There is no such things as the “C compiler”. There are lots of different compilers, and C is less strictly defined than Java® so they may differ in their behaviour. I don't think I have enough time to discuss simulating object‑orientation in C, but it can be done with pointers to structs sent to functions. It is rather similar to what you suggest.


    Okay, I don't know you and you may not really be a rude person or someone that feels superior to everyone else. But, when one parse a phrase such as 'the C compiler' as opposed to writing it as the more correct and general phrase way as in 'a C compiler' or when one begin a comment with "Good grief", or, tell a poster that they don't have time to ponder a computer science concept-and I can tell that you didn't have time because of the mythical 'simulated object' comment (Btw what is a simulated object vas a non-simulated one?), then that person may be a rude person.
     
    Paul Clapham
    Sheriff
    Posts: 21876
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Michael Stephenson wrote:In my mind's eye, I see Lambdas as a procedure pointer that can be passed around in your code. I call it Syntactic Sugar because we could achieve the same thing by passing an Object around that only contains just one overloaded method that implements the desired logic, thus the more concise code of a Lambda expressions is "syntactic sugar"?


    It's true that you can create a method reference with a Java lambda expression, and it's often true that you can produce a more verbose way of doing that by using an object of an anonymous inner class.

    Tutorial about that aspect of lambdas here: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html ... the previous page from that tutorial is the page from which you copied your nine points in your OP, so we're all on the same page, it looks like.

    Anyway to me it doesn't look like lambdas are implementing "every logical concept of pointer support", based on that. There's a whole lot more things you can do to mess with pointers which aren't covered here, and I'm not even sure that method references are exposing much that wasn't already exposed. Probably under the covers something similar to anonymous inner classes is used to support method references, but I don't want to know how things work under the covers.

    As for keeping Java "simple and pure", there's a name for languages which don't incorporate improvements over the time: "Dead". People in the computing field are suspicious of such things and attach pejorative names such as "legacy" to them, regardless of how useful they are. So "Java's undoing"? I say no.



     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:As for keeping Java "simple and pure", there's a name for languages which don't incorporate improvements over the time: "Dead".
    People in the computing field are suspicious of such things and attach pejorative names such as "legacy" to them, regardless of how useful they are. So "Java's undoing"? I say no.

    Couldn't agree more. Well said.

    I have to admit to being woefully behind on my "v8 stuff", but I really like the look of some of it - like streams - probably because they remind me of pipes in Unix, which are incredibly useful.

    About the only criticism I have of lambdas right now is that they "look" weird - but I suspect it's due to my lack of familiarity with functional syntax rather than any fault of the designers.

    Winston
     
    Maurice Naftalin
    Author
    Greenhorn
    Posts: 20
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Michael

    I'm not going to reply to most of the posts in this thread, as the discussion seems to have strayed quite a long way off topic. But I have some agreement with the commenters who took issue with your linking of lambdas with pointer support. It might be more useful to think in terms of functions and their implementation. A lot of experience with languages having functions as first-class citizens shows that this is a very useful abstraction, which leads to more natural solutions to a lot of problems and much more concise and readable code. In C, functions can be implemented using pointers, but that isn't possible in Java, for reasons given in the thread. (It's not a defect in Java that you can't use pointers for functions, by the way!)

    In one very narrow sense, lambdas do look like syntactic sugar, as you say in your follow-up post: anything you can write with a lambda can be written using anonymous inner class – much more clumsily. It's not syntactic sugar, because the implementation is quite different and potentially much more efficient. For the programmer, though, the important points are: the lambda form focuses your attention on the fact that you are using a function rather than an object; and the conciseness of the notation enables us to write stream code (for example), using many lambdas, where the equivalent with objects (ie anonymous inner classes) would be unreadably verbose.

    Regards
    Maurice
     
    Campbell Ritchie
    Marshal
    Posts: 52557
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    … and I have forgotten how you simulate OO in plain C with pointers. I can only remember about half of it.
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:No, a λ is not syntactic sugar for anything...there is no longer the anonymous class and the $123.class file will no longer be needed.

    It looks like the generated bytecode does not tell the full story because a lambda expression can create an object (like an anonymous class).

    According to the JLS

    Evaluation of a method reference expression (§15.13.3) or a lambda expression (§15.27.4) may require that a new instance of a class that implements a functional interface type be created.

     
    Rob Spoor
    Sheriff
    Posts: 20819
    68
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It may, but it doesn't have to. I guess they gave the option to anyone implementing their own JRE on how to do it. Oracle uses dynamicinvoke invokedynamic for most (if not all) cases.

    For instance:
    javap -p -c Test yields this:
    In this case no class was created but an extra method instead (lambda$main$0).
     
    Tim Holloway
    Bartender
    Posts: 18412
    58
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My, this has been a busy thread!

    Might as well dump in my $0.02,

    I got my experiences with lambda expressions in LISP, where they're a critical part of the architecture. Effectively, in core LISP all functions are lambda expressions, and the act of attaching a name (s) to a lambda is a secondary (and optional) construct. So I tend to perceive lambdas as anonymous functions. I haven't gotten deep enough into them in Java to tell yet if I'd amend my perceptions based on Java 8.

    Here's an illustration of the difference between abstract references and concrete pointers. On the original Macintosh OS, memory was fairly tight, but for the first time on a PC, multi-tasking (of a sort) was in the OS, so multiple apps could be running - and demanding memory - concurrently. This tends to get really messy on systems that don't have virtual memory, as all those different demands on memory mean that unless you swap RAM to disk en masse when you swap tasks (fairly slow) or pre-allocate separate heap/stack spaces for each process (potentially wasting lots of RAM), then you're likely to fragment memory resources all over the place.

    To get around that, the Mac used "handles". Handles are indirect pointers. Because they are indirect, the handle remains constant for the life of the allocated memory block, but the OS is free to move the referenced data itself around in order to compact and de-fragment RAM. I'm fairly certain that most JVMs are using some sort of similar scheme, but as others have mentioned, there's no specific mechanism required by the Java spec.

    Handles are still found in certain contexts, but their use as a primary memory management subsystem was pretty much limited to the original MacOS. When the Commodore Amiga came out shortly thereafter, it got most of its memory management functions from Unix. As do the more modern Apple OS's.

    To tie the two concepts together, in LISP, you didn't ordinarily have the slighest clue about what lived where in memory or even if there was memory in the traditional sense. A lambda function was simply another list (or tree) organized in a specific way and processed by the function processor on demand. To name a function, as I said, you had to create a name/value pairing in a LISP environment and that, in turn was a list construct.

    One of the books in my library spends a lot of time making the distinction between data structures and memory structures. A data structure abstractly defines a tuple of data items, which in Java can include other tuples. This is the basis of a Java class. In C, the data structures are more closely identified with storage structures. That is, there may be padding bytes to force memory alignment, the amount of memory required is easily computed (sizeof), items appear in RAM pretty much in the order in which they were defined, and pointers get involved. Java does not support such nuances. It's strictly concerned with data, not storage.
     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tim Holloway wrote:Might as well dump in my $0.02,

    Great explanation. Have a cow.

    My thought was that Java references could actually be '**'s (a pointer to a pointer), or something even more "indirect", but your explanation of handles offers yet another possibility.

    And, at the end of the day, the actual implementation really doesn't matter. To all intents and purposes, a reference is the object, and the only place you have to worry about it being some sort of "pointer" is when we get into those tedious discussions about "pass by value".

    Winston
     
    Stephan van Hulst
    Bartender
    Posts: 6583
    84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:And, at the end of the day, the actual implementation really doesn't matter. To all intents and purposes, a reference is the object, and the only place you have to worry about it being some sort of "pointer" is when we get into those tedious discussions about "pass by value".

    Here I have to disagree. I think it's immensely important to always keep in the back of your mind that variables of a reference type hold references, not objects. I'm always aware of whether I'm working with an object that could suddenly change out of nowhere because bugged or malicious code could mutate the object. I don't even trust my own code, so I'm making defensive copies all over the place. This awareness takes a lot of energy, so that's why I love immutable types so much. They allow you to forget that you're working with references for a while
     
    Campbell Ritchie
    Marshal
    Posts: 52557
    119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    'Ware tangents.

    That is why so many of the new Java8 classes (e.g. LocalDate) are immutable. And remember what Joshua Bloch says in Effective Java™ about defensive copies.
     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:Here I have to disagree...

    It's OK, I knew I'd get some flak for it.

    And you're probably right, especially when it comes to stuff like coding defensively - although my tactic (as it is for most "tricky" stuff) is avoidance.

    Winston
     
    Stephan van Hulst
    Bartender
    Posts: 6583
    84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:although my tactic (as it is for most "tricky" stuff) is avoidance.


    I'm curious what you mean here.
     
    Tim Holloway
    Bartender
    Posts: 18412
    58
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Constants - aren't, variables - won't.

    So goes the old saying.

    Java doesn't allow defining references to "constant" objects the way C++ does. The only way to make sure an object in Java is constant (immutable) is to define its basic semantics as immutable. That means that all fields within the class are declared "final" or making them all only visible via set/get methods with an option to eliminate or restrict the "set" methods. Declaring a class "final" doesn't do it - that only ensures that you cannot subclass it (in case anyone was wondering).

    I use a couple of anti-bugging tricks as well. First, minimize locality of scope. If I only need a variable within a block of code, I declare it within that block as close to its actual use as possible, not by dumping it along with a bunch of other variables at the top of the method definition. Secondly, while I cannot protect the contents of the object, I can at least protect the variable that references the object by declaring it final. That way some other instance of the object cannot be jammed into it by mistakes (or have the variable set to null).

    If I'm really paranoid, I'll also attach audit/log functionality to the "set" methods for critical object properties.
     
    Winston Gutkowski
    Bartender
    Posts: 10571
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:I'm curious what you mean here.

    1. Defensive copying is tricky - prefer Immutable classes.
    2. Synchronisation is tough - see above.
    3. Reflection is arcane - Rule 1: Don't use it. Rule 2: If you absolutely have to, use it in small doses.
    4. GUIs are hard - let someone else write 'em.
    Avoidance. It works about 80% of the time - for the other 20, you just suck it up.

    Winston
     
    Stephan van Hulst
    Bartender
    Posts: 6583
    84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, I wholeheartedly agree with you! Except GUIs... I know you'll declare me crazy, but I somehow love writing them :P
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!