Extending for loop values ​​in java

I just realized that when I do this in Java:

for(int x = 0; x < 3; x++)
{
    String bla = "bla";
    bla += x.toString();
}

This (Netbeans in this case) will tell me that I cannot dereference my x-integer this way (as I would in C #).

Why is this?

+5
source share
6 answers

Primitive types are not objects in Java, so you need to use other methods for this:

Integer.toString(x);
+8
source

int is primitive, not Object, and therefore does not have a toString () method.

But you can do this:

String bla = "bla" + x;
+7
source

int , . , int. Integer, . , toString, x String.

+5

x Integer, an int int , toString.

+4

, x int, , . (, toString()). Integer, toString(), toString(), Java .

+4

java (boolean int, short, char, long, float, double) .

However, they have a wrapper type (Integer, Character, ...) that have 1) utilyty static functions and 2) its instances can wrap primitive values.

+3
source

All Articles