• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difference between c++ and java Object creation

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, This question was asked in an interview of mine,

Why it is always necessary to use new operator while creating object in Java, while there is no such a need in c++??



Can anyone tell me the answer
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do we always use new to create objects in Java?

What about

String s1 = "abc";

int i1 = 10;

int [] ar = {1,2,3}

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, I can create new object by these in Java:


No new operatior involved.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the interviewer want to test whether you know the difference between class concept in C++ and Java (but using a bad question). I don't know about C++ much. But as I recall, C++ class is just a compiler construct (someone please correct me if I am wrong). It doesn't exist in runtime. Java class is a real entity existing in runtime - as represented by the Class object for that class.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish Singh wrote:int i1 = 10;


Not an object.
 
Marshal
Posts: 79968
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:

Manish Singh wrote:int i1 = 10;


Not an object.

He must have meant Integer i1 = 10;
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question is better answered from the C++ side, than the Java side -- as from the Java side, you'll just get into small details like the reflextion, autoboxing, etc. etc. etc..... not that it is wrong, but the answer is simpler from the C++ view.

In Java, all variables to objects are references. In C++, you can declare a variable to objects, just like any other type. You can declare a variable of that object type. You can declare a pointer to it. etc. The "new" operator is only needed when you want to allocate (and construct) the object, for assignment to a pointer. If it is not a pointer, then it is not necessary.

Henry
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic