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
8 3484
Its the same.
Only one String object is shared by all string having same character sequence.
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
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.
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
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 .
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
Yes, JosAh is absolutely correct.
They both are very much different! His explanations are appropriate.
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 .
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: Hans |
last post by:
Hello,
Why all C/C++ guys write:
const char* str = "Hello";
or
const char str = "Hello";
|
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;
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |