Rajesh Hadiya’s Post

View profile for Rajesh Hadiya, graphic

Helps you to master Android & Spring Boot 🚀 Aaye hain to thoda content dekh ke jaiye 😉

This trivia about String in Kotlin is very interesting and you'll probably answer wrong. Assume we've following code. Here, == checks for structural equality (i.e., whether the contents of the objects are the same) and === checks for referential equality (i.e., whether the references point to the same object). 𝘷𝘢𝘭 𝘴𝘵𝘳1 = "𝘩𝘦𝘭𝘭𝘰" 𝘷𝘢𝘭 𝘴𝘵𝘳2 = "𝘩𝘦𝘭𝘭𝘰" 𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘴𝘵𝘳1 == 𝘴𝘵𝘳2) 𝘱𝘳𝘪𝘯𝘵𝘭𝘯(𝘴𝘵𝘳1 === 𝘴𝘵𝘳2) We can easily guess that output would be 'true' and 'false'. But if you run the code, it'll print 'true' and 'true'. 💥💣💥 This happens, because, when you create string literals with identical content, Kotlin optimizes memory usage by making those literals point to the same memory location. This is known as "string interning." ✅ In Java, String Interning is a method of storing only one copy of each distinct String value. #android #kotlin #java Want to master Android development? Check my live course link in comment to get 30% discount. Let's connect because energy is contagious Rajesh Hadiya

Rajesh Hadiya

Helps you to master Android & Spring Boot 🚀 Aaye hain to thoda content dekh ke jaiye 😉

2mo

MAD Course details and registration link http://bit.ly/mad-course-details

Rajesh Hadiya

Helps you to master Android & Spring Boot 🚀 Aaye hain to thoda content dekh ke jaiye 😉

2mo

Credit: The initial motivation for this post is derived from Prem Thakur's post and Abhaysing Bhosale's comment on it.

Rohan Bhagat

SDE-3 at Zupee | Android Developer

2mo

I remember this concept used to be called "String constant Pool". Has there been any change in the concept naming?

Ashutosh Jena

Mobile & Backend Engineering - FinBox | X - Samsung

2mo

Iska thobda kidhar sunela lagta he 😂 [String constant pool]

Karan Mahajan

Senior Android Lead Engineer @ Mahindra First Choice Wheels Ltd with Fifth Gear Ventures | MCA | Android, Jetpack Compose, MVI, MVVM, CI/CD, TDD, RxJava, Coroutines, Analytics (Adobe, Google, Clever Tap), Hilt, Koin DI

2mo

This is a perfect example of how Kotlin's string interning can surprise developers. It's a clever optimization, but it's crucial to remember that this behavior isn't guaranteed across all scenarios. For instance, it might differ with string objects created at runtime. This trivia highlights the importance of understanding language-specific optimizations and not always assuming how memory management works under the hood.

Syed Taha Alam

Android Developer | Jetpack Compose | Kotlin | MultiPlatform | Flutter || Remote Work

2mo

it was also being done in java as well where if a string is created by string literal and some other variable is also being created in same way with same value the JVM will handle it by string pool. but if somebody creates via constructor then things will change as the string created by constructor are in the heap not on the string pool.

String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java and Kotlin String class. Funny thing is its output will be entirely different if you initialize a String object with same value i.e val str3 = String(“hello”)

See more comments

To view or add a comment, sign in

Explore topics