Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Number of objects created ?/

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In the below code, Can anyone tell me the number of objects being created?/

******************************
public class NumberOfObjects

{
static int a=10;
static int b=20;

public static void add()
{
System.out.println(a+b);
}
public static void main(String args[])
{
add();
}

}

*******************************

Thanks.
[ February 06, 2008: Message edited by: Arjun Reddy ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zero objects created
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would think "String args[]" counts
The answer should be 1 then..

And perhaps the question also gives you the actual command used to execute this ? Maybe there are some other arguments passed to the "main"

Regards,
Alex
[ February 06, 2008: Message edited by: Alex Belisle Turcot ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think zero(0) because there is no object created using "new".
Your example just calls a static method from main and prints the static variables.

Regards,
Siphiwe Madi
SCJP 1.5 (preparing)
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

I would think "String args[]" counts
The answer should be 1 then..



Only if there are argurments passed when the command "java NumberOfObjects" is executed, then the String array Object would be created.

e.g
java NumberOfObjects "Hello"

Regards,
Siphiwe Madi
SCJP 1.5 (aspirant)
[ February 07, 2008: Message edited by: Siphiwe Madi ]
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Siphiwe Madi:
Hi Alex,



Only if there are argurments passed when the command "java NumberOfObjects" is executed, then the String array Object would be created.

e.g
java NumberOfObjects "Hello"

Regards,
Siphiwe Madi
SCJP 1.5 (aspirant)

[ February 07, 2008: Message edited by: Siphiwe Madi ]



I believe that is not true. Even with no argument, you'll still have an "Array" object. Empty.

Regards,
Alex
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody please clarify
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, The number of objects getting created is 1 right?/

Thanks.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can only create objects in Java using the word "new". Other than that, there's no other way. Do the experts agree?
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, the answer is zero objects created. The args array was already created prior to calling the main() method.

If you really want to count that, then you should also count the system properties, the threads objects for the system threads, the objects created for the environment variables, etc. etc. etc.

Henry
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I'm willing to agree both ways actually, I wouldn't know what to answer..
Perhaps the actual question (if this is from a test) clarifies the scope ?

Regards,
Alex
[ February 08, 2008: Message edited by: Alex Belisle Turcot ]
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, I just got what you all being saying... the "Array" object is not created there, the code just receives the reference to it..

So, I would definitly say 0 object created there. Yes.. Sorry for all the confision I may I cause.. :roll:

Regards,
Alex
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.. for all ya answers . It's not a question from any book. I wish it was so that there can be more explanation on it. But I guess the guys have already cleared it.

Thanks.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no object creation in this program
So ans =0
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on whether objects not explicitly created count as "objects being created". As Henry pointed out, many objects are created in the running of any program. This particular code creates a char[] and a String in the Integer.toString method as a result of the println call.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code:



Doesn't this create a string object ?
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
It depends on whether objects not explicitly created count as "objects being created". As Henry pointed out, many objects are created in the running of any program. This particular code creates a char[] and a String in the Integer.toString method as a result of the println call.




Wow... I totally forgot about the Integer class !!

To continue (beating on the dead horse of the previous tangent), if this println() method call caused the loading of the Integer class, then this will cause many objects to be created -- including the preloading of the integer cache. By itself, this will account for 256 objects being created...

Henry
[ February 10, 2008: Message edited by: Henry Wong ]
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf/Henry,

I don't see any instance of Integer wrapper class being used in the code. Could you please further elaborate your explanation?



Thanks!
[ February 10, 2008: Message edited by: Jart Bo ]
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I get it, a & b get's autoboxed and System.out.println() has an implicit call to any object's toString() method.
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Fellow Ranchers and Experts,

For the exam, what type of questions should we expect to encounter regarding this topic of objects created? Only objects created explicitly by the program?

[ February 10, 2008: Message edited by: Jart Bo ]
[ February 10, 2008: Message edited by: Jart Bo ]
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic