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

What is difference between C language and Java language?
C is more procedure-oriented while JAVA is data-oriented. Java is an Interpreted language while C is a compiled language. C uses the top-down {sharp & smooth} approach while JAVA uses the bottom-up...
Poonam
0 0
5
hi this iz sunil here I done with my b.sc in cs and planning to go fr java course shld I prefer for institution or a lecturer who teaches java at his own tuitions ...
i can teach you java the way you can play with real world thinking you don't need of knowledge of any other language. you can directly learn java
Sunil
0 0
5
Hi, We provide online instructor-led training. I am looking for Java Teacher/Instructor who can teach java via online. If anyone interested then contact me. I have to start the class from tomorrow onwards.
Hi, If you haven't finalised yet, I am interested in being an instructor. I will be teaching the newest version of Java, Java 10. Experience - 3 yrs as Software Engineer, BTech CSE, teaching Java to school and college students since 3 yrs.
Ujjwal Rana
Can you tell me some new features in JavaFX 2.0?
JavaFX is a software platform for creating and delivering desktop applications, as well as rich internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace...
Renuka
0 0
5
What is the difference between abstraction and encapsulation?
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example...
Neval

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

Ask a Question

Related Lessons

Puzzle Game coding
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import...

Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video

Tips for Entry Level Java Job Seeker
3 steps you should take to make a break as a Java developer: Step 1: Invest in improving your job hunting skills encompassing interviewing, networking, and resume writing skills. Step 2: Keep applying...
A

Akshay Shende

1 0
0

Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

JAVA Version History
Java Version History: There are many java versions that has been released. Current stable release of Java is Java SE 8. JDK Alpha and Beta (1995). JDK 1.0 (23rd Jan, 1996). JDK 1.1 (19th Feb, 1997). J2SE...
V

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