About Question enthuware.ocajp.i.v7.2.868 :
Moderator: admin
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.868 :
see attached photo, careful when minimum capacity is negative, this is tricky.
Just thought of sharing it with you guys.
Just thought of sharing it with you guys.
- Attachments
-
- Screen Shot 2013-03-15 at 12.21.19 PM.png (38.13 KiB) Viewed 10092 times
-
- Posts: 2
- Joined: Wed Jun 05, 2013 6:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
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.
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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
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?
Doesn't A option specified certain number of characters? or it defined the minimum number of characters and it could be more during coding?
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
Because capacity of 100 satisfies what the question asks i.e. at least 100. I am not sure what is your doubt.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
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?
Am I wrong?
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Thu May 08, 2014 5:27 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
i got little bit confuse with the explanation.
The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
Not sure what is confusing about it. You have two numbers. Just pick the larger out of those two.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Sep 15, 2014 8:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
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()
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
Great explanation @Smoljanov Sergej, thank you.
I dint get this,
Thanks,
GPAR
I dint get this,
Why would ensureCapacity() is not a valid option when question says "100 characters"?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.
Thanks,
GPAR
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
Because the method may allocate more than 100. Basically, you need to be careful about the wordings used in the question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.868 :
Thanks for the explanation.
Who is online
Users browsing this forum: No registered users and 20 guests