473,654 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Diff b/w String s="hello" and String s=new String("hello") ;

12 New Member
hello friends,

i have a doubt,what is the difference between

String s="Hello" and
String s=new String("Hello")

please help me.


Thanking u
Rahana Parveen
Nov 14 '07 #1
8 3492
heat84
118 New Member
Its the same.
Only one String object is shared by all string having same character sequence.
Nov 14 '07 #2
JosAH
11,448 Recognized Expert MVP
Its the same.
Only one String object is shared by all string having same character sequence.
This is definitely not true. The String s= "hello" initialization statement creates
one single interned String while String s = new String("hello") creates two String
objects: one interned the other one on the heap.

kind regards,

Jos
Nov 14 '07 #3
heat84
118 New Member
This is definitely not true. The String s= "hello" initialization statement creates
one single interned String while String s = new String("hello") creates two String
objects: one interned the other one on the heap.

kind regards,

Jos
Whats contained in an objects is the same but the references are the different i.e if you invoke s1.equals(s2) it returns true but s1==s2 returns false . Thats what I mearnt when I said they are the same.
Nov 14 '07 #4
JosAH
11,448 Recognized Expert MVP
Whats contained in an objects is the same but the references are the different i.e if you invoke s1.equals(s2) it returns true but s1==s2 returns false . Thats what I mearnt when I said they are the same.
I understand what you mean but 'same', 'equal', 'equivalent' and 'identical' have
very precise meanings. Be a bit sloppy with their meaning and it will rear its ugly
head one sad day.

kind regards,

Jos
Nov 14 '07 #5
heat84
118 New Member
This is definitely not true. The String s= "hello" initialization statement creates
one single interned String while String s = new String("hello") creates two String
objects: one interned the other one on the heap.

kind regards,

Jos
At compile time , String s="hello" and String s = new String("hello") are the same in the sense that the strings will be in the string pool.In this case only one String object is shared by all string having same character sequence. However , at runtime, an object is created in the program space .
Nov 19 '07 #6
JosAH
11,448 Recognized Expert MVP
At compile time , String s="hello" and String s = new String("hello") are the same in the sense that the strings will be in the string pool.In this case only one String object is shared by all string having same character sequence. However , at runtime, an object is created in the program space .
There is no string pool at compile time; javac just puts the string literal "hello"
in the .class file when it generates code for it. String s= "hello" generates
different byte code from what String s= new String("hello") does; check it with javap.

The two code fragments are definitely different. If all you want to see is "hello",
yes, then they're identical.

kind regards,

Jos
Nov 19 '07 #7
itsraghz
127 New Member
Yes, JosAh is absolutely correct.

They both are very much different! His explanations are appropriate.
Nov 19 '07 #8
heat84
118 New Member
There is no string pool at compile time; javac just puts the string literal "hello"
in the .class file when it generates code for it. String s= "hello" generates
different byte code from what String s= new String("hello") does; check it with javap.

The two code fragments are definitely different. If all you want to see is "hello",
yes, then they're identical.

kind regards,

Jos
Thanks , I got some enlightening .
Nov 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2187
by: ME | last post by:
I am trying to find a prewritten method for converting a string like this:"Hello World" to this "Hello\sWorld". I plan to use it to build a regular expression. Specifically I am looking for a method that can convert not just space (\s) but also any character that needs to be escaped in a given string. Thanks, Matt
23
6602
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
8
7574
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
9
3773
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in code. for example, this is bad practice: header.h #include <string> using std::string;
10
5330
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
5
9094
by: Andrea | last post by:
Hi, I'm a newbie i want to know how can i convert string to its binary representation, i want to write a program where an example string "hello world" is transformed in his binary representation "11010010101...." and viceversa and then print on the stdout the result of the two representation. thanks in advance, Andrea
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.