1

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

Community
  • 1
  • 1
Anant666
  • 416
  • 9
  • 26
  • You're overthinking this. 20 is just a constant number, just because you use that number it doesn't mean that a location in memory needs to be allocated, initialized etc. No need to worry about memory usage for this case. Don't put the value in an `int` beforehand - that is actually worse than your first solution. – Jesper Apr 04 '17 at 13:18
  • 5
    When you're writing real performance intensive code, you use a profiler. You don't start blindly guessing whether some irrelevant `20` is slowing your program down. – Kayaman Apr 04 '17 at 13:18
  • As i wrote that I had a confusion. And @Jesper could you explain why the second solution is not good. If that's the case then I might have to think something else. – Anant666 Apr 04 '17 at 13:25
  • Because it's more code and doesn't change anything, specifically it does not make the program more efficient or cause less memory use. – Jesper Apr 04 '17 at 13:35

1 Answers1

8

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Great explanation @T.J. Crowder. It cleared my confusion. I think I have to change my approach. +1 for this clear and detailed answer. – Anant666 Apr 04 '17 at 13:23