Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Question on String class

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question on String class

    Hi, I know that to create an object we use the new keyword which then makes the variable a reference variable.

    Are String variables already objects? if so how if we dont use the new keyword, and why have they implemented the String class to do this, thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on String class

    There are String variables and there are String objects.
    String aStr; // define a String variable (it has a null value)
    aStr = "A string"; // give the variable a value
    Strings are handled differently in a shortcut manner so that you are not required to use new.
    Strings are used a lot in programs so the authors decided to make it easier to use them.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on String class

    At what point does a String variable become an object?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on String class

    A variable is always a variable. Class variables (as opposed to primitives) can either have a null value or a reference to(address of) an object.
    They "point" to the object.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on String class

    ok, can you answer me two things please:

    At what point does a class variable (example: String example have a reference to an object?

    Also, can you tell me is there a point in having a class variable with a null value, is this not pointless?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on String class

    At what point does a class variable (example: String example have a reference
    When it is assigned one. That is true of all variables. They do not have values until assigned one.

    point in having a class variable with a null value, is this not pointless?
    Most of the time yes. But you don't always know what value you are going to assign a variable at the time you define it. You define it so other code can see it (its definition is in scope), later you give it a value when you know what value you want it to have.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on String class

    Thanks for replying, but what im asking is how are String objects handled differently so that we do not need to use the new keyword?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on String class

    The compiler takes care of the details.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on String class

    Thank you

Similar Threads

  1. Java array and string question
    By colerelm in forum Collections and Generics
    Replies: 2
    Last Post: September 14th, 2011, 11:50 PM
  2. Noob question: Why .class?
    By Shaybay92 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 22nd, 2011, 04:56 AM
  3. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  4. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  5. .class to .exe question
    By james137 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2009, 09:18 PM