• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Objects and References....Constructors and new

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please forgive my ignorance. But I would like to know the intricacies related to memory allocation and construction in java. please explain the relation between construction and memory allocation. also tell about the steps that are taken for an object to get created on the heap. what would happen if i simply assign the fields in an object reference like in case of structures in C. Also tell me the relevance of new operator. include the difference between the actual object content and the reference variable and the confusion scenarios that may crop up when we thinki deep into them......waiting for a reply that would erase all my doubts.....i am sorry if the question went too long
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's all automatic.
And no, you can't do anything with an object that hasn't been created.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sumesh, welcome to javaranch.

That's a lot of questions all at once. How about you pick out just one to start.

Which one would you most like to get help with?


Cheers,
Pauline


Asking Questions 101
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give you an overview from which you can use Google to get more information.
  • The new operator is used to instantiate an object.
  • Instantiation involves allocating memory on the heap, initializing fields, calling the object's constructor, and returning a reference.
  • A reference is just a pointer to the object in memory (like a pointer in C). The object in memory (the instance) holds the field values.

  • what would happen if i simply assign the fields in an object reference like in case of structures in C[?]

    A NullPointerException would be thrown by the JVM (similar to a core dump in C, but you can catch it and take action if you want). You must always set references to a non-null value before using them to access fields or call methods.
     
    Sumesh Kumar T N
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Pleased to hear from you, Pauline. I would go for a single question. I would like to know about everything that happens right from when the new is called till creation of an object...........


    Thanks to everyone who cast light on the subject
     
    Sumesh Kumar T N
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Group one:
    Integer one = new Integer(1);
    Integer two = new Integer(1);

    Group two:
    String one = new String("Hello");
    String two = new String("Hello");

    Please explain the above code for me as to how many objects are created on heap.Is there any difference between the two groups of statements???
     
    Ranch Hand
    Posts: 140
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sumesh,

    The JVM manage the second group in a special way. Look at this thread for details.

    Best regards,
     
    look! it's a bird! it's a plane! It's .... a teeny tiny ad
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic