#Address assignment problem

1 messages · Page 1 of 1 (latest)

merry heart
#

i dont get why p1 isnt assigned with p2 address ? the opposite happens

hard larkBOT
#

This post has been reserved for your question.

Hey @merry heart! Please use /close or the Close Post button 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.

ruby folio
#

Can you show how P1 is defined and print out P1's address before the reassignment? Addresses will change every runtime

merry heart
#

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

pastel lake
#

They seem to be two different objects, so they have different adresses.

merry heart
#

but i assign P1=P2;

#

P1 must be P2 ?

#

pointing to same address ?

pastel lake
#

Yes, but you don't do that in the first screenshot.

merry heart
#

all are active

pastel lake
#

I'm not sure what do you mean. Everything works as expected.

merry heart
#

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

pastel lake
# merry heart +

You made an assignment here and they point to the same address.

merry heart
#

yes but to P1 address

pastel lake
#

Oh, I get now what do you mean.

ruby folio
#
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

pastel lake
#

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.

merry heart
#

🤯

ruby folio
#

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

merry heart
#

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 ?

pastel lake
#

It depends on how the JVM allocates memory. It's semi-random. It just happened that 48cf768c was used in both executions.

merry heart
#

😕

#

confuseeeed

#

im still in the illusion

#

twala helppppp

ruby folio
#

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

merry heart
# ruby folio There are two locations for memory, on the stack and heap. Essentially the stack...

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...

▶ Play video
ruby folio
merry heart
#

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 ?

pastel lake
# merry heart lol, why is P2 now the @48 address

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.

merry heart
#

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

pastel lake
#

P1 is 48... at the first print, and then you assign P2 to that variable, so the second print displays the address of P2.

merry heart
#

oh wow wait

hard larkBOT
#

💤 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.

merry heart
#

does someone know what this is

pastel lake
#

Well, 22 is not supported. Maybe downgrade it to 21.

hard larkBOT
#

💤 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.

merry heart
#

now 21

#

now the message doesnt appear

#

but when i click debug mode it just runs normal

pastel lake
#

Are you sure?

merry heart
#

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

pastel lake
#

52 is... Java 8

merry heart
#

-.-

#

and how do i change it..

pastel lake
#

No idea how you configured that project. And I haven't used Eclipse for years.

merry heart
#

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

pastel lake
#

Because the price is also static.

merry heart
#

ye and why does it change to 120 ?

pastel lake
#

I mean, it's static on the screenshot.

#

Oh, there's another one.

merry heart
#

2 screenshots

pastel lake
#

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.

merry heart
#

omg

#

ahh

#

that means when its static, the creating of the second object set it globally to 60 ?

pastel lake
#

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.

merry heart
#

static variables get their own place in the heap right ?

pastel lake
#

At least static fields. Static methods are a bit more useful.

#

Yeah, I think so.

little mason
#

You can also copy from p2 to p1,

#

Note we use == for equality