Difference in String declaration

Hello All,
Can any one please tell me whats the difference between following String declarations:
a) String str = "abc";
b) String str = new String( "abc" );
Also please tell me which one is correct way of declaration and in which sence.
Thanks in advance..........

both are different
the first one creates one String object and one reference variable. The second one creates two string objects, one in non-pool memory and the reference variable will refer to it., the second object is the literal that will be placed in the pool.

Similar Messages

  • What is the difference between string != null and null !=string ?

    Hi,
    what is the difference between string != null and null != string ?
    which is the best option ?
    Thanks
    user8729783

    Like you've presented it, nothing.  There is no difference and neither is the "better option".

  • What is the difference between String Constant and Empty String Constant

    What is the difference between string constant which does not contain any value and the Empty string constant?
    While testing a VI which contain a normal string constant in VI analyzer, it gives error to change string constant with the empty string constant?
    Please Reply
    prabhakant
    Regards
    Prabhakant Patil

    Readability.
    Functionally, they are the same. From a coding standpoint, the Empty String Constant is unambiguous.
    It is empty and will always be; good for initialization. Also, because you can not type a value into and Empty String Constant, someone would need to conciously replace it to set a 'default' value that is something other than NULL.
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Difference between String and final String

    Hi friends,
    This is Ramana. Can u suggest me in this Question
    What is the difference between String and final String? Means
    String str="hai";
    final String str="hai";
    Regards,
    Ramana.

    *******REPEAT POST***********
    We already answered your question why post in a different section?
    http://forum.java.sun.com/thread.jspa?threadID=5201549

  • What Are the Differences Between String and StringBuffer?

    Both String and StringBuffer are final classes. StringBuffer grows in size dynamically. Could people help to detail other differences between String and StringBuffer?

    String is immutable. In other words, once it is made, the contents of that instance of String cannot be changed (easily). The size and characters cannot be changed, and code can take advantage of this fact to optimize space. Also, once you have a reference to a string, you do not need to worry about it changing at all, so you can eliminate constant tests to verify things such as size in another class that may want to hold onto the string.
    StringBuffer, as you noticed, can change dynamically. While this provides flexibility, it cannot be optimized and assumptions cannot be made as with strings. If a class holds a reference to a StringBuffer that it does not own, there is a possibility that it may change things such as size when the class tries to use it later. StringBuffer is also less efficient in handling space then an equivalent String. The character array to hold a String is exactly the length to hold all the characters. StringBuffer, on the other hand, adds a 16 character buffer of array space for possible expansions. Also, the size of the internal array doubles when its internal capacity is exceeded. So, unless you explicitly maintain the size, a StringBuffer will take up more memory then an equivalent String, and if your program needs thousands of instances of Strings (which is not uncommon), that space can add up.
    I hope this helps clarify some things for you.
    -JBoeing

  • What's the difference between String() - as String - toString();

    Hello,
    I'm wondering what's the difference between this 3 ways of getting around.

    There is a great difference between String(), as String and toString() methods.
    String(param) -- the more general and safe method.
    Returns "null" (string!) if param is null;
    Returns result of param.toString() method if it is defined in class;
    Returns string representation of the param (returns "[object Object]" if param is Object);
    param asString --
    Returns string if param is a type of String;
    Returns null otherway (if param is custom class, int or other type);
    param.toString() -- call of toString() member function
    throws an exception if param is null;
    compile-time error if toString() method is not defined in param (if your custom class do not have or inherits this function);
    returns result of toString() function
    Exists also ""+param and param+"" ways. They are similar to ""+String(param) and String(param)+""
    Pay attention:
    var ss:String = null;
    trace(String(ss)==(ss as String)); // Returns false as "null" not equal to null

  • Difference between String=""; and String =null

    I want to know the difference especially the ADVANTAGE between the following-
    1. String s="";
    String s="ABC"
    and
    String s;
    String s="ABC";
    or String s=null;
    String s="ABC";
    2. Object anyObject;
    anyObject-new Object();
    and
    Object anyObject=null;
    anyObject=new Object();

    Tanvir007 wrote:
    I want to know the difference especially the ADVANTAGE between the following-
    1. String s="";s points to the empty string--the String object containing no characters.
    String s="ABC"s points to the String object containing the characters "ABC"
    String s; Declares String reference varialbe s. Doesn't not assign it any value. If it's a local variable, it won't have a value until you explicitly assign it. If it's a member variable, it will be given the value null when the enclosing object is created.
    String s="ABC";Already covered above.
    or String s=null;s does not point to any object.
    String s="ABC";???
    You've already asked this twice.
    Are you talking about doing
    String s = null;
    String s = "ABC";right after each other?
    You can't do that. You can only declare a variable once.
    If you mean
    String s = null;
    s = "ABC";It just does what I described above, one after the other.
    2. Object anyObject;
    anyObject-new Object();
    and
    Object anyObject=null;
    anyObject=new Object();As above: In the first case, its value is undefined until the new Object line if it's a local, or it's null if it's a member.
    As for which is better, it depends what you're doing.

  • Difference in String joining

    String m="Hello"
    Is there any difference between
    m=m+" Java";
    and
    m=m.concat(" Java");There must be some difference I guess, but not sure where.
    Can anyone explain please?

    Another interview question?
    Adding strings with + gets compiled into instantiations and invocations of StringBuffer or StringBuilder, invoking append() on it, and then calling toString on it. That is, unless the stuff you're adding is all immutable at compile time, in which case the concatenation happens at compile time and the class just has a big string.
    .concat(), I presume, creates a new String object right there and doesn't get transmogrified into anything else at compile time.
    As such, if you're doing a lot of string concatenations, or if the concatenations are built with immutable values, it's more efficient to use +. On the other hand if you're doing a single concatenation using a variable, .concat() is probably more efficient.
    But these are just educated guesses. Try experimenting with both, disassembling the class files with javap to see what happened, looking at the source code, etc.

  • Difference in string comparisons between varchar and nvarchar

    These statements were executed on SQL Server 2008 R2 Enterprise.  Collation is the default SQL_Latin1_General_CP1_CI_AS.
    Any ideas why the difference in behavior?
    DECLARE @StringWithCharZero VARCHAR(100), @StringWithoutCharZero VARCHAR(100);
    SELECT @StringWithCharZero = CHAR(0) + 'TEST', @StringWithoutCharZero = 'TEST';
    IF @StringWithCharZero = @StringWithoutCharZero
    BEGIN
    PRINT 'VARCHAR EQUAL';
    select ASCII(LEFT(@StringWithCharZero, 1)), ASCII(LEFT(@StringWithoutCharZero, 1));
    END
    ELSE
    BEGIN
    PRINT 'VARCHAR NOT EQUAL';
    select ASCII(LEFT(@StringWithCharZero, 1)), ASCII(LEFT(@StringWithoutCharZero, 1));
    END;
    GO
    DECLARE @StringWithCharZero NVARCHAR(100), @StringWithoutCharZero NVARCHAR(100);
    SELECT @StringWithCharZero = NCHAR(0) + N'TEST', @StringWithoutCharZero = N'TEST';
    IF @StringWithCharZero = @StringWithoutCharZero
    BEGIN
    PRINT 'NVARCHAR EQUAL';
    select UNICODE(LEFT(@StringWithCharZero, 1)), UNICODE(LEFT(@StringWithoutCharZero, 1));
    END
    ELSE
    BEGIN
    PRINT 'NVARCHAR NOT EQUAL';
    select UNICODE(LEFT(@StringWithCharZero, 1)), UNICODE(LEFT(@StringWithoutCharZero, 1));
    END;
    GO

    Hello,
    I got the followng result after run the query on SQL Server 2008R2 SP2 Enterprise edition:
    SQL Server specific version: Microsoft SQL Server 2008 R2 (SP2) - 10.50.4260.0 (X64)   Jul 11 2012 15:47:13   Copyright (c) Microsoft Corporation  Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack
    1) (Hypervisor)
    Please install the latest Service Pack for SQL Server 2008R2, we can get it from the link below:
    How to obtain the latest service pack for SQL Server 2008 R2:
    http://support.microsoft.com/kb/2527041
    Regards,
    Elvis Long
    TechNet Community Support

  • Difference between Scriptlet & Declarations

    Hello all,
    I cannot distinguish the difference in using a scriptlet to write methods and a declaration within a JSP?
    Can someone explain this please?

    Code generated from JSP scriptlets is placed in the jspService method, so you cannot define a method in a scriplet.  JSP declarations are placed outside of jspService and so you could declare a method if you wanted (e.g override jspInit, add a utility method).
    If you are declaring variables, scriptlets are safer because they are local to _jspService, and each request gets its own copy.  Declaration variables are instance variables and are therefore shared by all threads, so thread-safety is an issue.
    The best way to understand all this is to look at the servlet code generated from your JSP. This is not always easy to find, but it can be very useful.
    Good Luck

  • Limitations of public string declarations

    Is there anylimit for declaring number of public string in a java file.
    I have declared some 3300 Public String in a .java file as shown below
    public class JbnIntlLabel implements Serializable {
    public Properties prop = new Properties();
    //start of new 2 series labels
    public String Text_2A= null;
    public String securityname_2A = null;
    public String eqtTool_2A = null;
    3300 such declarations.
    once i try to create a extra string and trying to compile..,its giving the below error
    /devusr8/web61/WebLogicServer/weblogic61/config/NewWeb61Domain/applications/demo/WEB
    -INF/src/com/apollo/services/bean > demo.sh JbnIntlLabel.java
    An exception has occurred in the compiler (1.3.1-rc2). Please file a bug at the Java Devel
    oper Connection (http://java.sun.com/cgi-bin/bugreport.cgi). Include your program and the
    following diagnostic in your report. Thank you.
    java.lang.StackOverflowError
    at com.sun.tools.javac.v8.code.ClassWriter.writeFields(ClassWriter.java:588)
    at
    Can anybody help me out..
    Thanks and regards
    vijay
    Tata Consultancy Services.
    Mumbai -India

    Is there anylimit for declaring number of public
    string in a java file.
    I have declared some 3300 Public String in a .java
    file as shown below
    public class JbnIntlLabel implements Serializable {
    public Properties prop = new Properties();
    //start of new 2 series labels
    public String Text_2A= null;
    public String securityname_2A = null;
    public String eqtTool_2A = null;
    3300 such declarations.
    once i try to create a extra string and trying to
    compile..,its giving the below errorThese are member variables.
    The Java language (Java Language Specification) does not limit them.
    The JVM however does (Java Vitual Machine Specification.) However you are no where close to that limit.
    (Note that I do not consider this a great design. Unless you have profiled this under load I would simply keep the values in a hash table and use a named argument to retrieve each value.)
    >
    /devusr8/web61/WebLogicServer/weblogic61/config/NewWeb6
    Domain/applications/demo/WEB
    -INF/src/com/apollo/services/bean > demo.sh
    JbnIntlLabel.java
    An exception has occurred in the compiler (1.3.1-rc2).
    Please file a bug at the Java Devel
    oper Connection
    (http://java.sun.com/cgi-bin/bugreport.cgi). Include
    your program and the
    following diagnostic in your report. Thank you.
    java.lang.StackOverflowError
    at
    com.sun.tools.javac.v8.code.ClassWriter.writeFields(Cla
    sWriter.java:588)As pointed out this is a compile time error. When you deploy something to a container it needs to create wrappers to support it.
    So it compiles code. And the compiler is most likely written in java. So I would guess that your container server needs to have its stack space increased. This might be the server itself or it might be a property within the server depending on how it actually works. (And I have no idea which it would be.)

  • Shorten String declarations to one line

    I tried to shorten my declaration lines but get error. Please advise what I am doing wrong:
    I tried to shoten these declarations:
    String box = "";
    String circle = "";
    String rectangle = "";To:
    String box, circle, rectangle = "";
    Also tried:
    String (box, circle, rectangle) = "";

    String box= "", circle = "", rectangle = "";

  • Please tell basic difference between "" and null  for String  variable.

    1.What is difference between String strRemitInfo = "" and String strRemitInfo = null?
    2. Which one is good practice while coding ?

    1.What is difference between String strRemitInfo = ""
    and String strRemitInfo = null?Emptry string and nul reference
    >
    2. Which one is good practice while coding ?Depends on what you want to do.

  • Help needed to understand String object

    Hi all,
    I want to understand the difference in statements
    new String("") and string declared as "".
    Is this only a shortcut or is there any other difference like re-using the string objects created in the pool?
    what is the value in the String instantiated as new String()
    Thanx

    Hi all,
    I want to understand the difference in statements
    new String("") and string declared as "".
    Is this only a shortcut or is there any other
    difference like re-using the string objects created in
    the pool?
    what is the value in the String instantiated as new
    String()
    ThanxLiteral strings are internalized and reused, whereas new String always creates a new object.
    Thus
    String s = "";
    is better than
    String s = new String("");
    The purpose of the String copy constructor is not made explicit in the API docs, but if you look at Sun's code, you will see that it can serve a purpose:
    Substrings reference the same underlying char array as the String they are a substring of - they just have a different offset into that array, and a different length. The copy constructor however, creates a String with a new underlying char array, that contains only the relevant 'subarray' of the original String' sunderlying char array. So, if your code receives a large String, and then discards that String and passes around a substring of it, you might find you save a great deal of memory by using new String(s.substring(x, y));
    Note however that these comments are based solely on Sun's core API code, and it might not be the case that other vendors implement String the same way, or that Sun will continue to do so into the future.

  • What is the difference between Public and Protected Attributes

    hi ppl,
             what is the difference if i declare attributes under public or protected to access the private method.for both (public or private) section iam getting the same result.
    CLASS CL DEFINITION.
    *protected section.
      public section.
    DATA: M TYPE I VALUE 100.
    private SECTION.
    METHODS: FREE.
    ENDCLASS.
      CLASS CL IMPLEMENTATION.
       METHOD: FREE.
         WRITE:/ M.
         ENDMETHOD.
      ENDCLASS.
    CLASS CL1 DEFINITION INHERITING FROM CL.
    public section.
    METHODS: FREE1.
    ENDCLASS.
      CLASS CL1 IMPLEMENTATION.
         METHOD: FREE1.
         WRITE:/ M.
         ENDMETHOD.
      ENDCLASS.
    DATA: OBJ  TYPE REF TO CL1.
      START-OF-SELECTION.
      CREATE OBJECT : OBJ.
      END-OF-SELECTION.
      CALL METHOD: OBJ->FREE1.
    Please use meaningful subject in future questions and Post in correct forum*
    Please learn to use  tags around your ABAP
    Edited by: Vijay Babu Dudla on Dec 1, 2008 12:37 AM
    Edited by: Matt on Dec 1, 2008 4:16 PM

    hi,
        Iam really soory people..It wont repeat again.pls guide me in this regard
    hi ppl,
    what is the difference if i declare attributes under public or protected to access the private method.for both (public or private) section iam getting the same result.
    CLASS CL DEFINITION.
    *protected section.
    public section.
    DATA: M TYPE I VALUE 100.
    private SECTION.
    METHODS: FREE.
    ENDCLASS.
    CLASS CL IMPLEMENTATION.
    METHOD: FREE.
    WRITE:/ M.
    ENDMETHOD.
    ENDCLASS.
    CLASS CL1 DEFINITION INHERITING FROM CL.
    public section.
    METHODS: FREE1.
    ENDCLASS.
    CLASS CL1 IMPLEMENTATION.
    METHOD: FREE1.
    WRITE:/ M.
    ENDMETHOD.
    ENDCLASS.
    DATA: OBJ TYPE REF TO CL1.
    START-OF-SELECTION.
    CREATE OBJECT : OBJ.
    END-OF-SELECTION.
    CALL METHOD: OBJ->FREE1.
    Edited by: Matt on Dec 1, 2008 4:18 PM

Maybe you are looking for

  • Transforming non-theme data in Mapviewer

    Hi, We have generated a request to display a linestring over the top of a map base. At each end of the linestring are a marker to denote the start and end of the string. The challenge arises when we transform the map base to a desired projection (SRI

  • Dreaded CS3 Updater Loop/Hang

    After a new installing CS3 (from DVD) and launching Photoshop, Adobe Updater launches automatically in the background and promptly goes into a 100% CPU loop. It's hung at the dialog Adobe Updater Checking for Updates for Adobe Updater The dialog show

  • Collective order processing

    I have gone through the threads in w.r.t collective order and direct production. the sap help (IDES too) and the ERP documentation. I still cannot trigger get a process order to be directly created for the subcomponent when I convert the parent plann

  • 2 Concurrent vMotions saturate a 10GB link with 4.1?

    This is a great discussion going on over at VMware Communities and is very applicable to UCS so I'm cross posting here. 2 Concurrent vMotions saturate a 10GB link with 4.1? The discussion is being held between VMware and Cisco folks and the desired o

  • OFFICEJET 6500A PLUS PRINTER

    Print que shows job printing.  Printer continues to show ou of paper even though paper is in try correctly.   It sounds like it is trying to pull the paper through but no copy.  I have checked for jams - none; turned the printer off and back on - no