473,732 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strings.. Objects or not???

Hi all,

I am wondering why string's are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
' i expect this code to be executed
End If

If x1 is x2 then
' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true
objects?

Any thoughts would be appreciated!

Rigga.
Jul 21 '05
42 1644
In article <40************ *********@ptn-nntp-reader02.plus.n et>, s@v.c
says...
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?


Yes. It's called "String Interning" and the String.Intern method has a
description of what it's for. Basically (from the docs):

"The common language runtime conserves string storage by maintaining a
table, called the intern pool, that contains a single reference to each
unique literal string declared or created programmaticall y in your
program. Consequently, an instance of a literal string with a particular
value only exists once in the system."

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Jul 21 '05 #11
Hi Rigga,

Yes it is an object. (However for the rest nothing to add to Jon's
explanation)

Cor
Jul 21 '05 #12
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
From you I had expected more, the question is (see it yourself) ..... And
now everybody telling that a string object is a reference type or something
like that.


What would you have expected? It's similar to what's going on for all
the other reference types, so telling that string is a reference type
answers the question, IMO.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Jul 21 '05 #13
I did place an answer below.

:-)

Cor
Jul 21 '05 #14
* Patrick Steele [MVP] <pa*****@mvps.o rg> scripsit:
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?


Yes. It's called "String Interning" and the String.Intern method has a
description of what it's for. Basically (from the docs):

"The common language runtime conserves string storage by maintaining a
table, called the intern pool, that contains a single reference to each
unique literal string declared or created programmaticall y in your
program. Consequently, an instance of a literal string with a particular
value only exists once in the system."


Just to add that this is a common design approach, that has been taken in
VB6 too, for example.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Jul 21 '05 #15
Rigga,
In addition to the other comments.

Try the following:

Dim x1 As String = "veggie"
Dim x2 As String = "veg"

x1 = x1.Substring(0, 3)

If x1 = x2 Then
Debug.WriteLine ("i expect this code to be executed")
End If

If x1 Is x2 Then
Debug.WriteLine ("i do not expect this code to be executed")
End If

Both strings contain "veg", however because the second one was "calculated "
instead of being a constant it is a different string object on the heap.

You can use String.Copy to create a new instances of a string.

Dim x1 As String = "veg"
Dim x2 As String = String.Copy(x1)

Note String.Clone returns the same instance...

Hope this helps
Jay

"Rigga" <s@v.c> wrote in message
news:40******** *************@p tn-nntp-reader04.plus.n et...
Hi all,

I am wondering why string's are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
' i expect this code to be executed
End If

If x1 is x2 then
' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true objects?

Any thoughts would be appreciated!

Rigga.

Jul 21 '05 #16
Hey. dont ever change your second name to 'Mortis' !

LOL

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Rigga" <s@v.c> wrote in message
news:40******** *************@p tn-nntp-reader02.plus.n et...
Thanks all for your replies but I'm still confused.

So coding "veg" anywhere will only ever create one object?

and assigning that object to the reference variable as

x1 as string = "veg"

points to the same object as

x2 as string = "veg"

"veg" being the object?

actually, it has always been kind of strange hat you do not have to code

x1 as New String to instantiate the object.

So we are saying that string does not behave in the same way as other
objects?

Anyway, now that I know this I'll have to code around it.. thanks all..

Rigga.

"Rigga" <s@v.c> wrote in message
news:40******** *************@p tn-nntp-reader04.plus.n et...
Hi all,

I am wondering why string's are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
' i expect this code to be executed
End If

If x1 is x2 then
' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not

true
objects?

Any thoughts would be appreciated!

Rigga.


Jul 21 '05 #17
Perhaps I should have quoted my "change" word... but MSDN says:

<MSDN>
A String is called immutable because its value cannot be modified once it
has been created. Methods that appear to modify a String actually return a
new String containing the modification. If it is necessary to modify the
actual contents of a string-like object, use the System.Text.Str ingBuilder
class.
</MSDN>

This is what I was referring to... whoops

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Tom Spink <thomasdotspink atsp@mntlworldd otcom> wrote:
But let's not forget that they are immutable.... When you change the value of a string object, the object is destroyed and a brand new one is
created.
Um, no. You *can't* "change the value" of a string object. You can
change the value of a variable so that it's a reference to a different
string, but that doesn't necessarily destroy the old string or create a
new one.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #18
This is very true, but does not explain what is happening. The compiler
sees:

Dim x as String = "veg"
Dim y as String = "veg"

and says "hey, it is the same constant." Underneath the hood, some magic
happens. NOTE: This is actually the JIT compiler that does this magic and
not the initial IL compile. Tres kewl!

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ***
Think Outside the Box!
*************** *************** *************** ***
"Tom Spink" <thomasdotspink atsp@mntlworldd otcom> wrote in message
news:OS******** ******@TK2MSFTN GP12.phx.gbl...
But let's not forget that they are immutable.... When you change the value
of a string object, the object is destroyed and a brand new one is created.
--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:2l******** ****@uni-berlin.de...
* "=?Utf-8?B?RGF2aWQgV2l sbGlhbXM=?="

<Da***********@ discussions.mic rosoft.com> scripsit:
Remember that strings are ValueTypes, not ReferenceTypes.


No! Strings are not value types, they don't inherit from
'System.ValueTy pe':

\\\
Dim s As String = "Hello World"
MsgBox(TypeOf s Is ValueType)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Jul 21 '05 #19
The compiler recognizes you setting it to the same string and creates IL
that points to a single spot in memory. Run this test as a console
application and it will become crystal clear:

Module Module1

Private veg As String = "veg"

Sub Main()
Test1()
Test2()
Test3()

Console.Read()
End Sub

Sub Test1()
Console.WriteLi ne("TEST 1")
Console.WriteLi ne("-------------")

Dim x1 As String = "veg"
Dim x2 As String = "veg"

If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If

Console.WriteLi ne("")
End Sub

Sub Test2()
Console.WriteLi ne("TEST 2")
Console.WriteLi ne("-------------")

Dim x1 As String = veg
Dim x2 As String = veg

If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If

Console.WriteLi ne("")
End Sub

Sub Test3()
Console.WriteLi ne("TEST 3")
Console.WriteLi ne("-------------")

Dim sb As New Text.StringBuil der

sb.Append("v")
sb.Append("e")
sb.Append("g")

Dim x1 As String = "veg"
Dim x2 As String = sb.ToString()

If x1 = x2 Then
Console.WriteLi ne("x1 = x2")
End If

If x1 Is x2 Then
Console.WriteLi ne("x1 is x2")
End If

Console.WriteLi ne("")
End Sub

End Module

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ***
Think Outside the Box!
*************** *************** *************** ***
"Rigga" <s@v.c> wrote in message
news:40******** *************@p tn-nntp-reader04.plus.n et...
Hi all,

I am wondering why string's are not true objects?.... Let me explain...

If i write the code

Dim x1 as String = "veg"
Dim x2 as String = "veg"

If x1 = x2 then
' i expect this code to be executed
End If

If x1 is x2 then
' i do not expect this code to be executed
End If

However the second lot of code is executed!

Is this correct behavior??? if so is it true then that strings are not true objects?

Any thoughts would be appreciated!

Rigga.

Jul 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3135
by: Colin Savage | last post by:
Please could somebody explain what an "atomized string" is? I have been doing loops in the msdn around XmlNameTable and NameTable but neither explain what an atomized string is. The example for one adds a string to two different NameTables and then compares them and they are considered equal? I must be having a real THICK day today coz I'm just not getting it. Colin
10
9034
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i think!!). But I want to use malloc. Each string is at most 50 characters long, and there may be zero to thousands of lines. How do I actually start the array? I have seen char **array etc. At first I tried char *array but I think that gives 50...
73
2934
by: Rigga | last post by:
Hi all, I am wondering why string's are not true objects?.... Let me explain... If i write the code Dim x1 as String = "veg" Dim x2 as String = "veg" If x1 = x2 then
52
3963
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When I did help(sum) the docstring was very number-centric: It went further, and precluded its use on strings:
74
3145
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
95
5389
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ____________________________________ | | | ------------------ | | | BUTTON | | | ...
1
2022
by: Edward K Ream | last post by:
Hello all. I'm tracking down memory leaks in my app. To do this I wrote a script to show the numbers of each different type of object. But it doesn't show strings! Here is the script: import gc,types def printDict(d): keys = d.keys() ; keys.sort() print '-' * 30 for key in keys:
16
1875
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" Ok. It's a broad, and maybe a very silly question to ask, but still. I mean, what good has it brought to us? What advantages immutable strings have against mutable ones?
9
2682
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane T. Smith" "Fred Barzowsky" "Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc. etc. I am wondering what the efficient way to do this in code might be. The dumb and brute-force way would be to loop through the content...
0
8774
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
9447
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...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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
6031
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.