equality between the values assigned to char
John Lerry
Ranch Hand
Posts: 145
1
posted 1 year ago
This is not on the OCA exam (as mentioned in the explanation of the exercise as well).
So for completeness: it doesn't matter which number system (binary, octal, hexadecimal, decimal,...) is used to express the integral types (byte, short, int, and long). If for example an int value expressed in binary and another long value expressed in hexadecimal have the same value, they are equal; otherwise they are not. It's really that easy!
In order to calculate the value of such an integral type, you need to know which number system is used and calculate the value appropriately. And they all work very similar to the decimal number system you are familiar with, the only difference is the base (radix) of each number system is different (binary = 2, octal = 8, decimal = 10, hexadecimal = 16)
Sp let's start with the numbers from your example:
78 > decimal > 7 * 10^1 + 8 * 10^0 = 7 * 10 + 8 * 1 = 78 (obviously) 0x4e > hexadecimal > 4 * 16^1 + 14 * 16^0 = 4 * 16 + 14 * 1 = 78 (in hexadecimal: A=10, B=11, ..., F=15)
Another example: 0b00100001 > binary > 0 * 2^7 + 0 * 2^6 + 1 * 2^5 + 0 * 2^4 + 0 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0 = 0 * 128 + 0 * 64 + 1 * 32 + 0 * 16 + 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 = 33
And if you look for example to the Integer class you'll see that the static parseInt, toString and valueOf methods are overloaded and you can pass a radix. So you could for example decide to represent some number into the 35base number system (which is used in validating an IBAN number, a handy little known fact ). It's very similar to hexadecimal, you simply have much more letters which are valid: A = 10, B = 11, ..., Z = 35. An example:So you can have some fun with your own name as well Output: roel: 28 > 611933 ; 31 > 827876 ; 35 > 1187536
Hope it helps!
Kind regards,
Roel
 2
John Lerry wrote:How do I evaluate equality between different values (eg. between an int decimal and an int hexadecimal)?
This is not on the OCA exam (as mentioned in the explanation of the exercise as well).
So for completeness: it doesn't matter which number system (binary, octal, hexadecimal, decimal,...) is used to express the integral types (byte, short, int, and long). If for example an int value expressed in binary and another long value expressed in hexadecimal have the same value, they are equal; otherwise they are not. It's really that easy!
In order to calculate the value of such an integral type, you need to know which number system is used and calculate the value appropriately. And they all work very similar to the decimal number system you are familiar with, the only difference is the base (radix) of each number system is different (binary = 2, octal = 8, decimal = 10, hexadecimal = 16)
Sp let's start with the numbers from your example:
Another example: 0b00100001 > binary > 0 * 2^7 + 0 * 2^6 + 1 * 2^5 + 0 * 2^4 + 0 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0 = 0 * 128 + 0 * 64 + 1 * 32 + 0 * 16 + 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 = 33
And if you look for example to the Integer class you'll see that the static parseInt, toString and valueOf methods are overloaded and you can pass a radix. So you could for example decide to represent some number into the 35base number system (which is used in validating an IBAN number, a handy little known fact ). It's very similar to hexadecimal, you simply have much more letters which are valid: A = 10, B = 11, ..., Z = 35. An example:So you can have some fun with your own name as well Output: roel: 28 > 611933 ; 31 > 827876 ; 35 > 1187536
Hope it helps!
Kind regards,
Roel
posted 1 year ago
 2
The Unicode escape sequence in this line does not do what you think it does:
The \u004e is not equivalent to 0x4e or 78.
Unicode escape sequences are replaced by the corresponding Unicode characters by the compiler. The line above is exactly the same as writing this:
(because 4e is the chararacter code of the capital letter N). This will not compile if you don't have a variable named N.
If you want this to compile, you have to put it between single quotes, to make it a char constant:
Roel explained about different number systems. You cannot distinguish between 0x4e, '\u004e' and 78 because they are all exactly the same number. The way in which a number is displayed (hexadecimal, as a character constant or decimal) is not a property of the number itself.
John Lerry wrote:
The \u004e is not equivalent to 0x4e or 78.
Unicode escape sequences are replaced by the corresponding Unicode characters by the compiler. The line above is exactly the same as writing this:
(because 4e is the chararacter code of the capital letter N). This will not compile if you don't have a variable named N.
If you want this to compile, you have to put it between single quotes, to make it a char constant:
Roel explained about different number systems. You cannot distinguish between 0x4e, '\u004e' and 78 because they are all exactly the same number. The way in which a number is displayed (hexadecimal, as a character constant or decimal) is not a property of the number itself.
posted 1 year ago
So you can use the unicode escape sequences to write your complete code Of course just for laughs (or maybe for April Fools' Day) because the code isn't really readible (and that's quite an understatement). Your coworkers would probably be if you commited some code looking like this to the source code repository Now assume this file is in a source code file with the name MySourceFile.java you can compile and run this class as follows:
Kind regards,
Roel
(Disclaimer: of course this is just a nice little known fact, it's not required for the OCA exams)
 2
Jesper de Jong wrote:Unicode escape sequences are replaced by the corresponding Unicode characters by the compiler.
So you can use the unicode escape sequences to write your complete code Of course just for laughs (or maybe for April Fools' Day) because the code isn't really readible (and that's quite an understatement). Your coworkers would probably be if you commited some code looking like this to the source code repository Now assume this file is in a source code file with the name MySourceFile.java you can compile and run this class as follows:
Kind regards,
Roel
(Disclaimer: of course this is just a nice little known fact, it's not required for the OCA exams)
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/ThreadBoostfeature
