wallwalker: Venetian mask, dark purple with gold gilding. (Default)
I'm feeling moderately better. Did a couple of challenges. Am going to retry them eventually... see if I can't do them from memory later.

I figured I'd make a few notes to help me later.

---

Differences between String, StringBuilder, StringBuffer:

Strings are immutable in Java, and so when a string is "changed," you actually make a new String and throw away the rest. That's a lot of stuff to throw away. StringBuffer and StringBuilder are both objects that can make that easier - it can make mutable objects that can be easily converted.

StringBuffer: Synchronized. Even when there is only one thread, you still have to lock and release, which takes more time.

StringBuilder: Not synchronized, was created to fix that slowness. Therefore it is not thread-safe.

If you need to edit a String a lot, use StringBuilder unless you are absolutely sure you will need a method that can support more than one thread at a time.
A short and rather pointless example:

String str="abc";
StringBuilder sb = new StringBuilder(str); //sb contains "abc"
sb.append('g'); //appends the letter g, sb contains "abcg"
System.out.print(sb.toString()); //prints "abcg"


A few useful methods:
StringBuilder append - will append string representations of most types of data to the data in the builder.
StringBuilder reverse - reverses the characters in the StringBuilder. This is by far the easiest way to reverse a string in Java.

Also there are Character methods that are useful - you can use Character.toUpperCase('b') to make b into B.

Profile

wallwalker: Venetian mask, dark purple with gold gilding. (Default)
wallwalker

November 2024

S M T W T F S
      12
3456789
10111213141516
17181920212223
24252627282930

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 25th, 2025 02:08 am
Powered by Dreamwidth Studios