Postingan

Menampilkan postingan dengan label Java String

10 Differences between StringBuffer and StringBuilder in Java

Gambar
Both StringBuffer and StringBuilder are two important classes in Java which represents mutable String i.e. the String object, whose value can be changed. Since String is Immutable in Java, any change or operation on String object e.g. converting it to upper or lower case, adding character, removing a character, or getting a substring, all results in a new String object. This can put a lot of pressure on Garbage collector if your application generates lots of throws away String instances. To avoid this issue, Java designer presented initially StringBuffer class and later StringBuilder as mutable String. That's the main difference between String vs StringBuffer and StringBuilder and also one of the frequently asked Java questions for beginners. Anyway, when StringBuffer was introduced it has its own problem e.g. it was synchronized, methods like append have synchronized and hence they were slower. Even if you use them by just one thread and don't share with other threads, the cos...

3 Ways to convert String to Boolean in Java? Examples

Gambar
You can convert a String object to Boolean object or boolean primitive by using the Boolean.valueOf() and Boolean.parseBoolean() method. The steps are similar rot converting to String to other data types e.g. String to Integer and String to Long. You can use valueOf() method to convert String to Boolean object and parseBoolean() method to convert given string to a boolean primitive value. Internally, valueOf() also uses parseBoolean() for parsing String but on top of that it also provides caching e.g. it can return Boolean.TRUE and Boolean.FALSE cached value for "true" and "false" string. In fact, the Boolean.TRUE is returned only when String is equal to true ignoring case e.g. "True", "true", "TRUE" will evaluate into boolean true, hence Boolean.TRUE will be returned. For string like "Yes", Boolean.FALSE will be returned. We'll discuss the rules of String to boolean conversion in next section. 1. Rules of String to Boolea...

Difference between StringJoiner vs String.join in Java 8 with Examples

Gambar
Joining multiple String literals or object into one is a common programming requirement and you will often find situations where you need to convert a list of String or a Collection of String into a CSV String for your application. For a long time, JDK API has no way to join multiple String literals or objects together, which forces programmers to write hacks like looping through all String objects and manually joining them using String concatenation to create the final, joined String. Even though this approach worked, it was filled with errors and hacks like you need to be careful not to add delimiter before the first element and after the last element, which often caused issues, particularly in case of beginners and junior Java developers. But the bigger problem with that approach was that everyone needs to re-invent the wheel. Since it was a very common requirement you will find many programmers writing the same routines and making the same mistakes and often ending in StackOverflow...

When to use intern() method of String in Java?

Gambar
The String.intern() method can be used to deal with the String duplication problem in Java . By carefully using the intern() means you can save a lot of heap memory consumed by duplicate String objects. A String object is said to be duplicate if it contains the same content as another string but occupied different memory location e.g. str1 != str2 but str1.equals(str2) is true. Since String object consumes a large amount of heap memory in average Java application, it makes sense to use the intern() method to reduce duplication and take advantage of String pool feature provided by Java. You can use intern() method to intern a String object and store them into String pool for further reuse. For example, when you create a String literal like "ABC" then it's automatically stored in String pool, but when you create a new String object, e.g. new String("abc"), even though it's the same String, a new object at a different memory location is created. This is a dupl...

How to String Split Example in Java - Tutorial

Gambar
Java String Split Example I don't know how many times I needed to Split a String in Java. Splitting a delimited String is a very common operation given various data sources e.g CSV file which contains input string in the form of large String separated by the comma. Splitting is necessary and Java API has great support for it. Java provides two convenience methods to split strings first within the java.lang.String class itself: split (regex) and other in java.util.StringTokenizer . Both are capable of splitting the string by any delimiter provided to them. Since String is final in Java every split-ed String is a new String in Java. In this article we will see how to split a string in Java both by using String’s split() method and StringTokenizer. If you have any doubt on anything related to split string please let me know and I will try to help. Since String is one of the most common Class in Java and we always need to do something with String; I have created a lot of how to do...

Top 10 Java String interview Question answers - Advanced

Gambar
10 Java String interviews Question answers String interview questions in Java is one of Integral part of any Core Java or J2EE interviews. No one can deny the importance of String and how much it is used in any Java application irrespective of whether its core Java desktop application, web application, Enterprise application or Mobile application. The string is one of the fundamental of Java programming language and correct understanding of String class is a must for every Java programmer. What makes String interview questions in Java even more interesting is the special status of String in terms of features and privileges it has, like the + operator is kind of overloaded to perform String concatenation despite the fact that Java does not support operator overloading. There is a separate String pool to store String literal etc. In this article we will some frequently asked question on String in a Java interview which focuses on range of features like immutability, thread-safety, secu...

How SubString method works in Java - Memory Leak Fixed in JDK 1.7

Gambar
Substring method from String classis one of most used method in Java, and it's also part of an interesting String interview question  e.g. How substring works in Java or sometime asked as how does substring creates memory leak in Java. In order to answer these questions, you knowledge of  implementation details is required. Recently one of my friend was drilled on substring method in Java during a Java interview, he was using substring() method from long time, and of course all of us has used this, but what surprises him was interviewer's obsession on Java substring, and deep dive till the implementation level. Though String is a special class in Java, and subject of many interview questions e.g. Why char array is better than String for storing password . In this case it was, substring method, which took center stage. Most of us rather just use substring (..), and than forgot. Not every Java programmer go into code, and see how exactly it's working. To get a feel of how ...