Link to home
Start Free TrialLog in
Avatar of oroussea
oroussea

asked on

Small memory related question

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

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)