3

I had this question on a software development quiz and I'm not sure the reasoning behind the answer.

unsigned int x = 1;

while(x > 10) {

System.out.print(x);

x++;

}

My answer was "The code compiles, but there is no output." The correct answer is that "The code does not compile."

Would the compiler not try to run since there are no clear syntax errors?

  • 11
    There is no `unsigned` keyword in Java. – Pshemo Sep 29 '14 at 17:32
  • http://stackoverflow.com/questions/9854166/declaring-an-unsigned-int-in-java – Victor Sep 29 '14 at 17:34
  • 1
    when do you expect `1 > 10` to be true? – Peter Lawrey Sep 29 '14 at 18:11
  • The syntactic error has been already pointed out (unsigned). Just as a further remark: the loop is clearly unreachable code, though syntactically still correct; however, if variable x were declared as final, then the compiler would actually give you an error on the loop for this reason. – cornuz Sep 29 '14 at 18:27

3 Answers3

5

unsigned int x = 1;

That's not valid syntax in Java.

Change it to

int x = 1;

Eran
  • 387,369
  • 54
  • 702
  • 768
5

in valid key word unsigned in java but in latest version java 8 of oracle there is an explicit api for long type unsigned

https://blogs.oracle.com/darcy/entry/unsigned_api u may look The above documentation for more detail Hope IT may help u

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
1

Try running it in an IDE like eclipse. It will show you where the error is.

In this case, I am 99% sure that unsigned int x = 1; is wrong, and should just be int x = 1;

starvator
  • 989
  • 1
  • 11
  • 26