Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1146 :

Posted: Wed Oct 22, 2014 12:43 pm
by emailgutta
For this question, in the Explanation section it says as below.what does it mean when it says ''A char can be used as an unsigned integer ''

There no unsigned keyword in java! A char can be used as an unsigned integer

Re: About Question enthuware.ocajp.i.v7.2.1146 :

Posted: Wed Oct 22, 2014 6:31 pm
by admin
It means if you want to use an unsigned integer, you can use a char if your number lies between 0 and the max value of a char i.e. 2^16 - 1 because size of char is 2 bytes. A char cannot store negative numbers.

Re: About Question enthuware.ocajp.i.v7.2.1146 :

Posted: Mon Dec 07, 2015 5:05 pm
by ghostGuiggs
Hello

In my test this question is :

What will the following program print?
public class TestClass{
public static void main(String[] args){
unsigned byte b = 0;
b--;
System.out.println(b);
}
}

I do not agree with the answer given because in my opinion, the right answer should be "It will not compile" because "There are NO unsigned keyword in java". when i saw the answer given as "0" i was so surprised that i checked the code in my NetBeans with JDK 7.
The code indeed did not compile ! when i remove the word "unsigned", it compiles fine and prints "-1" as a result which is normal since byte values are between -128 and 127.

can you explain why the answer in the test prints "0" please.

Thank you in advance.

Re: About Question enthuware.ocajp.i.v7.2.1146 :

Posted: Mon Dec 07, 2015 9:10 pm
by admin
The correct answer is indeed it will not compile.
There is no unsigned keyword in java. If you want to store unsigned integers, you can use a char instead.

HTH,
Paul.