Now I have the following questions:
1. Since the objects (String objects) are created at runtime, The number of objects created for any input will always be 3 ((input, Y, Yes) or (input, N, No)) or 5 since it has to check for all the conditions in case if the input is "No"
2. Are these objects stored in stack? or in case of multithreaded env, is it a good choice to use static variables?

fifa lover wrote:1. Since the objects (String objects) are created at runtime, The number of objects created for any input will always be 3 ((input, Y, Yes) or (input, N, No)) or 5 since it has to check for all the conditions in case if the input is "No"

The closest correct answer is there will be up to 5 (including the input) String Objects created - assuming "N", "No", "Y", and "Yes" have not yet been referenced. The four literals are added to the String pool when the class is loaded - not when the code is run, so all four will be loaded before the method gets called, and regardless of which path through the method is executed.

2. Are these objects stored in stack? or in case of multithreaded env, is it a good choice to use static variables?

No, Strings are Objects, and as such should always be considered to be stored in the Heap. There are special cases when an advanced runtime feature can determine it is safe to store an instance of a class on the stack* but that can never happen with String literals.

*It is called escape analysis, is non-standard I believe, only available in latest JREs, beyond what you need to consider when programming, and can never be applied to String Literals because the String Object will be referenced from the String Pool, which means it will always 'escape' the method and so can not be stored on the stack.

Steve

Ivan Jozsef Balazs

Rancher

Posts: 999

5

posted 4 years ago

1

fifa lover wrote:Consider the following program:

It is not a program, it is the erroneous code of a method.
The Yes literal is not closed, and the method does not yield a return value for all execution paths.

First of all, why are you using the wrapper type Boolean instead of the primitive type boolean? Always prefer primitive types above wrapper types - use a wrapper type only when there's a good reason to do so.

Second, what happens if input contains anything else than "Y", "Yes", "N" or "No"? The code will not compile because there's a missing return statement for that case.

Objects are never stored on the stack* - objects are always stored on the heap. Using static variables does not apply here. Only member variables of a class can be static; local variables or arguments cannot be static.

* except when you're using a JVM which does escape analysis, which is an advanced optimization which you don't need to know about.

fifa lover wrote:Now I have the following questions:
1. Since the objects (String objects) are created at runtime, The number of objects created for any input will always be 3 ((input, Y, Yes) or (input, N, No)) or 5 since it has to check for all the conditions in case if the input is "No"
2. Are these objects stored in stack? or in case of multithreaded env, is it a good choice to use static variables?

My advice: Unless you need to answer these questions for the SCJP exam (or whatever it's called now), obsessing about this sort of stuff not a good use of your time. Furthermore, once you've passed the exam you will probably rarely need to worry about it again (and if you do, you're probably doing something wrong).

The fact is that the JVM is responsible for creating objects, not you (beyond making sure that they are created); so don't worry about where or when or how it does it, or how many it creates. Concentrate on writing good, clean code.

My two cents.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here