Skip to content
Advertisement

Tag: string

String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder? Answer Mutability Difference: String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Difference: The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs

How to add new elements to an array?

I have the following code: Those two appends are not compiling. How would that work correctly? Answer The size of an array can’t be modified. If you want a bigger array you have to instantiate a new one. A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a )

Why doesn’t String’s hashCode() cache 0?

I noticed in the Java 6 source code for String that hashCode only caches values other than 0. The difference in performance is exhibited by the following snippet: Running this in ideone.com gives the following output: So my questions are: Why doesn’t String’s hashCode() cache 0? What is the probability that a Java string hashes to 0? What’s the best

Java: convert List to a join()d String

JavaScript has Array.join() Does Java have anything like this? I know I can cobble something up myself with StringBuilder: .. but there’s no point in doing this if something like it is already part of the JDK. Answer String.join With Java 8 you can do this without any third party library. If you want to join a Collection of Strings

Prevent round off in String.format(“%.2f”, doubleValue) in Java

How do I prevent String.format(“%.2f”, doubleValue); from rounding off (round half up algorithm) instead of just truncating it? e.g. after formatting, I just want to discard the last digit, I know there are other ways to do this, I just want to know if this is possible using the String.format. Answer You can always set the rounding mode: http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html and

How do I find out if first character of a string is a number?

In Java is there a way to find out if first character of a string is a number? One way is and do the above all the way till 9, but that seems very inefficient. Answer Note that this will allow any Unicode digit, not just 0-9. You might prefer: Or the slower regex solutions: However, with any of these

What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

The title pretty much says it all. What’s the simplest/most elegant way that I can convert, in Java, a string from the format “THIS_IS_AN_EXAMPLE_STRING” to the format “ThisIsAnExampleString”? I figure there must be at least one way to do it using String.replaceAll() and a regex. My initial thoughts are: prepend the string with an underscore (_), convert the whole string

Advertisement