Shekhar Pareek

Greenhorn
+ Follow
since Aug 28, 2006
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Shekhar Pareek

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 !!
17 years ago
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.
17 years ago
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 ?
17 years ago
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.
17 years ago
Folks, i read this on a website:

"You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use.
It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL. "

So does it mean whenever we try to create a db connection, the driver manager would iterate through the entire list of drivers which have registered ??
In case the number of drivers registered are substantial, won't this create an overhead ?