• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors in Java

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, when we say: new Type(); two things happen:
1. execution of the constructor for the type
2. allocation of required memory on heap.

Does the call to a constructor call any native code, say, for allocating memory?
Is the new operator of Java written in Java or is it actually "new" operator of C++?

Thanks in advance!

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's specified anywhere in the Java Virtual Machine specification, so it's up to the implementer of the JVM to do whatever is best suited for that particular JVM implementation. In other words, there is no general answer to the question.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The memory allocator is a core part of the JVM implementation; "new" is a bytecode instruction (actually, there are several of them for different kinds of object and array allocation) and as such, it's going to be written in the implementation language of the JVM, not Java. If it were written in Java, it would have to be runnning on a higher-level "meta-JVM" which invoked it. IBM had a research JVM written in Java; it of course ran on such a "meta-JVM".

"new" never just maps to the platform-native "new" operator of C++. The JVM needs to manage the heap much more closely than that arrangement would allow. Instead, the JVM grabs huge chunks of memory from the platform, and then divides it up into individual objects by itself.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for your reply.

Seems to me that some native code is being executed on the Virtual machine that I am using.
I was monitoring allocation for my classes in Android. In the logs, I see stack trace of methods called from within the constructor. There are two new ones(2 method calls) that my code did not explicitly make:
1. newInstance()
2. constructNative(). [This one is flagged as a native call as also the name might suggest!]
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No longer a "beginning Java" question. Moving.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The memory allocator is a core part of the JVM implementation; "new" is a bytecode instruction (actually, there are several of them for different kinds of object and array allocation) and as such, it's going to be written in the implementation language of the JVM, not Java. If it were written in Java, it would have to be runnning on a higher-level "meta-JVM" which invoked it. IBM had a research JVM written in Java; it of course ran on such a "meta-JVM".

"new" never just maps to the platform-native "new" operator of C++. The JVM needs to manage the heap much more closely than that arrangement would allow. Instead, the JVM grabs huge chunks of memory from the platform, and then divides it up into individual objects by itself.


EFH: Thank you very much! I never thought about OPCODEs until you mentioned. I thought "new" of Java mapped to "new" of C++.
thanks again!

@CR: IMO, this is still a Java question
I mentioned Android only because Jesper said the behavior was dependent on the implementation of the JVM; Android uses a Dalvik Virtual machine which is different from the JVMs that comes with the JDK.

I got my answer eitherways; thank you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic