What is the complexity of this simple piece of code?

This seems to be a question of mislead, because I happened to read that book just now. This part of text in the book is a typo! Here is the context: =================================================================== Question: What is the running time of this code? 1 public String makeSentence(String[] words) { 2 StringBuffer sentence = new StringBuffer(); 3 for … Read more

Convert a StringBuffer to a byte Array in Java

A better alternate would be stringBuffer.toString().getBytes() Better because String.valueOf(stringBuffer) in turn calls stringBuffer.toString(). Directly calling stringBuffer.toString().getBytes() would save you one function call and an equals comparison with null. Here’s the java.lang.String implementation of valueOf method: public static String valueOf(Object obj) { return (obj == null) ? “null” : obj.toString(); }

Converting Char Array to List in Java

In Java 8 and above, you can use the String‘s method chars(): myString.chars().mapToObj(c -> (char) c).collect(Collectors.toList()); And if you need to convert char[] to List<Character>, you might create a String from it first and then apply the above solution. Though it won’t be very readable and pretty, it will be quite short.

StringBuffer is obsolete?

It’s obsolete in that new code on Java 1.5 should generally use StringBuilder – it’s very rare that you really need to build strings in a thread-safe manner, so why pay the synchronization cost? I suspect code that you see using StringBuffer mostly falls into buckets of: Written before Java 1.5 Written to maintain compatibility … Read more

What is difference between mutable and immutable String in java

Case 1: String str = “Good”; str = str + ” Morning”; In the above code you create 3 String Objects. “Good” it goes into the String Pool. ” Morning” it goes into the String Pool as well. “Good Morning” created by concatenating “Good” and ” Morning”. This guy goes on the Heap. Note: Strings … Read more

What is the difference between String and StringBuffer in Java?

String is used to manipulate character strings that cannot be changed (read-only and immutable). StringBuffer is used to represent characters that can be modified. Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String, you are creating a new object (internally) every time since String is immutable. You can … Read more

Python equivalent of Java StringBuffer?

Python 3 From the docs: Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below: if concatenating str objects, you … Read more

Create a string with n characters

Likely the shortest code using the String API, exclusively: String space10 = new String(new char[10]).replace(‘\0’, ‘ ‘); System.out.println(“[” + space10 + “]”); // prints “[ ]” As a method, without directly instantiating char: import java.nio.CharBuffer; /** * Creates a string of spaces that is ‘spaces’ spaces long. * * @param spaces The number of spaces … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)