Since strings are immutable, both versions are safe. The latter, however, is less efficient (it creates an extra object and in some cases copies the character data).
With this in mind, the first version should be preferred.
Related Posts:
- javac is not recognized as an internal or external command, operable program or batch file [closed]
- What is a StackOverflowError?
- How to uninstall Eclipse?
- How do I determine whether an array contains a particular value in Java?
- How can I solve Exception in thread “main” java.lang.NullPointerException error [duplicate]
- Why am I getting a NoClassDefFoundError in Java?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Using Pairs or 2-tuples in Java [duplicate]
- Is there a way to convert all comments into javadoc comments? [Eclipse/Java]
- Reverse a string in Java
- “Char cannot be dereferenced” error
- Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array
- Error occurred during initialization of boot layer FindException: Module not found
- Non-static variable cannot be referenced from a static context
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
- “Missing return statement” within if / for / while
- How to convert byte array to string and vice versa?
- good example of Javadoc
- Java: Multiple class declarations in one file
- Why do JVM arguments start with “-D”?
- How to split a string in Java
- Printing out a linked list using toString
- Received fatal alert: handshake_failure through SSLHandshakeException
- Best way to Format a Double value to 2 Decimal places
- Collision resolution in Java HashMap
- “Content is not allowed in prolog” when parsing perfectly valid XML on GAE
- Java Undefined Object
- What’s the difference between next() and nextLine() methods from Scanner class?
- How to identify object types in java
- What is the result of making log4j additivity equals to true or false?
- Attempt to invoke virtual method ‘…’ on a null object reference
- Is there a Mutex in Java?
- maven error: package org.junit does not exist
- ClassNotFoundException: org.slf4j.LoggerFactory
- Problems with setting the classpath in ant
- Spring Data JPA Update @Query not updating?
- What does ‘SSSXXX’ mean in a java simple date format object?
- Server Tomcat v9.0 Server at localhost failed to start
- Getting java.net.SocketTimeoutException: Connection timed out in android
- What is Java String interning?
- Change priorityQueue to max priorityqueue
- Only using @JsonIgnore during serialization, but not deserialization
- Cannot change version of project facet Dynamic Web Module to 3.0?
- InvalidKeyException Illegal key size
- Collections.emptyMap() vs new HashMap()
- java howto ArrayList push, pop, shift, and unshift
- Error Message: Can’t determine a valid Java Home
- How can I fix ‘android.os.NetworkOnMainThreadException’?
- Eclipse does not start when I run the exe?
- How to print to the console in Android Studio?
- How to fix the Hibernate “object references an unsaved transient instance – save the transient instance before flushing” error
- channel 3: open failed: connect failed: Connection refused
- Java Currency Number format
- How to create a generic array in Java?
- Print a boolean value with printf
- Meaning of @Resource annotation
- How to solve error lossy convertion from int to char?
- What does “error: ‘.class’ expected” mean and how do I fix it
- Printing the stack values in Java
- Import a custom class in Java
- The specified child already has a parent. You must call removeView() on the child’s parent first (Android)
- HTTP Status 500 Error instantiating servlet class
- Java: How to set Precision for double value?
- Any way to declare an array in-line?
- java.io.StreamCorruptedException: invalid stream header: 54657374
- How to cast ArrayList<> from List<>
- Best way to “negate” an instanceof
- How to send HTTP request in java?
- How to take input as String with spaces in java using scanner
- Address already in use: JVM_Bind java
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- How can I check if a value is of type Integer?
- what is the difference between doGet() and doPost() in term of the flow?
- Java 3D Game Engine?
- How to cast ArrayList<> from List<>
- Using insertion sort for descending order?
- What is the home directory for JDK?
- java.lang.ClassNotFoundException: org.postgresql.Driver, Android
- Java – Change int to ascii
- Adding JPanel to JFrame
- Append a single character to a string or char array in java?
- Java printf formatting to print items in a table or columns
- Javadoc @author tag good practices
- A fatal error has been detected by the Java Runtime Environment: SIGSEGV, libjvm
- java.lang.IllegalStateException: Failed to introspect Class
- ShoppingCart.Java Program Assignment
- White spaces are required between publicId and systemId
- Http status 401 This request requires HTTP authentication (). in tomcat 6
- Get integer value of the current year in Java
- How to make the division of 2 ints produce a float instead of another int?
- Java Console Prompt for ENTER input before moving on [duplicate]
- Can’t load IA 32-bit .dll on a AMD 64-bit platform
- Android Studio 2.2.3: Gradle project sync failed. Basic functionality (e.g. editting, debugging) will not work properly
- How to draw a circle with given X and Y coordinates as the middle spot of the circle?
- In Java, how do I parse XML as a String instead of a file?
- Java Returning method which returns arraylist?
- String cannot be resolved to a type – Java RAD
- IntelliJ IDEA “The selected directory is not a valid home for JDK”
- Simple timeout in java
- Eclipse message saying List cannot be resolved to a type