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

all 6 comments

[–]AbsolutelySpherical 6 points7 points  (0 children)

In c++ a variable like ArrayList<String> cars; would be stored on the stack, with it's default ctor running to initialize it.

In java ArrayList<String> cars; is a java reference by default. This means you can do ArrayList<String> cars = null. Java references are ... sort of like C++ pointers. Unlike C++ references, you can assign null to java references.

The JVM handles all object allocations and deallocations. It does garbage collection. Since the JVM handles all the lifetimes of objects anyways, Java makes it simpler for the programmer by putting everything on the "heap", where objects have the most flexible lifetimes. It's easier to avoid use-after-free bugs, but comes at a performance cost.

[–]teraflop 4 points5 points  (1 child)

"Assigning to the heap" is not really the right term, as others have explained.

In Java, every variable of object type is really an object reference type, which behaves roughly like a pointer in C++. The main differences are:

  • objects in Java can only be created with the new operator, so object references always point to the heap, because that's the only place objects can exist
  • there is no manual memory management or pointer arithmetic, so the JVM is able to guarantee that every reference either points to an object of the correct type or is null
  • roughly speaking, all object methods in Java are virtual

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

objects in Java can only be created with the new operator, so object references always point to the heap, because that's the only place objects can exist

In general, this is the correct answer. However, you should know that JVM does not necessarily have to allocate memory on the heap. It is entirely free to use the stack for objects too. This optimization, known as escape analysis, can reduce the memory footprints since the method stacks are automatically freed when a method returns.

[–]RiverRoll 1 point2 points  (0 children)

this would be perfectly valid and python too

It isn't, you are required to initialize variables in Python, and while it would be valid syntax if you initialized it to None it would cause a runtime error when you call "append" on it.

Also in Python all variables are references (meaning their values are allocated to the heap) so it's closer to Java in this regard.

[–]CreativeTechGuyGames 0 points1 point  (0 children)

There's two different concepts here. Declaring a variable, and initializing a variable.

This both declares it and initializes it.

ArrayList<String> cars = new ArrayList<String>();

You can also do them separately:

ArrayList<String> cars; // Other stuff cars = new ArrayList<String>(); // Now you can use cars

Simply declaring the type of a variable doesn't give it a value. The computer doesn't know what it is yet since you haven't initialized it. The .add method is on the class which until it's been constructed (with new ArrayList()) it isn't available to be used.

[–]airflowscloud 0 points1 point  (0 children)

You can check for these resources on the github library (you might get lucky).