• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects are created? - String[] args

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'd say 3, but I have a feeling this would be marked incorrectly because the array already exists.

What's the safest answer here?

Thanks,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:What's the safest answer here?

2 objects being created on line2 and line3.

nick woodward wrote:I'd say 3, but I have a feeling this would be marked incorrectly because the array already exists.

That array is indeed already created, args is just a reference variable to this array, not an object. Nice tip: if you need to count the number of objects, just count the occurences of the new keyword. Although there are a few exceptions to this general rule. Let's see if you can list them all
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:What's the safest answer here?

2 objects being created on line2 and line3.

nick woodward wrote:I'd say 3, but I have a feeling this would be marked incorrectly because the array already exists.

That array is indeed already created, args is just a reference variable to this array, not an object. Nice tip: if you need to count the number of objects, just count the occurences of the new keyword. Although there are a few exceptions to this general rule. Let's see if you can list them all


ummm.

String s = "Java"; (also StringBuilder and Buffer)
s.concat(" Rules"); s.substring("2"); (I know these aren't assigned to anything so don't change s)
- what about s.toString() - is that technically a different object? strings are definitely one of my weaker areas....

int[] = {1,2,3};
and i think there's another shorthand way of writing an array, but I can't remember it....
oh, and does an array have a method that creates a shallow copy, but is technically still a new object with references to existing objects? or is that arraylist, i can't remember.

hmmm.... can you use boxing? so Integer i = 8; ? (I'm allowed to get this one wrong, not oca :P)

what about if the code is incorrect? an exception doesn't have an explicit 'new' keyword, but could still be created.

that's all i've got!

* oh and thanks for the answer!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:that's all i've got!

Not bad at all! You can verify your answer in this post.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:that's all i've got!

Not bad at all! You can verify your answer in this post.


ahhh, enums! yeah, I wouldn't have got that one.

i enjoyed the test - reminded me of some things to go look up again!

*edit: one of which might be worth mentioning in case anyone else reads this. String s = "Java" doesn't necessarily create a new string object, if it has already been created in the program without using the new keyword; But that would be a bit of a nasty question!

thanks
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:You can verify your answer in this post.

I noticed that the referenced post does not include the String concatenation operator (+) which may create a String object at runtime.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:
Roel De Nijs wrote:You can verify your answer in this post.

I noticed that the referenced post does not include the String concatenation operator (+) which may create a String object at runtime.

True! But that's also part of "strings" (the 1st one on the list). If you add the String concatenation operator (+) seperately, you'll have to add each string manipulation method of the String class as well (e.g. toLowerCase(), toUpperCase(), substring(),...). And probably the toString method of class Object (and all overrides) too.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance can be explicitly created by explicitly using the new keyword or explicitly calling a method (like toString(), toLowerCase(), clone(), object deserialization etc). Any other technique counts as an implicit way to create an instance.

According to the JLS

A new class instance may be implicitly created in the following situations:
  • Loading of a class or interface that contains a String literal (§3.10.5) may create a new String object to represent that literal. (This might not occur if the same String has previously been interned (§3.10.5).)
  • Execution of an operation that causes boxing conversion (§5.1.7). Boxing conversion may create a new object of a wrapper class associated with one of the primitive types.
  • Execution of a string concatenation operator + (§15.18.1) that is not part of a constant expression (§15.28) always creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type.
  • Evaluation of a method reference expression (§15.13.3) or a lambda expression (§15.27.4) may require that a new instance of a class that implements a functional interface type be created.

  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic