Solved

Small memory related question

Posted on 2004-09-10
2
232 Views
Last Modified: 2010-03-31
Dear Java experts

I was wondering... in my code... to display a variable to the console i use the following like 1000+ times:

System.out.println("" + varName);

and

System.out.println("" + varName + " " + varName2);



My questions are related to the "" and " ".

1. In the examples above... does Java consider "" and " " as Strings? i mean... does it allocate (forgive my C example here) sizeof(String) + (numberOfCharsInString * 2 bytes per chars)?

2. Since there are more than 1000 of each in the code... does Java allocate memory for only one " " and ""? or does Java allocate 1000+ " " and ""?

3. Should i do this instead:

private final static String strEmptyString = "";
private final static String strSingleSpace = " ";

System.out.println(strEmptyString  + varName + strSingleSpace  + varName2);

4. Out of curiosity... in Java... can we do: "This is a String".length( );


So thanks in advance to everyone who will take time to clarify this!!

   -Mike

0
Comment
Question by:oroussea
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12028502
1. Probably not. Since these concatenations are done by StringBuffer internally, they will probably get appended as characters to the StringBuffer
2. No 'memory' as such is allocated (although of course memory is allocated for the StringBuffer) as an append happens (see 1.)
3. It's a good idea, as it makes the code more readable, although it won't affect the code footprint as far as memory/execution time is concerned. The Java convention is to capitalize constants.
4. Yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12028996
8-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 72
hibernate jars 4 57
Running JavaFX on the Raspberry Pi 27 127
DTD and JAVA versions 1 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question