473,503 Members | 8,784 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 3484
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
2166
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...
23
6536
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
8
7547
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
3748
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...
10
5304
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...
5
9079
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...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
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...
0
7267
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,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.