Thread history

From Talk:ScalarNext/Version History
Viewing a history listing
Jump to navigation Jump to search
Time User Activity Comment
05:32, 24 February 2018 Xor (talk | contribs) New reply created (Reply to Java 8 Stack Allocation)
05:28, 24 February 2018 Xor (talk | contribs) Comment text edited  
05:27, 24 February 2018 Xor (talk | contribs) New thread created  

Java 8 Stack Allocation

It seems that when an object is allocated in a function, and never escapes that, Java would replace the allocation to stack if the reference is not modified, which means Java would not reuse previously allocated memory for new objects in this case.

e.g.

void stack() {
  Point2D.Double a = new Point2D.Double(0, 0);
  a.setLocation(1, 1);
}

a could be allocated on stack, while

void heap() {
  Point2D.Double b = new Point2D.Double(0, 0);
  b = new Point2D.Double(1, 1);
}

b can not be allocated on stack.

Xor (talk)05:27, 24 February 2018

Meanwhile, builder functions behaves the same as constructors. e.g.

Point2D.Double origin() {
  return new Point2D.Double(0, 0);
}

void stack() {
  Point2D.Double a = origin();
  a.setLocation(1, 1);
}

behaves the same as previous example.

Xor (talk)05:32, 24 February 2018