String reference count

Hi,
is copy a string to another string a real copy or is it just an increment of the reference count to it. For example if used like this:
private void foo()
     String data = getFooData();
     lastData = data;
     if (someThingTodo) {
          // do something
          lastData = null;
}If it is a question of reference count, then this code only increment, and eventually decrement it, right ?

Assigning to a reference type--String, Object, List, MyClas, etc.--never copies the object. It always just causes the reference on the left side to point at the same object that the reference on the right side points to.
Java doesn't use reference counting for its memory management. But if your question about decrementing is in the vein, "Will the VM automatically detect when an object can no longer be reached and make its memory available for use by other objects?" then then answer is yes, that's taken care of for you.

Similar Messages

  • NSNumber Reference Count Question

    I'm working my way through Stephen Kochan's Programming in Objective-C right now, and I've just finished Chapter 17 on memory management. The first exercise is to write a program to determine the effect on reference counts of adding and removing objects in a dictionary object. Anyway, I've got the following code:
    #import <Foundation/Foundation.h>
    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSNumber *num1, *num2, *num3;
    NSString *string1, *string2, *string3;
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity: 3];
    num1 = [NSNumber numberWithInt: 1];
    num2 = [NSNumber numberWithInt: 2];
    num3 = [NSNumber numberWithInt: 3];
    string1 = [NSString stringWithString: @"first string"];
    string2 = [NSString stringWithString: @"second string"];
    string3 = [NSString stringWithString: @"third string"];
    printf("\n num1 retain count before dictionary: %x\n", [num1 retainCount]);
    printf("string1 retain count before dictionary: %x\n", [string1 retainCount]);
    printf(" num2 retain count before dictionary: %x\n", [num2 retainCount]);
    printf("string2 retain count before dictionary: %x\n", [string2 retainCount]);
    printf(" num3 retain count before dictionary: %x\n", [num3 retainCount]);
    printf("string3 retain count before dictionary: %x\n", [string3 retainCount]);
    [pool release];
    return 0;
    When I compile and run it, I get the following:
    num1 retain count before dictionary: 2
    string1 retain count before dictionary: 1
    num2 retain count before dictionary: 2
    string2 retain count before dictionary: 1
    num3 retain count before dictionary: 2
    string3 retain count before dictionary: 1
    Can anyone tell me why the NSNumber objects have a reference count of 2 after creation, while the strings only have a count of 1? I expected both types of objects to have a reference count of 1 after initialization.

    NSNumber itself retains them. Notice, if you set them all to the same value, they'll all just point to a single object.
    It's informative to check the addresses with something like:
    printf("%x\n%x\n%x\n, num1, num2, num3);

  • Do Java interfaces have automatic reference counting?

    I'm actually a Delphi developer trying to understand interfaces and I'm curious about the way Java implements them.
    Here is a simple Delphi psuedo-code snippet:
    var
    // interface variable
    Intf : IInterface;
    // object reference variable
    Car : TCar;
    begin
    // instantiate TCar
    Car := TCar.Create;
    // obtain IInterface interface for Car object
    Intf := Car; // the reference count for Car is now 1
    Intf :=Nil;
    // After setting Intf to nil, the reference count is decreased to 0 and Car is automatically freed.
    Car.Free; // EXCEPTION! Car was automatically freed because the only interface reference had a reference count of 0
    What would happen in Java with the above code?
    Does Java automatically reference count all objects with interfaces?
    I know Java reference counts all objects in general so they are automatically destroyed, but I'm trying to understand the difference in interface implementation.
    Thanks for any help!

    Thanks for the replies!
    Sorry about my confusion with Java...
    Does Java support interfaces? If so, can't you have a interface reference variable like I have in my Delphi code?
    Also, if Java does support interfaces, do they have any other purposes aside from creating a list of functions/methods that can be used as a contract to interact with an object?
    Interfaces seem really useful since you can have an IDriveable interface (for example), and any unrelated classes can implement it... (eg. TBoat class, TCar class, etc) even if the classes do not have a common ancestor class.
    However, in Delphi interfaces also automatically reference count so once you create an interface reference you have to be very careful so the interface doesn't automatically free the object it is pointing to...

  • Posting string reference 961 not planned

    Hi
    From the output list of MB51, while I try to open a material document with Mvt Type 961(Backflush GI for Order), the system throws out a error "Posting string reference 961 not planned".
    The same error appears when I try to access from Production Order -> Documented Goods Mvt -> Material document (with 961 Mvt).
    Material documents is displayed for all other Mvt types.
    Why is it so happening?
    Thanks
    Velu

    Dear Velu,
    Please check SE16 transaction:
    Table: T156
    BWART BUSTR
    961   961    <<<<<<<<<<<<
    This is the reason for the error message you are facing.
    After 4.6A version, it is not possible to create custom movement type
    by using a custom posting string reference.
    Possible solution:
    1) Deleting the movement and recreating the movement again in
       reference to a standard movement. This is the best and recommendable
       way.
    2) Use SE16 and manually change the reference movement to a standard
       movement.
    3)  Release 470 has additional table MIGO_T156 that has to be updated if you want to continue using a customer movement type that does not reference a standard movement in T156 but i think you have to tested it out as it is not supported by SAP Support, this is modification. Keep fields XTRANSFER_MVT,
    XSTOCK_TYPE_MVT, XREMOVE_FROM_MVT and XPLACE_IN_MVT as initial.
    Regards,
    ian Wong Loke Foong

  • Bpc  install issue : string reference not set to an instance of a string.

    hi
    i am trying to install BPC server in my machine . after giving bw credential i am getting below error
    bpc issue string reference not set to an instance of a string. parameter name s
    does any one came across this?  what might be wrong here?
    please help me

    Hi
    Once you install the ABAP and .NET components, the server manager would be part of your program list. It would be something like SAP->BPC Server 7.0. Click on the server manager and in the server menu you will find 'Diagnostic' option as this would give you a good picture about the installation success post installation.
    During the server manager installatio process, you would be alerted if any of the pre requisite software is not installed on your server machine. Please check the installation guide for detailed steps.
    Thanks,
    Santosh

  • Using reference counter with 6602 board

    I want to control a 6602 card in a Labview application so that I can count events in channels 1-6 depending on a reference counter in channel 0. I've set up channel 0 to count from a fixed value down to 0, and I am using hardware triggering to start all counters. Now I need something like a stop trigger to stop my event counters simultaneously when counter 0 reaches zero.
    I tried to use counter 0 output as a gate for the others, but without success. Unfortunately, the help files for the TIO counter VI's are pretty lousy (nudge, nudge), so I'd be grateful if someone could give me a hint how to solve this problem.
    Sebastian

    Hi Sebastian (whatthehellamithinking),
    Nice name I think your idea about using counter 0 output as the gate signal is the right idea. However, I would make sure to configure a couple things. The first thing I would do is configure counter 0 output for toggle mode. This means when the counter rolls beyond zero it will toggle it's output. I would alsoe configure the output to be configured as high-to-low logic. Then I would connect it to the gate of all the other counters (make sure to configure a gate on each counter). The gate should initially be high on each counter since the output of counter zero should be high. Then when it rolls or reaches terminal count it will toggle the output which will gate each of the other counters.
    Anyway, not sure i
    f you tried it that way but I think it shoud work. Hope that helps. Have a good day.
    Ron

  • "reference count underflow" printing NSDocument using "Save as PDF"

    Hello, I'm getting a "reference count underflow" error trying to do a "Save as PDF" in a NSDocument class using the printOperationWithSettings method. There are no errors sending to the printer or viewing the output in preview. I found the post http://discussions.apple.com/thread.jspa?messageID=8708986&tstart=0 which was posted a year ago showing a 10 line program with the same error. I've got garbage collection turned on.
    Is this still a know problem?
    Thanks

    Oh I do get the "reference count underflow" when compiling and running the above 10-line program.
    If I create a new "Document-based" project and setting "Garbage Collection" to required, add an IBOutlet variable to reference the NSTextField generated, and add the following required method for printing:
    - (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError
    NSPrintInfo *tempPrintInfo = [self printInfo];
    if ([printSettings count]) {
    tempPrintInfo = [[tempPrintInfo copy] autorelease];
    [[tempPrintInfo dictionary] addEntriesFromDictionary:printSettings];
    // textField is an IBOutlet variable
    NSPrintOperation *op = [NSPrintOperation printOperationWithView:textField printInfo:tempPrintInfo];
    return op;
    I can print and "Open PDF in Preview" without errors, but get the "reference count underflow" with the "Save as PDF".
    The sample application "SimpleStickies" created in 2009-10-14 and has by default Garbage Collection required, gets the same errors.
    Is there a simple solution to creating a document-based project that allows "Save as PDF" to work.
    This is a common function. I know I'm missing something obvious.
    Thanks!

  • Increase Registry Reference Count with Labview Installer

    I am trying to create an installer that is using an ActiveX control.  There are instructions on what files need to be installed and registered (See attached pdf).  I think I have the files going to the proper destinations and the ones that require registration I have checked the register COM in the source file settings section.  However I am unsure about two items.
    1.  There is a file (IAT_YUV.ax that needs to be registered, how do I go about doing that?
    2. There is a statement in the instructions as follows:
    All files marked with *) must be registered using regsvr32.exe. It is very important to copy all files to the target computer, before registering these files. Most setup toolkits have an option to automate this task.
    For all files marked with **), you have increase their reference counts in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curr​entVersion\SharedDLLs. Most setup toolkits have options to perform this step automatically.
    I am not sure how to accomplish this in the Installer?
    Regards
    Terry.
    Attachments:
    IC Image Control Deployment.pdf ‏34 KB

    Hi Terry,
    The answer to your question is quite involved. We have this knowledge base article that you can follow for registering (IAT_YUV.ax and similar files. You will write a LabVIEW executable that uses the System Exec VI to register the ActiveX dll. To include them in the installer, under the Installer Properties»Advanced..., select Run executable after installation and add the above executable. Therefore when the installer is finished your executable that registers the dll will automatically run. While this process is pretty involved that should be all you need to do.  Hope this helps!
    Regards,
    Travis Ann
    Customer Education Product Marketing Manager
    National Instruments

  • Audio capture filter reference count causing leak

    I have a purplexing problem.
    I have written my own audio and video capture filters and they work fine. I have tested with FMLE, graphedit and various other tools which i wrote myself. When i click "Stop" on the stream or close FMLE, the reference count on the audio capture filter is not release to 0, thus causing a memory leak. The leak is not a big problem, but my filters have to do some important processing in their destructors, which dont get called because its not being released.
    To purplex the problem further, my video capture filter is not having this problem, only my audio.
    I have written several more capture filters that literally do nothing other than register themselves to explore this problem and only the audio filters seem to be leaking. Anyone else seen such a bizare problem ?
    I am using FMLE 3.1.0.8703

    Thank you for your quick reply!
    However I cant manage to find the panic logs in the library/logs/ or any of its directorys. Ive looked for this logs before and my guess is that they dont get created if the panic occurs during boot.

  • Using this as String reference

    Hi friends,
    Can anybody help find my mistake in the following code.?
    public class ThisCheck
    { public ThisCheck()
    {System.out.printf("%s",this);
    public String str()
    {return "shiva";
    public static void main(String a[])
    { ThisCheck the=new ThisCheck();
    the output I am expecting is Shiva.
    But the output Iam getting is the address of the object ThisCheck
    as ThisCheck@7d772e
    What's going wrong?
    I appreciate ur help.

A: using this as String reference

Try:public class ThisCheck {
    public ThisCheck() {
        System.out.printf("%s", this);
    public String toString() {
        return "shiva";
    public static void main(String a[]) {
        ThisCheck the = new ThisCheck();
}The %s flag in printf() looks for the toString() method of the corresponding
argument. (Details here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html)
When you post it's a good idea to use the Formatting tips
http://forum.java.sun.com/help.jspa?sec=formatting
Basically it means putting [code] at the start of your code and [/code] at
the end.

Try:public class ThisCheck {
    public ThisCheck() {
        System.out.printf("%s", this);
    public String toString() {
        return "shiva";
    public static void main(String a[]) {
        ThisCheck the = new ThisCheck();
}The %s flag in printf() looks for the toString() method of the corresponding
argument. (Details here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html)
When you post it's a good idea to use the Formatting tips
http://forum.java.sun.com/help.jspa?sec=formatting
Basically it means putting [code] at the start of your code and [/code] at
the end.

  • String reference not changing

    I have 2 files as follows. (Runnable code)
    1. StringTest.java
    package mytest;
    public class StringTest
         public static String s1 = "Hello";
         public static void main(String args[])
              String s2= "Hello";
              String s3= "Hello";
              System.out.println("1. s1 s2 : " + (s1 == s2));
              System.out.println("2. s2 s3 : " + (s2 == s3));
              System.out.println("3. s1 s3 : " + (s1 == s3));
              System.out.println("4. s1 s4 : " + (s1 == InnerMain.s4));
              System.out.println("5. s2 s4 : " + (s1 == InnerMain.s4));
              System.out.println("6. s1 s5 : " + (s1 == OuterClass.s5));
              System.out.println("7. s4 s5 : " + (InnerMain.s4 == OuterClass.s5));
              System.out.println("8. s2 s5 : " + (s2 == OuterClass.s5));
              System.out.println("9. s3 s5 : " + (s3 == OuterClass.s5));
              SameFile sf = new SameFile();
              System.out.println("10. s1 s5 : " + (s1 == sf.s6));
              System.out.println("11. s2 s5 : " + (s2 == sf.s6));
              System.out.println("12. s3 s5 : " + (s3 == sf.s6));
              System.out.println("13. s4 s5 : " + (InnerMain.s4  == sf.s6));
              System.out.println("14. s5 s5 : " + (OuterClass.s5  == sf.s6));
              sf.s6 = "Hello";
              System.out.println("10. s1 s5 : " + (s1 == sf.s6));
              System.out.println("11. s2 s5 : " + (s2 == sf.s6));
              System.out.println("12. s3 s5 : " + (s3 == sf.s6));
              System.out.println("13. s4 s5 : " + (InnerMain.s4  == sf.s6));
              System.out.println("14. s5 s5 : " + (OuterClass.s5  == sf.s6));
         public static class InnerMain
              public static String s4 = "Hello";
    class SameFile
         public String s6 = "Hello";
    }2.OuterClass.java
    package mytest;
    public class OuterClass
         public static String s5 = "Hello";
    }OUTPUT
    1. s1 s2 : true
    2. s2 s3 : true
    3. s1 s3 : true
    4. s1 s4 : true
    5. s2 s4 : true
    6. s1 s5 : true
    7. s4 s5 : true
    8. s2 s5 : true
    9. s3 s5 : true
    10. s1 s5 : true
    11. s2 s5 : true
    12. s3 s5 : true
    13. s4 s5 : true
    14. s5 s5 : true
    10. s1 s5 : true
    11. s2 s5 : true
    12. s3 s5 : true
    13. s4 s5 : true
    14. s5 s5 : true"Hello" : String references dont change in any case?
    String is immutable so no problem .. but whats going on ..

    When you create a new String variable having the contents already existing in the String Pool ,then the reference point to existing data.
    e.g.
    String a="Hello";//Create a block of memory in the Pool with Data "Hello"
    String b="Hello";// Search for any block having data "Hello", here existing then b will point to same block.....
    // when you check s1==s2 // It check for the reference
    // when you check s1.equals(s2) // It check for the contents(value) of variables

  • Find one character in a string and count the amount of times its found

    Hey SQL Forum experts.
    I have a string that is about 3000 characters long.
    I'm trying to search my string for the value '{'.
    Then I want to count how many times this value is found.
    How do I form the Oracle SQL to do this?

    Hi Colin,
    Try the following:
    SQL> var s varchar2(100)
    SQL>
    SQL> exec :s := 'Colin {{ Mackenzie {{{'
    PL/SQL procedure successfully completed.
    SQL> select length(:s) - length(replace(:s, '{')) Matches from dual;
       MATCHES
             5
    SQL> exec :s := 'I have a string that is about 3000 characters long'
    PL/SQL procedure successfully completed.
    SQL> select length(:s) - length(replace(:s, '{')) Matches from dual;
       MATCHES
             0Regards.

  • Two string references pointing to the same object

    Look at the following code snippet         String   s1 = "hello";
            String   s2 = "world";
            String   s3 = "welcome";
            String   s4 = "here";
            String   s5 = "hello";
      They say that references s1 and s5 point to the same object "hello".
    At run time after creating objects from s1 to s4 how the Java run time knows that there is already an object "hello" and assigns a differernt reference.

    When the jvm load the class file, all suchconstants
    found in the class are loaded in a global constant
    pool. when you assign the constant to a variable,it
    uses the same one from the constant pool.Suppose I intialize a string by getiing input
    nput from the console which already exists, in that
    case a new object will be created or the existing one
    be used.A new object will always be created in that case, but you can get a reference to the string in the pool by calling intern. See the javadoc for intern in the String class.
    Kaj

  • String Letter Count

    I must create a program that reads in a string from the user then prints out the occurance of each letter of the alphabet using the charAt() method. Can I get a little help starting?

    import java.util.Scanner;
    import static java.lang.System.out;
    public class Project5B_2{
    public static void main(String[] args){
         //Create Scanner
    Scanner scan = new Scanner(System.in);
    int another=1;
    while (another == 1) {
         //Set Variables
    String str;
    int countA=0;
    int countE=0;
    int countI=0;
    int countO=0;
    int countU=0;
    int countY=0;
    int countSpace=0;
    int countOther=0;
    out.print("Enter a string: ");
    str=scan.next();     
    str=str.toUpperCase();
    for(int i = 0; i < str.length(); i++) {
    if(str.charAt(i) == 'A') {
    countA++;
    else if(str.charAt(i) == 'E') {
    countE++;
    else if(str.charAt(i) == 'I') {
    countI++;
    else if(str.charAt(i) == 'O') {
    countO++;
    else if(str.charAt(i) == 'U') {
    countU++;
    else if(str.charAt(i) == 'Y') {
    countY++;
    else if(str.charAt(i) == ' ') {
    countSpace++;
    else {
    countOther++;
    out.println("No. of \'A\' or \'a\' = " + countA);
    out.println("No. of \'E\' or \'e\' = " + countE);
    out.println("No. of \'I\' or \'i\' = " + countI);
    out.println("No. of \'O\' or \'o\' = " + countO);
    out.println("No. of \'U\' or \'u\' = " + countU);
    out.println("No. of \'Y\' or \'y\' = " + countY);
    out.println("No. of spaces = " + countSpace);
    out.println("No. of other characters = " + countOther);
    out.println();
    out.print("Do you want to continue? (1=yes, 0=no) ");
    another = scan.nextInt();
    ----jGRASP exec: java Project5B_2
    Enter a string: Hello, this is a test!
    No. of 'A' or 'a' = 0
    No. of 'E' or 'e' = 1
    No. of 'I' or 'i' = 0
    No. of 'O' or 'o' = 1
    No. of 'U' or 'u' = 0
    No. of 'Y' or 'y' = 0
    No. of spaces = 0
    No. of other characters = 4
    Do you want to continue? (1=yes, 0=no) Exception in thread "main" java.util.InputMismatchException
         at java.util.Scanner.throwFor(Scanner.java:840)
         at java.util.Scanner.next(Scanner.java:1461)
         at java.util.Scanner.nextInt(Scanner.java:2091)
         at java.util.Scanner.nextInt(Scanner.java:2050)
         at Project5B_2.main(Project5B_2.java:66)
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  • String reference equality

    I don't understand why following code when executed returns false
    String s1= " string".trim();
      if(s1 == "string"){
      System.out.println("true");
      else{
      System.out.println("false");

    In this case the strings are located in the String pool and are the same reference.
    It stops working, when you use
    String s1 = new String("String");
    String s2 = new String("String");
    System.out.println(s1 ==s2);
    Just stop using == when comparing Strings, unless you wnat to compare the objects.

  • Maybe you are looking for

    • Adjust the width of a column in a report.

      Hi, How can we adjust the width of a column in a report. We have a report with around 10 columns. out of the 10 columns 1 column has a width of 500 char.. we want to display only the first 150 chars of that column with 50 char on first line,next 50 o

    • Error starting batch job through Data Services Web Service

      I have a customer running our web service interface to BODS. We have not been able to successfully start any jobs due to the following error: Services sent the request to start batch job 'Job_Test_Web_Services' on server 'null'. The Job Server refuse

    • How to connect several clients to a server?

      Hi, i'm currently developing a client server based application. The problem i'm facing now is that i can only add one client. But i want to add every client that connects to the server. Do i need to assign each input and output stream to each connect

    • Short URL in the browser address bar

      Hi we are using Robo Help product. we launched it on Apache Tomcat server. When we publish a project in an area and try to view that, the URL in the address bar is displayed very long with % characters and other junk. can anyone please let me know ho

    • Issue of Speakers and SD Card not supporting.

      Recently I have bought Lenovo Yoga 2 Tab  8inch. My first great disappointment is the volume of sound that cannot be enhanced in the device to a decent level. Much smaller Samsung S4 has 100 times more prominent sound which can be heard without strai