• 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

Number of created objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:

How many objects do we have when reaching the comment "No of objects"?

In simple OCA tests the answer is one.

But in my opinion, the objects (OK, references to objects) are two: args and t.

So what's true? Which answer should we give if someone asked this question? Doesn't args count as an individual object? Or am I missing something?



 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
args does not have an instance. Thus, not an object.
 
George Charitonidis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but... they are both declared locally. If we have Object o, instead of Object o = new Object(), then:

args instanceof Object > true

o instanceof Object > compiler error: not initialized

So in what way is args different than Object o? Are method parameters treated somehow differently? Isn't args initialized (as an Object) when main() is called?
 
Saloon Keeper
Posts: 15529
364
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The args array certainly is an object.

I usually find these questions kind of silly because they don't specify what you should count as objects and what not. For instance, calling the main method may actually create a lot of objects behind the scenes.

Usually you should take these questions to only refer to the body of the method, and only other visible code that is called from the main method and given in the question.

I this case you should only count the new instance of Test.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I concur with Stephan on this, both on how you should take questions like this and the silliness of it in the first place.

Technically, you're right, you can count two objects referenced by that code: the Test object that is instantiated and referenced by the variable t and the array of Strings referenced by the args parameter.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get the compiler error (not initialized) if you try to use a local variable that the compiler can determine as not having been assigned to yet, i.e., uninitialized. Parameters are slightly different since the compiler can assume that it has been assigned something, even null. With the local variable declared but not initialized, it won't have any value assigned to it by default, not even null so you have to initialize it before you use it.

Try it with Object o = null.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Technically, you're right . . . .

. . . until some wally marks you down for a correct analysis of the situation.

The main() method's args parameter is never null.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The main() method's args parameter is never null.


Under typical conditions, no, it would not be null. There's nothing preventing an explicit call to the public main() method with null as the parameter though, so technically , it could happen that the main() method args parameter is null.

I was, however, speaking in general terms about parameters being initialized to something. A method call won't even compile if you don't have the correct number of arguments, even if the values you pass in are just null.  As such, the compiler can pretty much assume that a parameter will be always be initialized to something.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . . There's nothing preventing an explicit call to the public main() method with null as the parameter though

I forgot that I pass null often enough when I use JShell.

. . . the compiler can pretty much assume that a parameter will be always be initialized to something.

Yes, I think the JLS (Java® Language Specification) says that parameters are always definitely assigned to. This part of the JLS.
 
reply
    Bookmark Topic Watch Topic
  • New Topic