How memory works?

This doesn’t really have that much to do with Java or proramming in general… It is just a question that has been on my mind lately and this is probably the best place to ask… I already know how memory works in binary and how there is read and write commands and busses… But I want to know, when the program makes a new value to hold in memory, the memory assigns a location to put that value. But how does the program know where that location is? And how does the program remember that? Another memory point? How does this work?