#Address assignment problem
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @merry heart! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Can you show how P1 is defined and print out P1's address before the reassignment? Addresses will change every runtime
my mind expects both to be
Punkt2D@6a6824be
its nothgin complicated y can create it yourself
in 1min
assigments work rigt to left associativity ?
seems like in this one one its left to right lol
They seem to be two different objects, so they have different adresses.
Yes, but you don't do that in the first screenshot.
I'm not sure what do you mean. Everything works as expected.
P1 = Punkt2D@48cf768c
P2 = Punkt2D@6a6824be
Punkt2D@48cf768c
Punkt2D@6a6824be
Punkt2D@48cf768c
Punkt2D@48cf768c
??????
I expect P1 to assign to P2 address
the opposite is happening
You made an assignment here and they point to the same address.
yes but to P1 address
Oh, I get now what do you mean.
Punkt2D P1 = new Punkt2D();
Punkt2D P2 = new Punkt2D();
System.out.println("P1:" + P1);
System.out.println("P2:" + P2);
P1 = P2;
System.out.println("AP1:" + P1);
System.out.println("AP2:" + P2);```
What is the output of this code
P1 doesn't have its own firm address. It's a coincidence that in your second execution P1 points to the address that was assigned to P2 in your first execution.
Looks like it works as intended, as P1 now points to the same memory address as P2.
When you were running it independently, only printing before or after, the memory addresses were shifting after each run giving the illusion that P2 was being set to P1
hmm i still dont get it -.-
what influence does println have ?
it shouldnt have any ?
is the order of evaluation not from top to bottom ?
It depends on how the JVM allocates memory. It's semi-random. It just happened that 48cf768c was used in both executions.
There are two locations for memory, on the stack and heap. Essentially the stack is what stores all of the pointers to the heap as the heap is random access memory (RAM). What is going on here is when you call the Punkt2D obj = new Punkt2D(), it creates 2 things. The first is an object in memory on the heap (stored at the memory address you see), and a pointer to it on the stack. When you create two of these objects, you now have 2 objects in the heap and 2 on the stack. By saying P1 = P2, you are essentially telling P1 to point to the reference on the heap in which P2 points to.
Hopefully that helps
If you search "stack heap references with pointers" on google it should give you a better understanding
yes that is what i also know but i dont get the illusion of 2 times p1 being printed.
i watched this:
https://www.youtube.com/watch?v=450maTzSIvA&ab_channel=Guru99
https://www.guru99.com/java-stack-heap.html the following video demonstrates how memory is allocated in stack java & heap java.
This is complete guide to Java memory management
The JVM divided the memory into following sections.
Heap,
Stack,
Code,
Static.
What is Stack Memory?
Stack in java is a section of memory which contains methods, instance...
The reference at P2 is printed twice, not P1
why do i get 3 times the same address now ?
i commented out println P1
lol, why is P2 now the @48 address
P1 or P2 take @48 if i print it before ?
Like I said it only depends on how JVM allocates memory. It's only consistent within a single execution, it doesn't have to be among multiple different executions. You can run it on a different JVM and have different results. For all you care it could be allocated completely randomly.
it allocates memory on runtime and that randomly
ok
but why do i get different results when i print the same objects before and after an assignment ?
ohhh
i think i slowly understand
P1 is 48... at the first print, and then you assign P2 to that variable, so the second print displays the address of P2.
oh wow wait
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
does someone know what this is
Well, 22 is not supported. Maybe downgrade it to 21.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
i did
now 21
now the message doesnt appear
but when i click debug mode it just runs normal
Are you sure?
i changed the library
to the java21 folder
lol
debug "already running"...
i just need to switch the view !?!?!?!
normally when i enter the bug
it switches it automaticly
hm
not there is still something fishy
52 is... Java 8
No idea how you configured that project. And I haven't used Eclipse for years.
hmm okay
i can somehow for now can work with it
but there is another mysterious phenomenon
that i dont get
when i change the price from static to not-static
the umsatz (revenue) changes by 30
Because the price is also static.
ye and why does it change to 120 ?
2 screenshots
On the first screenshot it's static, so the both objects share the same price (60).
And I'm pretty sure you don't want that.
omg
ahh
that means when its static, the creating of the second object set it globally to 60 ?
Yeah.
In general you'd be better off avoiding static. Unless you really really want that behavior, but most of the time you don't and you shouldn't.
static variables get their own place in the heap right ?