Results 1 to 4 of 4

Thread: string doubt

  1. #1
    boy22 is offline Member
    Join Date
    Jul 2007
    Posts
    36
    Rep Power
    0

    Default string doubt

    what is the difference between following string intializations.
    Java Code:
    String s = "abc";
    String str = new String("abc");

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    s is a String.
    str is a copy of the String argument "abc". Since strings in java are immutable, the second statement is not necessary unless you need a copy of the argument string. See String api.

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    From what i learn so far, in Java, String is an object, not a primitive type
    The default way to create an object is by using the new keyword, like in the 2nd statement u wrote

    But as to the impractical way of using a new keyword each time u create a String -which the designer assume are going to be often that is, and it certainly does- the language designer deiced to put a shortcut to instantiate a String without using the new keyword, and also to make it look similar like how u would initialize a String in other programming language

  4. #4
    boy22 is offline Member
    Join Date
    Jul 2007
    Posts
    36
    Rep Power
    0

Similar Threads

  1. doubt
    By shaju_inspira in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-18-2008, 09:38 AM
  2. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 02:42 PM
  3. doubt about PRINT API
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 10:51 PM
  4. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:49 AM
  5. Doubt with classpath
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •