Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Small memory related question

Posted on 2004-09-10
2
Medium Priority
?
251 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

596 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