This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

String.replace() changing nothing, but the behavior is different for char and String input

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Consider the following code:

So if you replace a character with the exact same character, the result will still be referring to the same Object. If you replace a String with the exact same String though, a new Object is created. What is logic behind this? Why is the behavior different depending on the input to replace()?

Thanks!

Regards,
Shane
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
So if you replace a character with the exact same character, the result will still be referring to the same Object. If you replace a String with the exact same String though, a new Object is created. What is logic behind this? Why is the behavior different depending on the input to replace()?



It is actually two different overloaded methods.

The version which takes chars, was added with Java 1.4, and contains a simple check, that if the old and new chars are the same, the replacement is bypassed and the original string is returned.

The version which takes strings (actually char sequences), was added with Java 5, is a convenience method that uses the regular expression engine to do the replacement. It is actually somewhat complex for something so simple, as the pattern and matcher instances has to be created, the regex special characters has to be disabled, the regex replacement string special characters has to be disabled, etc.

So, it is probably not surprising that these methods are slightly different in behavior.

Henry
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an interesting fact, but it is certainly not expected that you know such details about the implementation of standard library functions for the OCAJP exam.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Shane Jensen wrote:
So if you replace a character with the exact same character, the result will still be referring to the same Object. If you replace a String with the exact same String though, a new Object is created. What is logic behind this? Why is the behavior different depending on the input to replace()?



It is actually two different overloaded methods.

The version which takes chars, was added with Java 1.4, and contains a simple check, that if the old and new chars are the same, the replacement is bypassed and the original string is returned.

The version which takes strings (actually char sequences), was added with Java 5, is a convenience method that uses the regular expression engine to do the replacement. It is actually somewhat complex for something so simple, as the pattern and matcher instances has to be created, the regex special characters has to be disabled, the regex replacement string special characters has to be disabled, etc.

So, it is probably not surprising that these methods are slightly different in behavior.

Henry


That actually makes sense. Thanks!

Jesper de Jong wrote:This is an interesting fact, but it is certainly not expected that you know such details about the implementation of standard library functions for the OCAJP exam.


Alright, good to know. The thing is, I keep trying out new code combinations in order to fully understand what I'm working with, sometimes disregarding the scope of the exam. Even if it's not relevant for the exam, at least I'll learn from it.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:The thing is, I keep trying out new code combinations in order to fully understand what I'm working with, sometimes disregarding the scope of the exam. Even if it's not relevant for the exam, at least I'll learn from it.


That's the true spirit of getting certified Have a cow!
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic