Comprender la sintaxis con experiencia previa en Objective-C

I'm new to Java. I've got 3 months experience of Objective-C so I understand its concepts quite easily. I am trying to port a simple factorial-example program written in Java to Objective-C for learning purposes. But there is this one line, I do not understand what is going on here:

for (int i; i < 10; i++){
println (i + "! =" + factorial(i));

Ahora la for loop and all that is fine, but this "! =" I´ve never seen -- what does it do? To me it looks like a "not equal to" operator, wich would make no sense i + not equal too + factorial (factorial is a method, by the way).

preguntado el 27 de agosto de 12 a las 10:08

"this" is a String and "5" is also a string an "! =" too -

Note the highlighting here and also in your IDE. Everything in between two quotation marks is a String and has no functionality. It's only data. -

5 Respuestas

This is building a string.

"+" in Java can be used to concatenate String. This happens if one of the parts is a String, and all other things are also converted to Strings (like the ints here).

The "!= " is just a String literal, not anything that Java sees.

In Objective-C, it would be

 [NSString stringWithFormat:@"%d! = %d", i, factorial(i)]

Note that in Java, you can also do

 String.format("%d! = %d", i, factorial(i));

which may be more familiar to you.

Respondido 27 ago 12, 10:08

However, the space between ! y = is important because is not a comparison :) - Sultán

Depends on your definition of important. This whole string is just output formatting, it does not do anything Java-side. - Thilo

! is the mathematical symbol for factorial. I presume that println takes a string and prints it to the console (or somewhere else), so that line is simple printing the result.

Respondido 27 ago 12, 10:08

That is simply a String that is concatenated in the output, producing the following output:

1! =1
2! =2 

In short, it isn't an operator at all, but rather a string literal.

Respondido 27 ago 12, 10:08

it is a String. The Output done by println will be "the value of i" then there will stand ! = and than whatever factorial(i) returns... This is actually very very basic stuff.

Respondido 27 ago 12, 10:08

its just text, that get printed out

so your console output would be

1! =1  
2! =2  
3! =6  

Strings in Java are in "" and concatination is done with the +

println ("the factorial of" + i + " is " + factorial(i)); 

would have been the same, but without confusing you

Respondido 27 ago 12, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.