简体   繁体   English

如果使用整数/双精度/浮点数,java是否使用内存

[英]Does java uses memory in case of integer/Double/Float number usage

I am facing a confusion hope anyone might answer. 我面临混乱,希望任何人都能回答。 Please see the code below 请看下面的代码

for(int i = 0; i < 40; i++){
    if(i < 20){  //Does this 20 here is initialized again and again
       System.out.println("less than 20");
    }else{
       System.out.println("greater than 20");
    }
}

So my question is: Does the 20 in i < 20 gets initialized to an integer again and again causing allocation of memory again and again or does java uses similar kind of concept like a STRING POOL it uses for string. 所以我的问题是: i <20中的20是否一次又一次初始化为整数,一次又一次地导致内存分配,还是java使用类似的概念,例如它用于字符串的STRING POOL

According to knowledge it takes memory, but I still want to be sure. 根据知识,它需要记忆,但是我仍然想确定。 The reason for that is: 原因是:

I am working on a code which is too much performance intensive, hence I cannot add conditional code in it. 我正在处理过多的性能密集型代码,因此无法在其中添加条件代码。 Something like below 像下面这样

 for(int p = 0; p < 10; p++){
            if(p < 20){
                System.out.println("ABC");
            }
            if(p < 20 && "SomeValue".equals("SomeValue")){
                System.out.println("SomeValue");
            }
            if(p < 20 && "ABC".equals("ABC")){
                System.out.println("ABCDEF");
            }
        }

so if the answer is yes that 20 is taking the memory then I can make the code something like below 因此,如果答案是肯定的,那么20正在占用内存,那么我可以使代码如下所示

 int value = ("Coldrink".equals("coca cola"))?10:20;
        for(int p = 0; p < 10; p++){
            if(p < value){
                System.out.println("ABC");
            }
            if(p < value && "SomeValue".equals("SomeValue")){
                System.out.println("SomeValue");
            }
            if(p < value && "ABC".equals("ABC")){
                System.out.println("ABCDEF");
            }
        }

as you can see the variable value is initialized once and I have put my own condition which might have some performance issue, but then I have reduced the memory consumption, which could make things even. 如您所见,变量值被初始化一次,并且我将自己的条件设置为可能存在一些性能问题,但是后来我减少了内存消耗,这可以使事情变得均匀。

EDIT: Thanks @ TJ Crowder got my confusions cleared up. 编辑:谢谢@ TJ Crowder消除了我的困惑。 People who have the same issues please read the accepted answer as well as Click on this resource too 有相同问题的人,请阅读接受的答案以及也单击此资源

Does the 20 in i < 20 gets initialized to an integer again and again causing allocation of memory again and again or does java uses similar kind of concept like a STRING POOL it uses for string. i < 2020是否一次又一次初始化为整数,一次又一次地导致内存分配,还是java使用类似的概念,例如它用于字符串的STRING POOL。

Neither. 都不行 The 20 is a primitive int value — not an Integer instance — which is hardcoded in the bytecode and loaded into a register for the comparison operation. 20是原始int值(不是Integer实例),该值硬编码在字节码中,并加载到寄存器中以进行比较操作。 No need for anything like the string intern pool, and no new allocation for each iteration. 不需要像字符串实习生池之类的任何东西,也不需要为每次迭代分配新的资源。

We can see this by putting that loop in a main in an example class: 我们可以通过将循环放在示例类的main中来看到这一点:

public class Example {
    public static void main(String[] args) {
        for(int i = 0; i < 40; i++){
            if(i < 20){  //Does this 20 here is initialized again and again
               System.out.println("less than 20");
            }else{
               System.out.println("greater than 20");
            }
        }
    }
}

...then compiling it, and looking at the bytecode via javap -c Example : ...然后进行编译,然后通过javap -c Example查看字节码:

Compiled from "Example.java"
public class Example {
  public Example();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_1
       3: bipush        40
       5: if_icmpge     39
       8: iload_1
       9: bipush        20
      11: if_icmpge     25
      14: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      17: ldc           #3                  // String less than 20
      19: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      22: goto          33
      25: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      28: ldc           #5                  // String greater than 20
      30: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      33: iinc          1, 1
      36: goto          2
      39: return
}

Note the boldfaced operations at offsets 9 and 11. 注意偏移量9和11的粗体操作。

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

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