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

HI , I am Chat GPT

 I am ChatGPT, an advanced AI language model developed by OpenAI. I have been trained on a diverse range of texts, allowing me to answer questions and generate text on a wide variety of topics with high accuracy and coherence.



As an AI language model, I do not have personal experiences or emotions. I exist solely to process and respond to questions and requests from users. My primary goal is to provide helpful and informative answers to the best of my ability.


I am designed to respond quickly and accurately to a wide range of questions, making me a valuable resource for those seeking information on a variety of topics. Whether you are looking for answers to questions about technology, finance, history, or any other subject, I am here to help.


If you have any questions or requests, feel free to ask and I will do my best to provide you with a helpful and informative response.

As an AI language model, I can answer a wide range of questions and generate text on various topics, including but not limited to:


  1. Technology
  2. Science
  3. History
  4. Literature
  5. Mathematics
  6. Geography
  7. Sports
  8. Art
  9. Music
  10. Pop culture
  11. Everyday life
  12. Health and wellness
  13. Cooking and food
  14. Travel
  15. Finance and economics
  16. Politics
  17. Environmental issues
  18. Religion and spirituality
  19. Personal development
  20. Productivity and time management

I have been trained on a diverse range of texts, so I can provide information, definitions, explanations, and summaries on a variety of subjects. Additionally, I can generate original text, such as creative writing, articles, or lists.


Note that as an AI language model, I strive to provide accurate and up-to-date information, but my knowledge is limited to my training data and is current as of my 2021 knowledge cut-off.

Deep, Shallow and Lazy Copy

 We can define Cloning as “create a copy of object”. Shallow, deep and lazy copy is related to cloning process. 

Shallow Copy 

  •  Whenever we use default implementation of clone method we get shallow copy of object means it creates new instance and copies all the field of object to that new instance and returns it as object type, we need to explicitly cast it back to our original object. This is shallow copy of the object.
  • clone() method of the object class support shallow copy of the object. If the object contains primitive as well as non primitive or reference type variable in shallow copy, the cloned object also refers to the same object to which the original object refers as only the object references gets copied and not the referred objects themselves.
  • That’s why the name shallow copy or shallow cloning in Java. If only primitive type fields or Immutable objects are there then there is no difference between shallow and deep copy in Java.



This can lead to unpleasant side effects if the elements of values are changed via some other reference.


Deep Copy 
 


  • Whenever we need own copy not to use default implementation we call it as deep copy, whenever we need deep copy of the object we need to implement according to our need.
  • So for deep copy we need to ensure all the member class also implement the Cloneable interface and override the clone() method of the object class.
  • A deep copy means actually creating a new array and copying over the values.




Changes to the array vals will not result in changes to the array data.
when to use what 
There is no hard and fast rule defined for selecting between shallow copy and deep copy but normally we should keep in mind that if an object has only primitive fields, then obviously we should go for shallow copy, but if the object has references to other objects, then based on the requirement, shallow copy or deep copy should be done. If the references are not updated then there is no point to initiate a deep copy.
Lazy Copy 
A lazy copy can be defined as a combination of both shallow copy and deep copy. The mechanism follows a simple approach – at the initial state, shallow copy approach is used. A counter is also used to keep a track on how many objects share the data. When the program wants to modify the original object, it checks whether the object is shared or not. If the object is shared, then the deep copy mechanism is initiated.
Summary 
In shallow copy, only fields of primitive data type are copied while the objects references are not copied. Deep copy involves the copy of primitive data type as well as object references. There is no hard and fast rule as to when to do shallow copy and when to do a deep copy. Lazy copy is a combination of both of these approaches



Saturday, January 28, 2023

What is Mental Masturbation

Mental Masturbation is human's favorite activity 

As we are taking so much of inputs from movies, Instagram, Facebook, news etc. We get inspired by many people, we admire other people's lifestyle, we get agitated with other political leaders actions, we get envy many people whose life is simply perfect.

So we get to talk with someone like our friends we want to sound perfect by talking about all these things you gonna do for getting your dream car, dream life, things you wish you had to make certain things better in your life, you would have done it differently if that person were you. 

That is okay. That's how group conversations made many life changing decisions for country. And also what else you got to talk with your friends? You cannot talk about productivity all the time. you got to judge few people for their bad behavior, you got to do hate talks for few people for the things that they have done for their petty desire. That way you can avoid those actions in your life and you are ensuring with your friends that they follow these moral behaviors.   

BUT 

One thing you got to do after that is you got to take steps to achieve what you said before.

Say you have to learn this skill to make your career better (Say Full stack Development)

Step 1: Just make up your mind that you will be a better full stack developer in next 3 months (say 90 days)

Step 2: Split the materials you gonna study into 3 parts. 

Step 3: Silently spend 1hour no matter what you got to finish that 1hour for sure to study it

Step 4: If you can keep going spend some more time. The more time you put the more valuable you gonna be.

Step 5: Keep going for everyday. Either you do this or don't its gonna be same like past 30 days(coz if you have done something earlier you wouldn't be reading this article to better yourself) So you got nothing to loose. 

Note: This is an advantage for you. because the people who see you doesn't gonna know about the things you are doing silently. You will get the urge to tell about this. But control yourself. If you cannot control yourself then you are beyond help. So forget about these things when you hangout with friends

Step 6: It will be hard for a week. But appreciate your self for doing it and believe yourself you are about to make a great impact.

Step 7: At the end of 90 days you will be a better Full Stack Developer 

Step 8: Then later if you have achieved something with your effort people will certainly notice you and ask about these efforts that you take. Then reveal it to your friends who you really care about it and who gives respect to you for your effort. If they don't value the effort that you took you should know that they envy you and certainly don't like the way you grow. So don't take these things to heart and feel bad about it. instead take another year challenge and become far better than before. Eventually the world will adapt to your actions. You will get like minded people sooner in your circle.

You are stronger than you know until you put yourself into action nobody will get to know it



  

Friday, January 27, 2023

Life of a Software Developer in D&B

Work Life Balance @ D&B


Working as D&B employee is awesome(i mean that's what i feel till now with 3 years of experience)

You get to work in many open tech stacks if you wish like angular React java .NET python automation AWS , Azure. 

If you are senior/proficient enough in Technology skills then you will get to choose the stack for the projects

Managers don't do Micromanagement but in return we gotta finish what we commit to complete on time(which is pretty much easy if you work properly without procrastinating) 

Sometimes i extended till late night and worked due to critical issue but as a reward they let you to take offs accordingly

Managers, colleagues are friendly. at the end they are also an employee like you. Give others what you expect from others. Say appreciation, helping when you are free, clearing doubts, etc. 

Great Hikes, Bonuses 

Matching product based company Market salary

Your work will be recognized and appreciated

As a Developer that's what i feel


Thursday, January 26, 2023

Scaler Full Stack Development Course Review

 Hi I'm a 6 years Front end developer

A Scaler Student

About myself I'm already having 6years of experience in Front end but less challenging projects to work

I don't think I'm capable enough to clear(MAANG) interviews with my Backend skills


So joined Scaler to strengthen my Backend and interview skills

Lets go for it in brief

This is good for anyone who has less problem solving skills and patience to learn it in a long committed time like 8 to 12 months by contributing 3 to 4hrs a day in class and doing assignments

If you really wanna make urself sharp you can already find those things in the outside open source but just not organized. you have to organize it and study

But with Scaler everything is organized and people are there to teach you everything 


Let me update about it slowly.

Keep watching this space for more inputs

 

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...