• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors in Java

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic