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 4 of 4

Thread: Address of String instance

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Address of String instance

    AClass c=new AClass(“Hi”);
    System.out.print(c);// here it prints the address of  object c
    String s=new String(“Hi”);
    System.out.print(s);// here it prints Hi. But I need the address of s.

    Can anyone help give the code to find address of s ?
    Last edited by helloworld922; September 5th, 2010 at 11:48 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Address of String instance

    In both cases it's actually calling the toString() method. The default toString method for objects is to append the class name followed by the hashCode (default is the memory address).

    For obvious reasons, the String toString() method returns itself. However, the String class also overrides the hashCode() method so there's no easy way to retrieve the memory address of the string (I don't think it's possible without using JNI).

    The larger question is why would you want to know the memory address of strings? In Java, you can't manually modify this value, except by setting it to a "valid" object of that type or null.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Address of String instance

    I wanted to know the address for the reason that :
    String s1=new String("Hi");
    String s2=new String("Hi");
    System.out.print(s1==s2);// false
    s1=s2;
    System.out.print(s1==s2);// true
    I understand why it's false in the first case. If it's true in the second then what is true (is it the address?)
    what is there at the addresses of s1 and s2 ?
    Last edited by helloworld922; September 6th, 2010 at 03:27 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Address of String instance

    For object equality, you should be using the equals as opposed to equality with '==', which is more for primitives. Using your example:
    String s1 = new String("HI");
    String s2 = new String("HI");
    System.out.println(s1.equals(s2));
    For custom objects, you can override the Object.equals method to accomplish this task. In the case of String, you could also call intern() on your strings, which causes the String to be stored/recalled from memory, in which case the == will return true.
    Last edited by copeg; September 6th, 2010 at 02:24 PM.

  5. The Following User Says Thank You to copeg For This Useful Post:

    American (September 6th, 2010)

Similar Threads

  1. How to get your computer name and IP address?
    By JavaPF in forum Java Networking Tutorials
    Replies: 7
    Last Post: December 8th, 2011, 12:36 PM
  2. [SOLVED] Help with classes & instance methods
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 16
    Last Post: July 30th, 2010, 01:20 PM
  3. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 03:08 AM
  4. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  5. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM