How to get the StringBuilder capacity in Java

5 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder("Java");  

        System.out.println(sb.capacity());
    }
}



/*
run:

20

*/

 



answered Jun 16, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder();  

        System.out.println(sb.capacity());
    }
}



/*
run:

16

*/

 



answered Jun 16, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder();  
        System.out.println(sb.capacity());
        
        sb.append("Java");  
        System.out.println(sb.capacity());
    }
}



/*
run:

16
16

*/

 



answered Jun 16, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder();  
        System.out.println(sb.capacity());
        
        sb.append("12345678");  
        System.out.println(sb.capacity());
        
        sb.append("abcdefgh");  
        System.out.println(sb.capacity());
    }
}



/*
run:

16
16
16

*/

 



answered Jun 16, 2019 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder();  
        System.out.println(sb.capacity());
        
        sb.append("12345678");  
        System.out.println(sb.capacity());
        
        sb.append("abcdefgh");  
        System.out.println(sb.capacity());
        
        sb.append("W"); // (16 * 2) + 2
        System.out.println(sb.capacity());
    }
}



/*
run:

16
16
16
34

*/

 



answered Jun 16, 2019 by avibootz
edited Jun 16, 2019 by avibootz

Related questions

1 answer 139 views
1 answer 119 views
2 answers 223 views
1 answer 62 views
1 answer 73 views
1 answer 75 views
1 answer 84 views
...