9 replies
how many objects are created in the statement
String s=new String("Hello Java");
#objects
  • Profile picture of the author michael_gourlay
    Two, now tell us why
    {{ DiscussionBoard.errors[5379684].message }}
  • Profile picture of the author ScottAN
    How is it two? I must be really rusty in Java.
    {{ DiscussionBoard.errors[5381220].message }}
    • Profile picture of the author jacklarry
      Hi Sandeep Gupta, I think it is one.

      sql tutorials and online test papers on sqlatfinger.in
      Signature

      For sql tutorials, online test and books go to sqlatfinger.in

      {{ DiscussionBoard.errors[5383903].message }}
      • Profile picture of the author michael_gourlay
        Unless I'm the one that is rusty, "Hello Java" creates an object in the String pool and new String() creates a copy of that object.
        {{ DiscussionBoard.errors[5385610].message }}
        • Profile picture of the author frenchsquared
          Originally Posted by michael_gourlay View Post

          Unless I'm the one that is rusty, "Hello Java" creates an object in the String pool and new String() creates a copy of that object.
          I Agree. I think it is two.
          {{ DiscussionBoard.errors[5419889].message }}
  • Profile picture of the author johnchristopher
    It has to be One
    {{ DiscussionBoard.errors[5417070].message }}
  • Profile picture of the author gaetanoc
    its one object
    Signature
    An experienced technical programmer wants to JV with you


    I will build any kind of software, bots, web applications, desktop applications, mobile applications - you will handle marketing and sales.
    {{ DiscussionBoard.errors[5417106].message }}
    • Profile picture of the author michael_gourlay
      new String() creates a copy of "Hello Java" (which is an object itself), thus a second object.

      If it was the same, then "Hello Java"==s would be true, but this isn't the case.

      If we did String s = "Hello Java", and then did "Hello Java"==s, it would be true.

      If we use the equals method, s.equals("Hello Java"), it will be true because that is comparing the contents of the strings.

      Here's a quick example:

      public static void main(String[] args) {
      String a = "Hello Java";
      String b = new String("Hello Java");
      String c = b;
      String d = a;

      System.out.println("Hello Java"==a);
      System.out.println("Hello Java"==b);
      System.out.println("Hello Java"==c);
      System.out.println("Hello Java"==d);

      }

      This prints out

      true - a references the object "Hello Java"
      false - b doesn't reference the object "Hello Java"
      false - c doesn't reference the object "Hello Java"
      true - d references the object "Hello Java"

      When you use a literal in a java program, it creates an interned string object. So in the following:

      String s = new String("Hello Java");

      You've created an interned String object with "Hello Java" and a copy of that object in s.
      {{ DiscussionBoard.errors[5418363].message }}
      • Profile picture of the author Don Art
        well this is supposed to be a trick question, i'm pretty sure

        i am going to say '2' but it could be '1';

        The compiler will add "Hello Java" to the the String Literal Pool (if not already there!) thus creating a new object - but only if necessary

        then, when the actual line is executed at run-time a new object using "Hello Java" will certainly be created

        in no case is the 's' an object but rather a reference/pointer to the new object created (and not to the object in the string literal pool)
        {{ DiscussionBoard.errors[5419825].message }}

Trending Topics