简体   繁体   English

如何更改存储在java中的内存位置的值

[英]How to change a value stored at a memory location in java

String val="Hello";
//blocks of code

int c=val.hashCode();  //say i get 101 as memory location.

Say i don't know what is the "val" at mem location 101 .And i want to store val="abc" at this location. 说我不知道​​mem位置101的“val”是什么。我想在这个位置存储val =“abc”。 How can i use the memory location to append the val ? 如何使用内存位置附加val? Is this possible in Java. 这在Java中是否可行。

Java doesn't allow direct manipulation of memory locations. Java不允许直接操作内存位置。 It is built as a layer between the user and the actual memory. 它构建为用户和实际内存之间的层。

Not to mention that hashcode has nothing to do with the memory address. 更不用说哈希码与内存地址无关。

Per @ValekHalfHeart's comment I did a quick search and appearantly there is an API available for unsafe operations (including changes to the the memory location). Per @ ValekHalfHeart的评论我做了一个快速搜索,看起来有一个API可用于不安全的操作(包括更改内存位置)。 I have no experience with this, but it might look like a backdoor to do what you want. 我没有这方面的经验,但它看起来像是一个后门去做你想要的。

Do note that Java is explicitly built to abstract all these things away from you. 请注意,Java是明确构建的,以便将所有这些内容从您身上抽象出来。 Trying to use them after all is abusing your tools. 毕竟试图使用它们是滥用你的工具。

The short answer to your question is "no". 对你的问题的简短回答是“不”。 Java "protects" you from knowing (or caring) about the memory locations of objects, allocation, destruction, etc. Java“保护”您不知道(或关心)对象的内存位置,分配,破坏等。

If you like that kind of thing, might I recommend you use C? 如果您喜欢这种东西,我可以建议您使用C吗? It's a much maligned language - but it's great for that kind of "close to the silicon" programming. 这是一种备受诽谤的语言 - 但它对于那种“接近硅片”的编程非常有用。 With all the perils that entails... 承担所有危险......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM