Things you disagree with in the java language

Things that are bothering me today:

  • “For each” loops expect an Iterable instead of an Iterator or Iterable object.
  • There is no way to group primitives similar to a C struct type.
  • Iterators can’t return structs because structs don’t exist.
  • Iterators can’t return structs or primatives because Generics only works for Objects.
  • There is no way for Iterators to return multiple values (as in x-y coordinates, key/value pairs, etc) without creating wrapper classes.
  • There are actual bugs (fruit flies?) crawling on my screen while I code, but only when my IDE is the front window… >:(

I guess I’m kinda new to programming to be asking this but wouldn’t a class which has no methods and only data fields be the same functionality-wise as a struct? or am I missing something that java does with classes that just isn’t what you want from a struct ???

We don’t have structs. I suppose enums are somewhat similar, but no methods and stuff in enums…

“Struct” in java-land tends to mean a stack-allocated value type. I don’t see what they have to do with iterators at all. Lack of tuple types is annoying, again not really related to iteration…

It’s a touch annoying there’s no syntax for iterating over an existing Iterator, but I can see the reasoning. Even more annoying is that you can’t get at the Iterator instance in foreach syntax so it’s useless if you want to use remove()

I’d say that structs are (hopefully) about designing data-structures in linear memory. There’s been some talk about adding some tuple like structures.

Interesting questions :slight_smile:

struct, same as class - only with differed default access :wink:
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/cplr054.htm

Do you really need them ?:slight_smile:

Java classes != C++ classes. Adding “structs” without changing classes would add a lot to Java, but currently you cannot use classes like (value type) structs/tuples.


Point p = new Point(123, 456);
Map<Point, String> map = new HashMap<Point, String>();
map.put(p, "A");
Point p2 = p.copy();
p.x *= -1;
p.y *= -1;
System.out.println(map.get(p)); // null
System.out.println(map.get(p2)); // null

[tr][td][/td][td]Java[/td][td]C++[/td][/tr]
[tr][td]Primitives[/td] [td]Val[/td] [td]Val[/td][/tr]
[tr][td]Structs[/td] [td]NA[/td] [td]Val[/td][/tr]
[tr][td]Classes[/td] [td]Ref[/td] [td]Val[/td][/tr]
[tr][td]Arrays[/td] [td]Ref[/td] [td]Val/
Ptr[/td][/tr]

Even in-house engineers working on Java acknowledge that adding structs is one of the things needed to close the performance gap between dynamically vs. statically compiled languages.

First one should print “A” and the second one should be null…unless I’m blind :persecutioncomplex:

Inserting an object into a hashmap and then changing its hashcode is fairly likely (although not certain) to make get fail.

The ‘p’ instance inside the HashMap is the same instance as the ‘p’ you’re passing in, no matter how different the variables inside ‘p’ are :wink:

‘p2’ fails because it’s a different instance.

This is true but irrelevant. Actually in this case (substituting

p.getLocation()

, which exists, for

p.copy()

, which doesn’t) it happens to output A, but that’s because

java.awt.geom.Point2D.hashCode()

is implemented (roughly speaking) as an xor of the two parameters; remove the line

p.y *= -1

and test it and you’ll see that it fails.

I was working with a hypothetical class to illustrate one of the problems with using reference types exclusively. I assumed it overrode hashcode and equals, but even if it didn’t, then you would still have other problems with assuming objects were the same as structs. (Although if I were designing a class I would not override equals or hashcode for a mutable Object.) Emulating by value assignment for groups of values using reference types means you either have to make the object immutable (which will require excessive copying, memory management overhead, extra code, and extra memory usage) or that you have to defensive copies of any value you rely on (which has all the same problems.)

I don’t know if Java counts as being dynamically compiled. The distinction doesn’t even exist now. We now have C and C++ interpreters, Janino, GCJ, etc. What matters most is static typing. Java could use structs to a much greater advantage than dynamically typed languages.

thanks everyone for explaining this for me. I appreciate it as I try to get better.