File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding string concepts

 
fifa lover
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following program:

public Boolean checkValue(String input){
if(input.equals("Y") || input.equals("Yes)){
return Boolean.TRUE;
}
else if(input.equals("N") || input.equals("No")){
return Boolean.FALSE;
}
}

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?

Thanks in advance
newbie
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please check your private message for an administrative matter.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let me reference this JavaRanch article: Strings-Literally. It goes into some detail explaining how things work.

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.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some remarks about your code.

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you will see here, there is a much more elegant way to write those return statements.
 
Winston Gutkowski
Bartender
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic