Solved

Small memory related question

Posted on 2004-09-10
2
197 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
Comment Utility
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
Comment Utility
8-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
Fibonacci challenge 11 82
IT Company 5 68
solarwind tftp server 2 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now