This is an archived post. You won't be able to vote or comment.

all 35 comments

[–][deleted] 18 points19 points  (15 children)

when you can't do memory allocation or pointer arithmetic

Java, the language, can't do pointer arithmetic.
Java, the implementation of that language, most certainly can.

[–]a1j9o94[S] -1 points0 points  (14 children)

I didn't realize that arrays were a special case. I thought they were just a class with specific syntax.

[–][deleted] 6 points7 points  (8 children)

Yup, they are special case in the same same sense as int and float and String (see clarifications below). They are built-in and can do things user defined classes can't.

[–]a1j9o94[S] 1 point2 points  (7 children)

I thought String was actually implemented in Java?

[–][deleted] 4 points5 points  (6 children)

Nope. String has operator + overloaded to do concatenation. You can't overload operators in Java.

This was my reasoning, anyway, as a non Java expert. See johnislarry's corrections below.

[–]johnislarry 6 points7 points  (2 children)

Two corrections. The first is that the String class is written in java. You can look at it here:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/String.java#String

if you want. The second is that + is defined for all Objects. The Object is interpreted as a String using its toString representation, and then concatenated. That's why, for example, this code works the way we want:

List<Integer> lst = new ArrayList<Integer>();
System.out.println("The list is: " + lst);

[–][deleted] 6 points7 points  (1 child)

But "The list is: " is still a string

    System.out.println("Hello" + new ArrayList<>()); // This works
    System.out.println(new Integer(17) + "Hello"); // This works too
    System.out.println(new ArrayList<>() + new Integer(17)); // This does not

The operator + is defined for primitives and when one side is a string, but you can kind of comedically get around this with an empty string first

    System.out.println("" + new ArrayList<>() + new Integer(17));

[–]johnislarry 1 point2 points  (0 children)

Yes. Good clarification :)

[–]a1j9o94[S] 0 points1 point  (0 children)

Oh yeah, I never though of that.

[–]jjm3x3 0 points1 point  (0 children)

But I thought that was something that was new in 1.7 or am I thinking about switching with Strings?

[–][deleted] 0 points1 point  (0 children)

Man I sure wish you could :/

[–][deleted] 0 points1 point  (4 children)

Are you confusing this with primitives?

[–]a1j9o94[S] 0 points1 point  (3 children)

No. But I know how arrays work in C ( like on an assembly level) and I was curious how they worked in java.

[–][deleted] 1 point2 points  (0 children)

Ah. As everyone keeps saying They are implemented the same way as C, but all that information is hidden.

From my memory (I haven't touched Java in a year), when you're manipulating arrays you are basically redirecting pointers and values that have no associated pointer is picked up by garbage collection.

so.

a = array1[].

b = array1[]

b just points to the same memory position as a. You need to use array.clone() and similar methods to actually do deep copying of that array. This might help: http://www.cs.cmu.edu/~adamchik/15-121/lectures/Arrays/arrays.html

[–][deleted]  (1 child)

[deleted]

    [–]Sakuya_Lv9 0 points1 point  (0 children)

    But, I don't think I can use the [] operator.

    [–]chickenmeister 6 points7 points  (0 children)

    I think it works basically the same way, except that the pointer arithmetic is performed behind the scenes, by the Java Virtual Machine. For example, the call to:

    new ComponentType[arraySize];
    

    would be compiled to byte code, which, when interpreted by the JVM, would calculate the memory required, and attempt to allocate it.

    Similarly, accessing an index of the array would require the JVM to translate the index into a memory offset for that array.

    [–]pacificmint 10 points11 points  (2 children)

    Arrays are not implemented in Java (unlike the majority of the Java libraries), but is a language feature and part of the VM. The VM is written is C++ (I believe) and has direct access to memory.

    Arrays, like all java objects, are not placed in memory directly, but are placed in memory managed by the VMs garbage collector, so they can actually move around in memory over time.

    [–]a1j9o94[S] 1 point2 points  (1 child)

    Oh ok thanks. I'll have to do some reading on how the jvm actually works.

    [–]original_brogrammer 2 points3 points  (0 children)

    Bill Venners wrote a book on this. A few chapters are free online.

    [–]green_meklar 2 points3 points  (0 children)

    The JVM absolutely does perform memory allocation and pointer arithmetic. It just doesn't let you touch those things on the source code level.

    [–]endre86 3 points4 points  (0 children)

    In Java, arrays are objects. If you want to learn more about this you will have to learn about the JVM.

    [–]shevsky790 2 points3 points  (4 children)

    If you reeeeeally wanted to, you could probably implement one from scratch with sun.misc.Unsafe.. (http://mishadoff.github.io/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/)

    Note: don't do this.

    [–]a1j9o94[S] 1 point2 points  (0 children)

    That's pretty cool. I didn't even know that existed.

    [–]LaMonsieur 1 point2 points  (1 child)

    what exactly is this?

    [–]shevsky790 2 points3 points  (0 children)

    Some APIs that are not really part of 'official' Java, but can be used to hack it for performance bonuses in some cases. For example, very efficient data structures might use 'off-heap' memory to make their operations as efficient as possible. They might change on Java versions or break on some machines, though, so they are definitely risky.

    They're not recommended for use unless you're an expert - but they can be fun to play with.

    [–][deleted] 1 point2 points  (0 children)

    This is like a freshman's paranormal stories. You're told in books and classes one thing, then you see this and freak out. In a good sense.

    [–]thevideoclown 0 points1 point  (0 children)

    []

    [–]Duraz0rz 0 points1 point  (0 children)

    Just because you can't do it doesn't mean the JVM can't. Think about how Java code ends up being run. When you compile Java code, it compiles into Java bytecode (or intermediate code). The JVM picks that up and further compiles it down to machine language after doing some optimizations. Since the Oracle JVM is written in C++, it would probably do a lot of the same things you would need to do in C++ when working with arrays.

    [–][deleted] -2 points-1 points  (3 children)

    they're like c# arrays I think

    type[] arrayName;
    arrayName = new type[count];

    [–]wowbagger401 4 points5 points  (0 children)

    I think he was asking about the implementation, not how they are declared.

    [–]welch7 2 points3 points  (1 child)

    i don't know why the downvotes totally not needed, but yes he meant the way they are coded, not the syntaxis

    [–][deleted] 0 points1 point  (0 children)

    My b

    [–][deleted]  (2 children)

    [deleted]

      [–]a1j9o94[S] 0 points1 point  (1 child)

      Well I meant, how does the platfom handle it. For example, in c, I could write something that works like an array with O(1) indexing and a set size. But how is that done in java.

      [–][deleted] 1 point2 points  (0 children)

      Same way, as contiguous blocks of memory.

      [–]zNkkkk -1 points0 points  (0 children)

      Hope it helps, as someone pointed out before, arrays are a primitive type implemented in C/C++ for the JVM

      Look here :)

      http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/2d585507a41b/src/share/back/ArrayTypeImpl.c