Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 30th, 2007, 05:12 AM
Authorized User
 
Join Date: Aug 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default about String

what the difference between

String s="q";

and


String s=new String("q");

 
Old September 4th, 2007, 06:29 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

if you just assign a string literal ["q" is called literal] compiler will check whether are there any String object having the same string pointed by them the i'll give the same reference to the object 's'. That means if there is a line of code like
Code:
String r="q";
 before the line
Code:
String s="q";
 both the variables 'r' and 's' will point to the same location.
On the other hand if you use the new keyword it'll allocate memory for the string object and give the reference to the String object.

- Rakesh
 
Old September 4th, 2007, 09:42 AM
Authorized User
 
Join Date: Aug 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks rajesh . i understood your point

 
Old September 9th, 2007, 03:16 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rakesh

what if the code

String r="q"

is not present before

String s="q"

will memory allocation not happen in this case ?

Rgds
Jo

 
Old September 14th, 2007, 07:53 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Jo,
     Here it is following concept of saving memory. When we create a String object, it checks in memory whether it exists or not. If exists, it will not create new object. If it is not exist, it will create new string object.
     So in this case, if there is no statement like String r="q", it will create new object in memory.


Venkat.K





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find a string in another string in vb6 satish_k VB How-To 3 March 30th, 2007 12:17 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
syntax to find a string in a string cole SQL Server 2000 2 October 10th, 2005 06:06 PM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.