Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Norm
  • 2 Post By Tolls

Thread: Questions regarding how varaibles in java work (also in memory).

  1. #1
    NirelG is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Questions regarding how varaibles in java work (also in memory).

    Hi all,
    This is my first post here and any help would be highly appreciated.
    This is a bit long (because I have some examples, but my 3 questions are at the very end)

    I used to write a bit in python which is a language that variables are stored as pointers, meaning if I would give two different variables the same value they would point to the same place in memory.

    From researching online and on this forum I understood that java does not work like that at all, it stores each variable at different places, meaning if I was to write a code like:
    Java Code:
    int a = 5;
    int b = 5;
    The variables a and b would point to different places in memory. (unlike python for example)

    Now, my problem starts when it comes to arrays, although it would work the same way as the above example if I was to write a code like:

    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = {1,4,6,8};
    System.out.println(a);
    System.out.println(ab);
    The output was different:
    [I@15db9742
    [I@6d06d69c

    When I tried the next code I got the same output for both arrays:
    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = a;
    System.out.println(a);
    System.out.println(ab);
    The output was the same:
    [I@15db9742
    [I@15db9742

    As you can see, when I said an array was equal to another array it just gave the same address in memory of the first one to the second one.
    You can even manipulate it further by writing:
    Java Code:
    		int a[] = {1,4,6,8};
    		int ab[] = a;
    		ab[0] = 99;
    		System.out.println(a[0]);
    And the output is: 99,
    meaning they really do point to the same place in memory.




    So my questions are as follows,
    1) If i will write something like the code below will both of those variables have the same place in memory, or is it true only for arrays?
    Java Code:
    		int a = 2
    		int b = a
    2) Is there any reason the arrays work like this? if the language doesn't work with pointers as a all, then why it does on this case?
    3)In python there is a command called "id", which lets you see the address of objects in memory.
    I searched a lot online and did not find anything close to that in java,
    Therefore I had to print the arrays to see at least if they output the same thing, is there anything in java that can do it? (for any variable so I can even see what happens to the code on question 1)


    Thanks in advance,
    Nirel.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Questions regarding how varaibles in java work (also in memory).

    values such as int, float, double, long (note lower case first letter) are primitives. Thus a = 5 an b = 5 are two different variables with the same value. Arrays and objects use references (think of pointers) so they behave like you are used to.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    NirelG is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Questions regarding how varaibles in java work (also in memory).

    Quote Originally Posted by jim829 View Post
    values such as int, float, double, long (note lower case first letter) are primitives. Thus a = 5 an b = 5 are two different variables with the same value. Arrays and objects use references (think of pointers) so they behave like you are used to.

    Regards,
    Jim
    Firstly, thank you very much for your answer.
    Secondly, I can now get that a=5 and b=5 will be two different variables with the same value.

    Yet can you please elaborate a bit and explain to me why when i use this:
    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = {1,4,6,8};
    System.out.println(a);
    System.out.println(ab);
    It gives me two different outputs? if they work with references shouldn't they output the same answer?
    This is what I cant seem to understand.

    Thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Questions regarding how varaibles in java work (also in memory).

    It gives me two different outputs?
    Please post the outputs you are talking about when asking questions about an output.

    I suspect you are talking about the String that is returned by the toString() method for an array. Since the arrays are different, the String that is returned will be different. a is not the same array as ab. They do have the same contents.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    NirelG is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Questions regarding how varaibles in java work (also in memory).

    Quote Originally Posted by Norm View Post
    Please post the outputs you are talking about when asking questions about an output.

    I suspect you are talking about the String that is returned by the toString() method for an array. Since the arrays are different, the String that is returned will be different. a is not the same array as ab. They do have the same contents.
    Sorry about that, I meant the output from System.out.print(a) and System.out.print(ab) and they are different:
    [I@15db9742
    [I@6d06d69c

    From your answer I think I understand now.
    you mean that if I write:
    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = {1,4,6,8};
    System.out.println(a);
    System.out.println(ab);
    It will give me 2 arrays with different places in memory, because they are different, even if their content is the same

    when I use this code on the other hand:
    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = a;
    ab[0] = 5;
    Because it became the same array, and I only changed an index inside one of those arrays (in the same place of memory) this is why both of the arrays now have on their 0 place in the index "5".
    (I hope I am correct so far).

    If that is true so is there any reason behind why java gives arrays the same place in memory on this case:
    Java Code:
    int a[] = {1,4,6,8};
    int ab[] = a;
    Yet not give the same place in memory to variables like on this case?:

    Java Code:
    int a = 1;
    int b = a;

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Questions regarding how varaibles in java work (also in memory).

    why java gives arrays the same place in memory on this case:
    int ab[] = a;
    Because the two variables refer to the same place in memory. A new array is not created.

    Use the clone() method to create a new array and copy the contents of a to ab:
    int ab[] = a.clone(); // create a new array with the same content
    NirelG likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Questions regarding how varaibles in java work (also in memory).

    Java has two basic types of variable.
    Primitives, which are simply a value stored as bytes (int, long, float, double, char, boolean). The value of a primitive is its value...that's it. It's not a pointer to memory where the data resides.
    References, which point to an Object in memory. The value of a reference is its memory location.

    In both cases, when you assign a variable to another variable you are changing the value. In the case of the primitive this is the thing you are interested in directly (the number 5 for example). In the case of a reference it changes which object the reference is referring to.
    Norm and NirelG like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Couple Questions to have JFormDesigner Work
    By Feriscool in forum Advanced Java
    Replies: 2
    Last Post: 06-15-2011, 10:02 PM
  2. Memory Leak questions, code review
    By mensaFool in forum Advanced Java
    Replies: 9
    Last Post: 03-08-2010, 04:27 PM
  3. Out of memory work around for a java application (please help!)
    By javameanslife in forum Advanced Java
    Replies: 9
    Last Post: 02-02-2010, 01:52 PM
  4. topic is for errors and questions i get throughout my work
    By wildheart25c in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-14-2009, 02:05 AM
  5. mysql memory questions
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •