Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| Search | Today's Posts | Mark Forums Read
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 April 24th, 2006, 02:26 AM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regarding String Initialization

Hi,
i need to know the differences (Memory allocation/For Garbage Collection) in initializing a String in the following two ways.

String str = null;

String str = "";


 
Old April 26th, 2006, 01:58 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

String str = null;

means u creating a reference only not initilizing it in memory

String str = "";

is same as
String str = new String("");
creating a reference and inilizied in memory with vale ""




 
Old April 27th, 2006, 04:07 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Diff. between String literal and String object :-

public class Str
{
    public static void main(String args[])
    {
        String s1 = "hello";

        String s2 = "hello";

        String s3 = new String("hello");

        String s4 = new String("hello");

    System.out.println("s1==s2\t" + (s1==s2));
    System.out.println("s1==s3\t" + (s1==s3));
    System.out.println("s3==s4\t" + (s3==s4));

    System.out.println("s1.equals(s2)\t" + (s1.equals(s2)));
    System.out.println("s1.equals(s3)\t" + (s1.equals(s3)));
    System.out.println("s3.equals(s4)\t" + (s3.equals(s4)));
    }
}

 
Old April 25th, 2009, 01:58 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Quote:
Originally Posted by yashtomar View Post
String str = "";

is same as
String str = new String("");
No, they are not the same, especially under the context of this question.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaration of variable without initialization v_kewlani Other Programming Languages 0 May 24th, 2007 01:51 AM
Property initialization problem VoodooCreator Pro Visual Basic 2005 1 June 29th, 2006 07:56 PM
c# comboBox Initialization problem ColdFusion ASP.NET 2.0 Basics 1 May 11th, 2006 12:44 PM
variable initialization vin_0x1 Classic ASP Databases 1 April 18th, 2006 06:46 AM
initialization vin_0x1 Classic ASP Professional 3 April 16th, 2006 06:40 AM





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