Passing a String by Reference in Java?

I am used to doing the following in C : void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } And the output is: foo However, in Java, this does not seem to work. I assume because the String object is copied instead of passed by referenced. I thought Strings were objects, which are always passed by reference.

在Java中通过引用传递字符串?

我习惯在C执行以下操作: void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } 输出是: foo 但是,在Java中,这似乎不起作用。 我假设因为String对象被复制而不是被引用传递。 我认为字符串是对象,它总是通过引用传递。 这里发生了什么? 你有三个选择: 使用StringBuilder: StringBuilder zText = new StringBuilder ()

Return boolean from method that calls an Int

As per instructions: Write a static method that takes one integer as a formal parameter and returns a Boolean value of True if the parameter value is even and False if the it odd. It would seem my method must call an int instead of a boolean. With that being said I don't know how to return a boolean from a method that calls an int. I've tried this but it doesn't work. EDIT - Lang

从调用Int的方法返回布尔值

根据说明:编写一个静态方法,它将一个整数作为形式参数,如果参数值为偶数,则返回布尔值True;如果参数值为奇数,则返回False。 看来我的方法必须调用一个int而不是布尔值。 这样说,我不知道如何从调用int的方法返回一个布尔值。 我试过这个,但它不起作用。 编辑 - 语言是JAVA。 nEDIT 2 - 对于将来看到这个的任何人,我原本打算输入private static int result。 不是私有的静态布尔结果。 那个错误最终导致我的代

i don know how to calling the method

1.Write a static boolean method – a method that returns a boolean – that takes an int parameter and converts the integers 0 and 1 into false and true respectively. Include a main() method to test the boolean method. my code: public class Assignment2task1 { public static boolean isPrime (int N) { if (N <= 0)return false; System.out.println ("you entered 0 which evaluates

我不知道如何调用该方法

1.写一个静态布尔方法 - 一个返回布尔值的方法 - 接受一个int参数并将整数0和1分别转换为false和true。 包含一个main()方法来测试布尔方法。 我的代码: public class Assignment2task1 { public static boolean isPrime (int N) { if (N <= 0)return false; System.out.println ("you entered 0 which evaluates to false"); for (int i = 1; i<=N/i; i++) if (N>=1)return true

(int) Math.sqrt(n) much slower than (int) Math.floor(Math.sqrt(n))

I was looking at my code, hoping to improve its performance and then i saw this: int sqrt = (int) Math.floor(Math.sqrt(n)); Oh, ok, i don't really need the call to Math.floor, as casting the double returned from Math.sqrt(n) will be effectively flooring the number too (as sqrt will never return a negative number). So i went and dropped the call to Math.floor: int sqrt = (int) Math.sqrt(n)

(int)Math.sqrt(n)比(int)Math.floor慢得多(Math.sqrt(n))

我正在查看我的代码,希望改善其性能,然后我看到了这一点: int sqrt = (int) Math.floor(Math.sqrt(n)); 哦,好吧,我并不需要对Math.floor的调用,因为从Math.sqrt(n)返回的double将会有效地铺平数字(因为sqrt永远不会返回负数)。 所以我去了Math.floor的电话: int sqrt = (int) Math.sqrt(n) 坐下来,自满地看着代码运行并执行大约10%! 比之前的版本差。 这让我感到震惊。 任何想法的人? Math.floor javad

Effect of a Bitwise Operator on a Boolean in Java

The bitwise operators are supposed to travel variables and operate on them bit by bit. In the case of integers, longs, chars this makes sense. These variables can contain the full range of values enforced by their size. In the case of booleans, however, a boolean can contain only two values. 1 = true or 0 = false. But the size of the boolean isn't defined. It can be as big as a byte or

位运算符对Java中布尔值的影响

按位运算符应该传送变量并对其进行一点一点的操作。 在整数,长整数的情况下,这是有道理的。 这些变量可以包含由其大小强制执行的全部范围的值。 然而,在布尔值的情况下,布尔值只能包含两个值。 1 =真或0 =假。 但是布尔的大小没有定义。 它可以像字节一样大或稍小一点。 那么在布尔值上使用按位运算符有什么作用? JVM是否将其转化为正常的逻辑运算符并继续前进? 为了操作的目的,它是否将布尔值视为单个位实体

In Java, what is the best way to determine the size of an object?

For example, let's say I have an application that can read in a CSV file with piles of data rows. I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause OutOfMemoryError s. Each row translates into an object. Is there an easy way to find out the size of that object programmatically? Is there

在Java中,确定对象大小的最佳方法是什么?

例如,假设我有一个应用程序,它可以读取包含一堆数据行的CSV文件。 我根据数据类型向用户提供行数摘要,但我想确保不读取太多数据行并导致OutOfMemoryError 。 每一行都会翻译成一个对象。 有没有一种简单的方法来以编程方式找出该对象的大小? 是否有一个引用定义了VM基本类型和对象引用的大小? 现在,我的代码中读取了32,000行 ,但我还想要代码尽可能多地读取行,直到我使用了32MB的内存。 也许这是一个不同的问题,

Why is this code with several "or" statements slightly faster than using a lookup table in Java?

While looking at a micro-optimization question that I asked yesterday (here), I found something strange: an or statement in Java is running slightly faster than looking up a boolean value in an array of booleans. In my tests, running the below algorithms on long values from 0 to 1 billion, alg1 is about 2% faster. (I have altered the order in which the algorithms are tested, and I get the same

为什么使用几个“或”语句的代码比在Java中使用查找表稍快?

在我看到昨天(这里)提出的一个微观优化问题时,我发现了一些奇怪的东西:Java中的一个语句or一个语句的运行速度比在一个布尔数组中查找布尔值要快一些。 在我的测试中,对于从0到10亿的long值运行以下算法,alg1快2%左右。 (我改变了算法的测试顺序,并且得到了相同的结果)。 我的问题是: 为什么alg1更快? 我希望alg2稍快一些,因为它使用查找表,而alg1必须对75%的输入执行4次比较和3次操作。 private final stati

Does Java JIT cheat when running JDK code?

I was benchmarking some code, and I could not get it to run as fast as with java.math.BigInteger , even when using the exact same algorithm. So I copied java.math.BigInteger source into my own package and tried this: //import java.math.BigInteger; public class MultiplyTest { public static void main(String[] args) { Random r = new Random(1); long tm = 0, count = 0,result=0;

Java JIT在运行JDK代码时是否会作弊?

我对一些代码进行了基准测试,即使使用完全相同的算法,我也无法使它像java.math.BigInteger一样快速运行。 所以我将java.math.BigInteger源文件复制到我自己的包中,并尝试这样做: //import java.math.BigInteger; public class MultiplyTest { public static void main(String[] args) { Random r = new Random(1); long tm = 0, count = 0,result=0; for (int i = 0; i < 400000; i++) {

How do I "decompile" Java class files?

What program can I use to decompile a class file? Will I actually get Java code, or is it just JVM assembly code? On Java performance questions on this site I often see responses from people who have "decompiled" the Java class file to see how the compiler optimizes certain things. Update February 2016: www.javadecompilers.com lists JAD as being: the most popular Java decompile

如何“反编译”Java类文件?

我可以使用什么程序来反编译类文件? 我真的会得到Java代码,还是只是JVM汇编代码? 在这个网站上的Java性能问题上,我经常看到有人反编译Java类文件,以了解编译器如何优化特定的东西。 2016年2月更新: www.javadecompilers.com将JAD列为: 最流行的Java反编译器,但主要是在这个年龄段。 用C ++编写,非常快速。 过时,不受支持,不能正确反编译Java 5及更高版本 所以你的里程可能会随着最近的jdk(7,8)而变化

Create instance of generic type in Java?

Is it possible to create an instance of a generic type in Java? I'm thinking based on what I've seen that the answer is no (due to type erasure), but I'd be interested if anyone can see something I'm missing: class SomeContainer<E> { E createContents() { return what??? } } EDIT: It turns out that Super Type Tokens could be used to resolve my issue, but

在Java中创建泛型类型的实例?

是否有可能在Java中创建一个泛型类型的实例? 我正在考虑基于我所看到的答案是no (由于类型擦除),但是如果有人能看到我失踪的东西,我会感兴趣: class SomeContainer<E> { E createContents() { return what??? } } 编辑:事实证明,超类型令牌可以用来解决我的问题,但它需要很多基于反射的代码,因为下面的答案已经指出。 我将暂时搁置一会儿,看看是否有人提出与伊恩罗伯逊的Artima Artic