Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, January 29, 2023

Why String is Immutable or Final in Java

 

Why String is Immutable or Final in Java

In object-oriented programming, the immutable string or objects that cannot be modified once it is created. But we can only change the reference to the object. We restrict to change the object itself. The String is immutable in Java because of the security, synchronization and concurrency, caching, and class loading. The reason of making string final is to destroy the immutability and to not allow others to extend it.

The String objects are cached in the String pool, and it makes the String immutable. The cached String literals are accessed by multiple clients. So, there is always a risk, where action performs by one client affects all other clients. For example, if one client performs an action and changes the string value from Pressure to PRESSURE, all remaining clients will also read that value. For the performance reason, caching of String objects was important, so to remove that risk, we have to make the String Immutable.

Why String is Immutable or Final in Java

These are some more reasons of making String immutable:

  • The String pool cannot be possible if String is not immutable in Java. A lot of heap space is saved by JRE. The same string variable can be referred to by more than one string variable in the pool. String interning can also not be possible if the String would not be immutable.
  • If we don't make the String immutable, it will pose a serious security threat to the application. For example, database usernames, passwords are passed as strings to receive database connections. The socket programming host and port descriptions are also passed as strings. The String is immutable, so its value cannot be changed. If the String doesn't remain immutable, any hacker can cause a security issue in the application by changing the reference value.
  • The String is safe for multithreading because of its immutableness. Different threads can access a single "String instance". It removes the synchronization for thread safety because we make strings thread-safe implicitly.
  • Immutability gives the security of loading the correct class by Classloader. For example, suppose we have an instance where we try to load java.sql.Connection class but the changes in the referenced value to the myhacked.Connection class does unwanted things to our database.

Let's understand the concept of immutable through an example.

ImmutableString.java

  1. import java.util.*;  
  2. class ImmutableString{    
  3.     public static void main(String args[]){    
  4.         String NewString = "Hello";    
  5.         NewString.concat("World");  
  6.         System.out.println(NewString);    
  7.     }    
  8. }    

Output:

Why String is Immutable or Final in Java

Description: We can understand the above example with the help of the following diagram:

Why String is Immutable or Final in Java

In the string constant pool, the Hello remains unchanged, and a new string object is created with HelloWorld. It shows that the strings are immutable. The reference variable points to the Hello not to the HelloWorld.

If we want that it refers to the HelloWorld, we have to explicitly assign it to that variable. For example:

  1. import java.util.*;  
  2. class ImmutableString{    
  3.     public static void main(String args[]){    
  4.         String NewString = "Hello";    
  5.         NewString = NewString.concat("World");  
  6.         System.out.println(NewString);    
  7.     }    
  8. }    

Output:

Why String is Immutable or Final in Java

Best Coding Keyboard under 2000 rupees in 2023

Here are five of the best keyboards that you can buy on Amazon for around 2000 Rupees:


5: Logitech K120: A wired USB keyboard with a low profile design and a comfortable, quiet typing experience.


About this item
  • All-day Comfort: The design of this standard keyboard creates a comfortable typing experience thanks to the deep-profile keys and full-size standard layout with F-keys and number pad.
  • Easy to Set-up and Use: Set-up couldn't be easier, you simply plug in this corded keyboard via USB on your desktop or laptop and start using right away without any software installation.
  • Compatibility: This full-size keyboard is compatible with Windows 7, 8, 10 or later, plus it's a reliable and durable partner for your desk at home, or at work
  • Spill-proof: This durable keyboard features a spill-resistant design (1), anti-fade keys and sturdy tilt legs with adjustable height, meaning this keyboard is built to last.
  • Upgrade to Logitech K270 Wireless Keyboard: Experience wireless freedom with K270, featuring 2-year battery life, plug-and-play simplicity, and reliable hassle-free wireless connection.



4: Zebronics Zeb-Warrior: A budget gaming keyboard with adjustable backlighting, media controls, and a spill-resistant design.


About this item
  • Zeb-Magnus USB Gaming Keyboard with 4 Modes multicolor LED Lights
  • It has gold plated USB connector and braided cable
  • It includes 3 customizable macro keys, 5 programmable keys with rupee key
  • Interface : USB
  • Power Consumption : DC5V, <350mA
  • Keystroke Life : 10 million cycles
  • 1 year warranty-from the date of purchase
  • Country of Origin: China


3: Lenovo 300 USB Keyboard: A compact wired keyboard with a simple design, quiet keys, and durable construction.

About this item
  • Lenovo Wired Keyboard and Mouse Combo: Black color
  • Keyboard: waterproof exoskeleton, Mouse: full size, an ergonomic masterpiece – all-day comfort at your fingertips.
  • A rugged set that not only passed rigorous performance tests, but passed all while still looking sleek and stylish
  • Island style keys and quiet key strokes give you a typing experience like no other - and it’s all liquid resistant!
  • A high-resolution, 1600 dpi optical sensor ensures you’ll be smoothly gliding from window-to-window
  • Country of Origin: China


2: AmazonBasics Wired Keyboard: A wired USB keyboard with a standard layout, media controls, and a plug-and-play design.




About this item
  • Wired keyboard with sturdy design
  • Comes with a USB 2.0 cable for a reliable connection
  • Robust keys provide a comfortable experience
  • Convenient multimedia keys for easy access to common functions
  • Compatible and works well with most PC/Mac/Laptop/Smartphone/Tablet.


1: Zebronics Companion 110 Wireless Keyboard and Mouse Set with Bluetooth multiconnect, 2.4GHz Receiver, 2 Months* Backup, 1600DPI, Smartphone/Tablet Holder, Built in Rechargeable Battery




That's all about the Keyboard budget friendly best keyboards

Why String is Immutable or Final in Java

  Why String is Immutable or Final in Java In object-oriented programming, the  immutable string or objects  that cannot be modified once it...