473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String problem.

dmjpro
2,476 2GB
Plz guys look at this code carefully.

Expand|Select|Wrap|Line Numbers
  1. String a = "abc";
  2. String b = "abc";
  3. String c = new String("abc");
  4. System.out.println(a == b);
  5. System.out.println(a == c);
  6.  
  7. //and the output is true and false.
  8.  
  9.  
why not the output is false and false?
plz explain.

kind regards.
dmjpro.
Jun 1 '07 #1
7 1454
r035198x
13,262 8TB
Plz guys look at this code carefully.

Expand|Select|Wrap|Line Numbers
  1. String a = "abc";
  2. String b = "abc";
  3. String c = new String("abc");
  4. System.out.println(a == b);
  5. System.out.println(a == c);
  6.  
  7. //and the output is true and false.
  8.  
  9.  
why not the output is false and false?
plz explain.

kind regards.
dmjpro.
Think references and remember the String pool.
In
Expand|Select|Wrap|Line Numbers
  1.  String b = "abc";
no new oject is created because the literal "abc" already exists in the String pool so the reference b is merely made to point to the same memory that a is pointing to. In

Expand|Select|Wrap|Line Numbers
  1. String c = new String("abc");
The new keyword forces a new object to be created in memory disregarding what is in the String pool.
Jun 1 '07 #2
sumittyagi
202 Expert 100+
Plz guys look at this code carefully.

Expand|Select|Wrap|Line Numbers
  1. String a = "abc";
  2. String b = "abc";
  3. String c = new String("abc");
  4. System.out.println(a == b);
  5. System.out.println(a == c);
  6.  
  7. //and the output is true and false.
  8.  
  9.  
why not the output is false and false?
plz explain.

kind regards.
dmjpro.
This is due to string pool.
whenever you define a string, that string is dropped in a pool of strings. When you define another string with same char sequence, then new string object is not created, the same object kept in the pool is returned.

But if you create a string object by using new keyword, then java have no option other than creating new object as you directed it to do so.

That's why first two references are same but third one is not.
Jun 1 '07 #3
dmjpro
2,476 2GB
I came to know a good thing .

thanks for ur suggestions.

kind regards.
dmjpro.
Jun 1 '07 #4
JosAH
11,448 Expert 8TB
Also have a look at the String.intern() method; this method adds a String to
the String pool.

kind regards,

Jos
Jun 1 '07 #5
dmjpro
2,476 2GB
That means all time the String Object is not pooled every time.
right???

so the first print will not be true every time.
right???

if not then why String.intern()?????
plz help.

kind regards.
dmjpro.
Jun 1 '07 #6
JosAH
11,448 Expert 8TB
That means all time the String Object is not pooled every time.
right???

so the first print will not be true every time.
right???

if not then why String.intern()?????
plz help.

kind regards.
dmjpro.
String literals such as "abc" are always pooled and the following will always be true:
Expand|Select|Wrap|Line Numbers
  1. String a= "abc";
  2. String b= "abc"
  3. System.out.println(a == b); // true
  4.  
The following will always be false:
Expand|Select|Wrap|Line Numbers
  1. String c= new String("abc");
  2. System.out.println(a == c || b == c); // false
  3.  
And the following will be true again:
Expand|Select|Wrap|Line Numbers
  1. c= c.intern();
  2. System.out.println(a == c && b == c); // true
  3.  
kind regards,

Jos
Jun 1 '07 #7
dmjpro
2,476 2GB
Now the picture comes more clearer.
Thanks for ur suggestions.

kind regards.
dmjpro.
Jun 1 '07 #8

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

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
4
by: MooMaster | last post by:
After some google searching on the forum I couldn't find any topics that seemed to relate exactly to my problem, so hopefully someone can help me out... I'm running python 2.4.1 on a local Win2K...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
5
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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 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.