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

Anybody knows converting jsf primeface into css bootstrap?
We can use some bootstrap CSS classes and helpers but if we want give style to each UI control, we will need to set the primefaces theme to none. context-param param-name primefaces.THEME /param-name...
Pradeep
0 0
8
What exactly the work of constructor( default, parameterized) especially about instance variables initialization. Suppose user initialize the instance variables explicitly. Then what is the use of parameterized constructor over here.. Please reply me
Construct as the name suggest are used to construct the object. So, they are invoked only once and that too at the time of object creation. Default Constructor : Initialize all the instance variables...
Surekha
how to run a java program
go to command prompt then write the following-- For compilation: javac filename.java then press enter For run: java classname then press enter
Prem Kumar Pathak
Should we learn DBMS and RDBMS without any Java training?
java or i can say any programming language is not required to learn DBMS or RDBMS
Karthik
0 0
6

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

Ask a Question

Related Lessons

CONDITIONAL STATEMENT - IF ELSE
1. IF condition only if is true conditon is required. if(condition){//statements} 2. IF-ELSE condition 1. to check whether the condition will be true or false.syntax of if-else2. only 1 conditionif(condition){//statements}else{//statements} 3....

Android : Support multiple themes in an android application
If you are developing a theme based application to enhance user experience, then following steps needs to follow.We are taking example of an android application having 2 themes white and black.Step 1:Define...

10 Cool SQL Optimizations That Do Not Depend On The Cost Model
Today, we don’t want to talk about cost based optimisation, i.e. optimisations that depend on a database’s cost model. We’ll look into much simpler optimisations that can be implemented...

Java Collection
Collection: The collection provides an architecture like a list where we can store and manipulate the group of items. Through Collections, we can achieve operations such as searching, sorting, insertion,...

Introduction to Programming Languages
What is a Programming Language? A programming language is a formal computer language or constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages...

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