• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaring a string constant

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Say i have a following class.

class Test{

public method1(){
String str = buildString();
....
....

}

public method2(){
String str = buildString();
....
....

}

public method3(){
String str = buildString();
....
....

}


private String buildString() {
StringBuffer sb = new StringBuffer();
sb.append(MyConstant.ITEM);
sb.append(" , ");
sb.append(MyConstant.CODE);

return sb.toString();
}


}


So in the above piece of code my methods make use of a particular String constant, which i obtain through the above buildString() method.
I feel there is a performance issue in the above case, and was wondering if i could optimise it, then i came up with something as such:

class Test{

private static final String str = MyConstant.ITEM + " , " + MyConstant.CODE;


public method1(){
String str = Test.str;
....
....

}

public method2(){
String str = Test.str;
....
....

}

public method3(){
String str = Test.str;
....
....

}
}


But again in the above case, the concatenation operator will cause an overhead if there are say 10 constants (or literals) to be concateneted.

Is there a better way around ??

I thought of using a static block at the class level, where i wanted my String to be a final and Static one, but i wont be able to refer to it frm methods as the scope would die outside the static block itself.

class Test {
static{
final StringBuffer sb = new StringBuffer();
sb.append(MyConstant.ITEM);
sb.append(" , ");
sb.append(MyConstant.CODE);
final String str = sb.toString();
}

method1(){
String string1 = Test.str; ----> not possible cos the scope dies outside the static block
}
method2(){
String string2 = Test.str;
}

}

Please give your valueable feedbacks.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do this:

private static final String str = MyConstant.ITEM + " , " + MyConstant.CODE;

and the "constants" are actually constants, then the concatenation will be done by the compiler -- it will have no runtime overhead.

But even if it is done at runtime, it's just done once, when the class is loaded and initialized. Concatenating a few strings is a tiny amount of work compared to loading a class, so this really doesn't matter at all.
 
Shekhar Pareek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Ernest.
But what if i had to concatenate a mix of Constants and string literals like
private final static String str = MyConst.Name + " , " + MyConst.Age + " , "
+ MyConst.Sex + .......so on, say there are 20 such constants and each followed by a comma, as shown above ..would it not be expensive ?

Also, could a static block come to rescue in this case ?
 
Shekhar Pareek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i came up with this:
class Test{
private static final StringBuffer sb = new StringBuffer();
private static final String str;

static {
sb.append(Const1);
sb.append(" , ");
sb.append(Const2);
....
....
....
str = sb.toString();
}

method1() {
String str1 = Test.str;
.....
}
method2() {
String str1 = Test.str;
.....

}
method3() {
String str1 = Test.str;
.....

}
method4() {
String str1 = Test.str;
.....

}
}

I think by introducing a static block, we have minimised the overhead due to the "+" operator.
Internally + operator would use a StringBuffer's append() method.

Pls let me know if i am right.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shekhar Pareek:

But what if i had to concatenate a mix of Constants and string literals



String literals are constants.

I'm not sure you understood my last message. There are two points.

First: if you write code like "String s = a + b + c" where a, b and c are compile-time constants, then the compiler will put the strings together at compile-time. Therefore the runtime overhead is zero. In the class file will be the result of adding all these Strings together, not the code to do it.

Second: you do realize that no matter how you do this, once the value of the static final is assigned, it will be used without any further computation. The concatenation will be done once and once only, when the class is loaded and initialized. Therefore, in the grand scheme of the world, this is such a tiny little thing that it really doesn't matter how you do it.
 
Shekhar Pareek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Mr. Hill, i have realised there is lot more to learn and clear the basics before digging into the real problems

I appreciate your help very much, wish you were my prof. at college !!
 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic