Domanda

When I type the statement:

String population_construction_str = "some externally set value";
int answer_counter = 0;  
answer_counter = population_construction_str > 6 ? 2 : 1;

I get a usable result (which is apparently always true). I figured out what my error was. I needed to get the LENGTH of the string to make this behave was expected. But prior to my doing that, I was clearly getting a value that was > 6.

Here is my question: What was this string returning that was greater than six? Will the value it returns always be greater than six? Why was my compiler not returning an error? And is knowing what a string returns when queried this way useful at all?

I hope this question is not too open-ended, and for me it is simply enough to know generally what the string was returning when queried in this fashion and why it does that. Thank you.

È stato utile?

Soluzione

That string wasn't returning anything. In fact, as @ZouZou mentioned, it gives this error "The operator > is undefined for the argument type(s) String, int" and won't even compile. Please check that and if you aren't getting the compiler error with the code posted above, we'd all like to know what java version you're using and IDE if any.

Altri suggerimenti

Your code will show a compilation error.

enter image description here

Try with,

answer_counter = population_construction_str.length() > 6 ? 2 : 1;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top