我在Java中有一个Integers数组,我只想使用它的一部分.我知道在Python中你可以做类似这个数组[index:]的东西,它从索引中返回数组.这样的事情在Java中是可能的.
我知道垃圾收集在Java中是自动化的.但我明白,如果你System.gc()在代码中编写,Java VM可能会或可能不会在运行时决定在那时进行垃圾收集.这是如何工作的?基于什么基础/参数,VM在看到GC时决定做(或不做)GC System.gc()?是否有可能的例子在这种情况下将它放在代码中是个好主意?
如果不可变类对象副本将等于原始副本那么为什么StringJava 中的类具有复制构造函数?这是一个错误还是这个实施背后的原因?在Java文档中,它被指定为:
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
....
....}
Run Code Online (Sandbox Code Playgroud) 据说substring String类中的方法会导致内存泄漏.这是真的吗?怎么样?有什么替代方案吗?
特别是在寻找答案,
在java中可能导致内存泄漏的所有其他事情是什么?这将有助于我在编码时保持谨慎.
我<<在循环中使用运算符准备了一个巨大的字符串.最后我想删除最后2个字符.
some_loop
str << something
end
str = str[0..-3]
Run Code Online (Sandbox Code Playgroud)
我认为上面的最后一个操作也会消耗内存和时间,但我不确定.我只是想看看是否有一个具有相反效果的操作, <<所以我可以从同一个字符串中删除那两个最后一个字符.