?
Solved

Small memory related question

Posted on 2004-09-10
2
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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