How does Java keep the value from temporary variable?

Hello all:
I have a Question for Java Assignment operator.
Object v1;
Object v2;
v1 = v2; // Java assignment by reference if I understand correctly
///// for example:
import java.io.*;
import java.util.*;
public class TestOne {
Vector v1;
public void changeV() {
Vector v2 = new Vector();
v2.addElement(new Integer(10));
v2.addElement(new Integer(11));
v2.addElement(new Integer(12));
v1 = v2;
public TestOne() {
changeV();
System.out.println(v1.size());
System.out.println("0: " + v1.elementAt(0));
System.out.println("1: " + v1.elementAt(1));
System.out.println("2: " + v1.elementAt(2));
public static void main(String[] args) {
new TestOne();
// Output from Screen
bash-3.00$ java TestOne
3
0: 10
1: 11
2: 12
If I understand correctly, " v1 = v2; " is assigned by reference.
So the question is when function 'changeV' return, why v1 still holds the
values from v2 which has been destroyed?
Thank you
-daniel

firstly, the assignment is by-value, not by-reference.
the value the variables hold is a thingy called "reference." (In other languages such as C or C++ it might be called "pointer.") So "v1 = v2" always copies the value of v2 to v1.
secondly, the vector object that is created in the changeV method, is created on the heap, that is, in dynamic memory. The variable v2 holds only a reference to it. So when the method returns, only the reference is destroyed, not the actual object.
The object is automatically destroyed when there are no more reachable references to it (it is "garbage collected").

Similar Messages

  • Hi, I have quick question about use of USEBEAN tag in SP2. When I specify a scope of SESSION for the java bean, it does not keep the values that I set for variable in the bean persistent.Thanks,Sonny

     

    Make sure that your bean is implementing the serializable interface and that
    you are accessing the bean from the session with the same name.
    Bryan
    "Sandeep Suri" <[email protected]> wrote in message
    news:[email protected]..
    Hi, I have quick question about use of USEBEAN tag in SP2. When I
    specify a scope of SESSION for the java bean, it does not keep the
    values that I set for variable in the bean persistent.Thanks,Sonny
    Try our New Web Based Forum at http://softwareforum.sun.com
    Includes Access to our Product Knowledge Base!

  • How does one keep the Creative Cloud purchase/signup popup from popping up everytime I flash up Dream Weaver CS6?

    How does one keep the Creative Cloud purchase/signup popup from popping up every time I flash up Dream Weaver CS6?

    Johnnyontherocks it is likely you installed an updated version of the Adobe Application Manager.  Remove all copies of Adobe Creative software from the computer and reinstall to downgrade the installation.

  • How does java support the concept of destructor?

    How does java support the concept of destructor?

    @KunalSurana: before you flood the forum with basic questions, do us a favour and read.
    [Sun's basic Java tutorial|http://java.sun.com/docs/books/tutorial/]
    [Sun's New To Java Center|http://java.sun.com/learning/new2java/index.html]
    Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    jGuru
    A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch
    To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    [Yawmarks List|http://forums.devshed.com/java-help-9/resources-for-learning-java-249225.html]
    [The Java Developers Almanac|http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]
    [http://javaalmanac.com|http://javaalmanac.com]
    Bruce Eckel's [Thinking in Java(Available online.)|http://mindview.net/Books/DownloadSites]
    Joshua Bloch's [Effective Java|http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]
    Bert Bates and Kathy Sierra's [Head First Java|http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance ]
    James Gosling's [The Java Programming Language|http://www.bookpool.com/sm/0321349806]
    Gosling is the creator of Java. It doesn't get much more authoritative than this.
    Joshua Bloch and Neal Gafter [Java Puzzlers.|http://www.javapuzzlers.com/]

  • From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    Hi,
    Use NVL or COALESCE:
    NVL (col_a, col_b)
    Returns col_a if col_a is not NULL; otherwise, it returns col_b.
    Col_a and col_b must have similar (if not identical) datatypes; for example, if col_a is a DATE, then col_b can be another DATE or it can be a TIMESTAMP, but it can't be a VARCHAR2.
    For more about NVL and COALESCE, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm#sthref1310
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • How to get all the values from the dropdown menu

    How to get all the values from the dropdown menu
    I need to be able to extract all values from the dropdown menu; I know how to get all those values as a string, but I need to be able to access each item; (the value in a dropdown menu will change dynamically)
    How do I get number of item is selection dropdown?
    How do I extract a ?name? for each value, one by one?
    How do I change a selection by referring to particular index of the item in a dropdown menu?
    Here is the Path to dropdown menu that I'm trying to access (form contains number of similar dropdowns)
    RSWApp.om.GetElementByPath "window(index=0).form(id=""aspnetForm"" | action=""advancedsearch.aspx"" | index=0).formelement[SELECT](name=""ctl00$MainContent$hardwareBrand"" | id=""ctl00_MainContent_hardwareBrand"" | index=16)", element
    Message was edited by: testtest

    The findElement method allows various attributes to be used to search. Take the following two examples for the element below:
    <Select Name=ProdType ID=testProd>
    </Select>
    I can find the element based on its name or any other attribute, I just need to specify what I am looking for. To find it by name I would do the following:
    Set x = RSWApp.om.FindElement("ProdType","SELECT","Name")
    If I want to search by id I could do the following:
    Set x = RSWApp.om.FindElement("testProd","SELECT","ID")
    Usually you will use whatever is available. Since the select element has no name or ID on the Empirix home page, I used the onChange attribute. You can use any attribute as long as you specify which one you are using (last argument in these examples)
    You can use the FindElement to grab links, text boxes, etc.
    The next example grabs from a link on a page
    Home
    Set x = RSWApp.om.FindElement("Home","A","innerText")
    I hope this helps clear it up.

  • How we can get the values  from one screen to another screen?

    hi guru's.
         how we can get the values  from one screen to another screen?
              we get values where cusor is placed but in my requirement i want to get to field values from one screen to another screen.
    regards.
      satheesh.

    Just think of dynpros as windows into the global memory of your program... so if you want the value of a field on dynpro 1234 to appear on dynpro 2345, then just pop the value into a global variable (i.e. one defined in your top include), and you will be able to see it in your second dynpro (assuming you make the field formats etc the same on both screens!).

  • How do I get the values from a form?

    How do I get the values from a form?

    You can try using request method..
    request.getParameter("yourFormInputName");
    Try this.

  • I created an album from photos taken off three different cameras. I am sorting the photos manually and when I close iPhoto, the photos move back either by date or by camera import time. How can I keep the photos from moving around?

    I created an album from photos taken off three different cameras. I am sorting the photos manually and when I close iPhoto, the photos move back either by date or by camera import time. How can I keep the photos from moving around?

    Don't tell us, tell them
    http://www.apple.com/feedback/macosx.html is the place for feature requests and feedback

  • How do i keep the aps from running in the background on my iphone 4s?  Just downloaded the new version

    How do i keep the aps from running in the background on my iphone 4s?  Just downloaded the new version

    If you're talking about closing running apps, they changed it up a bit from iOS6. Now you just double click the home button to bring up the running apps, side scroll to the app you want to close, then just swipe up.
    = L.I.

  • When I import some of my cd's they have multiple album images but have just one song from the cd because of a guest performer. How do I keep the cd from "blowing up" like this? eergh

    When I import some of my cd's they have multiple album images but have just one song from the cd because of a guest performer. How do I keep the cd from "blowing up" like this? eergh

    for purchases from the iTunes store do this:
    connect your device and right-click or control-click it in the iTunes Source list, then choose transfer purchases from the shortcut menu that appears.
    for everything else check out this post by Zevoneer.

  • How does one keep the photo description attached to a photo in iphoto when that photo copies to a disc or flash drive

    how does one keep the photo description attached to a photo in iphoto when that photo copies to a disc or flash drive?
    the metadata transfers ok, it is the description of that photo that does not transfer.

    Use the File -> Export command and note the settings to write the metadata to the file on export.
    This User Tip
    https://discussions.apple.com/docs/DOC-4921
    has details of the options in the Export dialogue.

  • How I can save the value of a variable in a database?

    hello,
    how I can save the value of a variable in a database? I need to keep the # of times you press click animation. to know how many times it has been seen.
    I connect to msql database, using php, javascript, ajax and jquery, but not how to save the variable counter clicks from adobe edge Animate
    appreciate your help so I can connect to the database from adobe edge animate.
    thank you very much
    Luis Felipe Garcia
    [email protected]

    Hi, all-
    This isn't an Animate-specific issue, but I thought I'd take a stab at it anyway.  As with anything else on the web, you need to have a server script to handle incoming data, so it's all back end work.  Your page will need to call that script and provide parameters to that script that it can read.
    The easiest way I can think of to do this (and this is all highly theoretical, so please make sure it's secure before you implement it) is to create a 1px div that has visibility off.  Whenever you click, you can load your URL in that div, which then increments your counter on your db.
    Hope that helps inspire some interesting solutions!
    -Elaine

  • [MDX]can't get the value from two variables (@FromDimTimeFiscalYearMonthWeekLastDateDay &(@ToDimTimeFiscalYearMonthWeekLastDateDay

    can't get the value from two variables when execute MDX : Anyone who can help ?
    ================================
    with member [Measures].[APGC Replied Volume] as
     ([Measures].[Question],[Dim Replied By].[Replied By].&[APGC])
    member [Measures].[APGC Moderated Volume] as
     ([Measures].[DashBoard Thread Number],[Dim Moderated By].[Moderated By].&[APGC])
    member [Measures].[APGC Answered Volume] as
     ([Measures].[Question],[Dim Answered By].[Answered By].&[APGC])
     SELECT
     NON EMPTY
     [Measures].[Forum Thread Number],
     [Measures].[Reply In24 Hour],
     [Measures].[Question],
     [Measures].[Deliverale Minute],
     [Measures].[Working minutes],
     [Measures].[Answered],
     [Measures].[1D Answer],
     [Measures].[2D Answer],
     [Measures].[7D Answer],
     [Measures].[APGC Replied Volume],
     [Measures].[APGC Moderated Volume],
     [Measures].[APGC Answered Volume],
    [Measures].[Avg HTR],
    [Measures].[Avg HTA],
     [Measures].[Total Labor]
     } ON COLUMNS, 
     NON EMPTY { ([Dim Engineer].[SubGroup-Alias].[Alias].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
    FROM (
     SELECT ( STRTOMEMBER(@FromDimTimeFiscalYearMonthWeekLastDateDay, CONSTRAINED) : STRTOMEMBER(@ToDimTimeFiscalYearMonthWeekLastDateDay,
    CONSTRAINED) ) ON COLUMNS
    FROM [O365]) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

    Hi Ada,
    According to your description, you can't get the member when using the strtomember() function in your MDX. Right?
    In Analysis Services, while the member name string provided contains a valid MDX member expression that resolves to a qualified member name, the CONSTRAINED flag requires qualified or unqualified member names in the member name string. Please check if the
    variable is a Multidimensional Expressions (MDX)–formatted string.
    Reference:
    StrToMember (MDX)
    If you have any question, please feel free to ask.
    Simon Hou
    TechNet Community Support

  • I have the iCloud Photo Library (Beta) turned on. The photos from my iPhone do get uploaded to the iCloud. The photos are then downloaded to my wife's iPhone (we share the iCloud account). How do I keep the photos from downloading to my wife's iPhone

    I do not want all the photos from my iPhone downloading to my wife's iPhone from iCloud? We share an iCloud account. How can I prevent the download to my wife's iPhone from iCloud. .The "Upload to My Photo Stream" is turned off on my iPhone. . I am running 8.1.3. 
    I have a related question. In order to same space on my iPhone is there a way to upload the photos to the iCloud and then delete them from my iPhone.

    The way to keep the photos from downloading to your wife's phone would be for her to go to Settings > iCloud > Photos > and turn off iCloud Photo Library. If that isn't an option, the other option would be for her to have her own Apple ID ( and possibly explore the new family sharing options https://www.apple.com/ios/whats-new/family-sharing/ )
    Regarding the "related question" see the quotes below from iCloud Photo Library beta FAQ - Apple Support
    Deleting a photo or video from your Photos app will also remove the item from iCloud Photo Library and from your iCloud storage allocation. Photos and videos you remove can be recovered from the Recently Deleted album for 30 days. After 30 days, your content will be removed from the Recently Deleted album.
    If you want to reduce the space used by photos on your phone, rather than deleting them see the following:
    How does iCloud Photo Library save space on my device?
    If you turn on Optimize [device] Storage, iCloud Photo Library will automatically manage the size of your library on your iOS device, so you can make the most of your device's storage and access more photos than ever. iCloud Photo Library stores the original, high-resolution photos and videos in iCloud and can keep lightweight, device-optimized versions on each of your devices. As long as you have enough storage, recent photos and videos that you access the most will stay on your device at full resolution.
    You can turn on Optimize [device] Storage from Settings > iCloud > Photos or Settings > Photos & Camera > iCloud Photo Library on your iPhone, iPad, or iPod touch. You need an Internet connection to access an original photo or video that’s stored only in iCloud.

Maybe you are looking for

  • Questions on MCX Policies for Laptop/Mobile Users:

    Questions on MCX Policies for Laptop/Mobile Users: I have several managed Macs running Leopard. Many of them are MacBook laptops. My main MCX policies involve mapping printers and mounting network volumes at login. These are simple computer and group

  • In MRP - Alternate Item

    Hi , As one of our client need to know whether the how we can include the  Alternate items also in MRP wizard. As we are currently in 2007b. Thanks in advanace Anand

  • 1.3.5 backup??

    I have to agree that apps seem a little "snappier" with 1.3.5.  Has anyone been running it long enough to know if automatic backup is working again??  It wasn't working in 1.3.1.  Happy New Year!! Post relates to: Pre p100eww (Sprint)

  • How do I calculate strain from a volt amplitude vs time plot that labview generates

    I have set up four strain gages, a DAQ system and a coded output in labview. After running my program labview gives me a voltage vs time plot. How do I convert the voltage measurements to strain? My strain gages are configured in a Wheastone quarter

  • Windows 8 and photoshop 10

    Is there a compatability issue as my os is not recognising my photoshop 10 disk.....worked great with windows 7 is there a way around this?