I tried two examples of how you are doing for your question. Based on this, I must say that the second approach will be better. ( Although I do not consider Multi-Threading )
Test.java
public class Test{ public static void main(String... args){ String[][] arr2 = new String[5][5]; for (final String[] obj : arr2) { for (final String str : obj) System.out.println(str.length() +" " + obj.length); } } }
after compiling and then decompiling again I got this.
* Decompiled with CFR 0_114. */ import java.io.PrintStream; public class Test { public static /* varargs */ void main(String ... arrstring) { String[][] arrstring2; String[][] arrstring3 = arrstring2 = new String[5][5]; int n = arrstring3.length; for (int i = 0; i < n; ++i) { String[] arrstring4; for (String string : arrstring4 = arrstring3[i]) { //assignment will take place m*n. System.out.println("" + string.length() + " " + arrstring4.length); //this arrstring4.length will execute m*n (in this case).So, this will less efficient than others. } } } }
Test1.java
public class Test1{ public static void main(String... args){ String[][] arr2 = new String[5][5]; for (final String[] obj : arr2) { int value = obj.length; for (final String str : obj) System.out.println(str.length() +" " + value); } } }
after compiling and then decompiling again I got this.
import java.io.PrintStream; public class Test1 { public static void main(String ... arrstring) { String[][] arrstring2; for (String[] arrstring3 : arrstring2 = new String[5][5]) { int n = arrstring3.length;
source share