aspose file tools*
The moose likes Java in General and the fly likes understanding string concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "understanding string concepts" Watch "understanding string concepts" New topic
Author

understanding string concepts

fifa lover
Greenhorn

Joined: Nov 20, 2012
Posts: 1
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

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi,

Please check your private message for an administrative matter.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Aug 16, 2005
Posts: 14143
    
  18

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
As you will see here, there is a much more elegant way to write those return statements.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7772
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: understanding string concepts