UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

What is the difference between String s1 = "Hello"; and String s1 = new String("Hello"); in Java?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Both these expressions are used to create string objects. But the difference lies in the memory management of the objects created. Let's consider the first case: String s1="Hello"; In the above statement, the string object s1 is created and the value of the string "Hello" is assigned to the string...
read more

Both these expressions are used to create string objects. But the difference lies in the memory management of the objects created.

 

Let's consider the first case:  String s1="Hello";

In the above statement, the string object s1 is created and the value of the string "Hello" is assigned to the string object s1. JVM assigns this value to the object and stores it in a separate block of memory called the string constant pool.

Now if another String object is created,  say String s2="Hello";

Then, JVM checks if any string object with the same value already exists in the string constant pool. If so, then JVM assigns a reference of the existing object to the new variable.

Hence we run:

String s1="hello";

String s2="hello";

System.out.println(s1==s2)

Then the output will be True because s1 and s2 are references to the same object.

Now let us consider the second case:  String s1 new String("Hello");

We create an object s1 using the new keyword. This will create two objects, one in the string Constant Pool(SCP) and the other one in the Heap Memory and one reference variable, and S1 will refer to the object in heap memory. Now if we create another object String s2= new string("hello");

Then, it'll create a new object s2 with the same value, irrespective of whether it is present in the heap memory or not.

read less
Comments

Related Questions

 I want to learn from beginning, please tell me what is the best source to learn (core java)

hi sunaini It is very good that you want to learn programming... I would recommend you c,c++,java and dotnet courses...for becoming a good provrammer.. we provide online training..at lowest prices if...
Sunaini
What is difference between Multithreading and miltiprocessing?
It is simple to understand. You question itself contains the answer. Only the keynote is: A single processor/core of the cpu can execute single task at a time, not more than one task. And you should...
Milan
Is synchronized keyword necessary, in Java, if everything that is modified in a function is local to that function?
No, it is not required if you make sure whatever parameters are updated/modified will be done as atomic references/operations. Atomicity is an alternative to syncronizatiom.
Abishiek
0 0
6
Is it really needed to do Java training before graduation, to get a job?
Yes, It's must to get better opportunities and to standard alone in IT field. Not only core Java but also learn Spring,Hinernat ,Struts. ANGULARJS add an advantage to get job easily.
Udaybhan
Need to learn java....
You may contact me. I teach Java in great depth and with very firm building blocks.
Ajay

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Java : Compile-time Versus Runtime optimization
While designing and development, one should think in terms of compile-time and run-time.It helps in understanding language basics in a better way.Let's understand this with a question below : What...
S

Java inheritance example
Answer these questions Answer the above question

Amazon written test for experience
For the below written test answer I defined with "*" at the end of the options NULL is the same as 0 for integer the same as blank for character * the same as 0 for integer and blank for character the...

Why Should We Learn Java Language?
Most of starters has confusion today that which of the programming language should be chosen to work on. It totally depends upon your need. As per my opinion, if you want to grow your career in IT industry...

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

Recommended Articles

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more