Anant666
Anant666

Reputation: 416

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.

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

 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 @ T.J Crowder got my confusions cleared up. People who have the same issues please read the accepted answer as well as Click on this resource too

Upvotes: 1

Views: 815

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074178

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.

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. 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:

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:

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.

Upvotes: 8

Related Questions