Page 1 of 1
About Question enthuware.ocajp.i.v7.2.868 :
Posted: Fri Mar 15, 2013 11:23 am
by baptize
see attached photo, careful when minimum capacity is negative, this is tricky.
Just thought of sharing it with you guys.

Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Wed Jun 05, 2013 6:32 am
by terence_j_coffey
Working with java data types
StringBuffer
Notes state "If the minimum capacity argument is non positive this method takes
no action and simply returns."
I've tried this and I observe the following
If I enter StringBuffer sba = new StringBuffer(-5); it compiles fine but i get a runtime error.
Exception in thread "main" java.lang.NegativeArraySizeException
at java.lang.AbstractStringBuilder.<init>(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at InitClass3.main(InitClass3.java:54)
I'm using java 1.7.0_17
If I create a StringBuilder string with initial capacity of zero, there is no runtime error.
I've just added this in with other code but the error message is clear where the issue is.
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Mon Mar 10, 2014 10:32 pm
by fasty23
As the question asked "at least 100" (minimum), how "StringBuilder sb = new StringBuilder(100);" is correct?
Doesn't A option specified certain number of characters? or it defined the minimum number of characters and it could be more during coding?
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Tue Mar 11, 2014 12:10 am
by admin
Because capacity of 100 satisfies what the question asks i.e. at least 100. I am not sure what is your doubt.
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Tue Mar 11, 2014 2:59 pm
by fasty23
I just assumed as the question asked "at least 100" (minimum) which means capacity 100 character is minimum and the variable could have more than 100 characters, but "StringBuilder sb = new StringBuilder(100);" has exact capacity not more.
Am I wrong?
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Tue Mar 11, 2014 8:02 pm
by admin
If you create a StringBuffer with some capacity (or even without specifying capacity), it will always be created with a certain capacity.
Creating a StringBuffer with 100 doesn't mean it can store only 100 characters. It can store more if required. But initial capacity is 100, which satisfies the requirement given in the question.
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Fri Jul 11, 2014 2:56 am
by kashyapa
i got little bit confuse with the explanation.
The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.

Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Fri Jul 11, 2014 5:02 am
by admin
Not sure what is confusing about it. You have two numbers. Just pick the larger out of those two.
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Sun Oct 12, 2014 7:47 am
by Smoljanov Sergej
kashyapa wrote:i got little bit confuse with the explanation.
The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.

Code: Select all
StringBuilder sb = new StringBuilder();
System.out.println("new StringBuilder().capacity() = " + sb.capacity());
//capacity of new StringBuilder() is 16
sb.ensureCapacity(30);
// if current (currentCapacity*2+2)> argumetn of ensureCapacity()
// result will be (currentCapacity*2+2)
System.out.println(sb.capacity());
sb = new StringBuilder();
sb.ensureCapacity(34);
System.out.println(sb.capacity());
sb = new StringBuilder();
sb.ensureCapacity(35);
System.out.println(sb.capacity());
// if current (currentCapacity*2+2)< argumetn of ensureCapacity()
// result will be argumetn of ensureCapacity()
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Tue Jan 27, 2015 1:53 am
by gparLondon
Great explanation @Smoljanov Sergej, thank you.
I dint get this,
Observe that the question says "at least 100 characters". In the exam, you may get a question that says "100 characters", in that case, ensureCapacity() may not be a valid option.
Why would ensureCapacity() is not a valid option when question says "100 characters"?
Thanks,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Tue Jan 27, 2015 2:09 am
by admin
Because the method may allocate more than 100. Basically, you need to be careful about the wordings used in the question.
Re: About Question enthuware.ocajp.i.v7.2.868 :
Posted: Wed Jan 28, 2015 1:45 pm
by gparLondon
Thanks for the explanation.