0

When we are in a for loop,

for(int i =0; i< 10 ; i++)
{
   System.out.println(i + 1);
}

will execute 1-10

However, if I change System.out.println to lets say a function passed such as

for(int i=0;i<10;i++)
{
passed( "Row" + (i+1));
}

Where passed is

public static void passed(String check) {

System.out.println(check + "Passed");
}

It executes

01 Passed 11 Passed 21 Passed 31 Passed.... 41 51 61

and so forth

Can anyone explain why does it pass the value first and skip the math?

1
  • 1
    "Row" + (i+1) or "Row" + i+1? Commented Sep 26, 2014 at 14:18

3 Answers 3

5
System.out.println(i + 1);       --> println(int) called
System.out.println(check + "Passed"); --> println(String) is called.

java code :

public static void main(String[] args) {
    int check = 0;
    System.out.println(check + "Passed"); //StringBuilder operation int + string concatenated as Strings then println is called.
    System.out.println(check + 5); // int + int concatenated as int, then println is called 
}

byte code :

 0:   iconst_0
   1:   istore_1
   2:   getstatic       #16; //Field java/lang/System.out:Ljava/io/PrintStream;
   5:   new     #22; //class java/lang/StringBuilder
   8:   dup
   9:   iload_1       
   10:  invokestatic    #24; //Method java/lang/String.valueOf:(I)Ljava/lang/Str
ing;
    // use StringBuilder and invoke println()
   13:  invokespecial   #30; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
   16:  ldc     #33; //String Passed
   18:  invokevirtual   #35; //Method java/lang/StringBuilder.append:(Ljava/lang
/String;)Ljava/lang/StringBuilder;  
   21:  invokevirtual   #39; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
   24:  invokevirtual   #43; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V --> string println called
   27:  getstatic       #16; //Field java/lang/System.out:Ljava/io/PrintStream;
   30:  iload_1              // load value of check(int)
   31:  iconst_5           ---->  constant 5 
   32:  iadd               ----> add constant 5 and value of check and then invoke method
   33:  invokevirtual   #48; //Method java/io/PrintStream.println:(I)V --> integer println called
   36:  return
Sign up to request clarification or add additional context in comments.

9 Comments

I am passing passed( (i+1) ), shouldn't it do the math first?
@OctavianRox: I suggest posting a full example that exhibits the behavior -- it's not clear if you're doing something to cause it without realizing it.
Even without println (int) method, it would give the same result: int -> (boxing) -> Integer <- Object, while there is method println (Object), which just runs toString on it.
Now that's a hell of an explanation. Withdrawn dislike.
@OctavianRox - The Java source code (.java file) when compiled generates a .class file (known as bytecode). This contains the code after the compiler has parsed it.
|
4

Java apply a particular rule when it comes to mixing String and any numerics (int, double, ...):

System.out.println(1+1+""); //prints 2

however

System.out.println(""+1+1);//prints 11

How's that ?

Java will read from right to left and give priority to String. So, in the first case Java will evaluate int + int = int before evaluating int + String = String. But in the second example, it will be String + int = String being first evaluated then String + int = String.

1 Comment

That is the coolest thing I've seen the whole day! Thanks for educating me
2

When you are calling System.out.println directly, the run time can determine that the value being passed is an int and that addition should be performed before the println (for int values) is executed. When you are wrapping that call up in a function which takes a String parameter and passing your int value into that function, the int value is being coerced into a String value as passed is called and then concatenation is being done instead of addition before the println (for String values) is executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.