Why xor operation on constants (character) is working differently when used variables in its place

afzalex

Have a look at the following code :

Case 1 :

char a = 'x' ^ 'y';

It is working fine. But when I use variable instead of constants as here :

Case 2:

char x = 'x';
char y = 'y';
char a = x ^ y; // Error!

In java language : case 1 is working and value of a comes out to be 1 but case 2 is not working.

In C and C++ : both cases are working, and the value of a comes out to be 1

In C# : both the cases are not working.

In javascript : both cases are working, which is not a HLL, and value of a comes out to be 0.


I understand that java is converting variables to integer to do binary operation, but why it is working in case 1 and not in case 2, Why same is not working in C# And why the values are different in the case of javascript.

Updated When I made the variables final than it is working in java, but still not in C#

final char x = 'x';
final char y = 'y';
char a = x ^ y;

But still I cannot understand why constants are working but when using variable they are not. And why same is not happening with other high level programming language.
I think it is a basic operation and should be working in all programming languages with same behaviour.

Note To test all above cases in javascript I am replacing 'char' with 'var' in all cases and they are working.

yshavit

Answering for Java only.

The expression 'x' ^ 'y' is a constant expression; x ^ y is not, unless both variables are declared final. Furthermore, the result is an int; ^ is an integral bitwise operator, meaning that both operands have to be promoted to an integral type before being evaluated. char promotes to int.

So you have this int expression, and you try to narrow it to a char. In the general case, this could lead to a loss of precision (ints are 4 bytes, chars are 2), so the compiler doesn't let you do that without you explicitly stating it's what you want to do (via a cast to char). However, you can implicitly narrow constant expressions, if their value would fit into the new type. From JLS 5.2:

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

(Emphasis added)

Intuitively, this makes total sense: the error is there to tell you that you may lose precision, and so it wants you to confirm that you know that; in a sense, it's a loud warning. But if the compiler can know absolutely that this won't happen, as it can for a constant expression, then it makes things a bit easier for you and "hides" that warning.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Why isn't my class constructor initializing its member variables?

来自分类Dev

Why does a space preceding a non-combining diacritic function differently when using IndexOf(string) and IndexOf(char)?

来自分类Dev

How can I delete a user in linux when the system says its currently used in a process

来自分类Dev

In R, transform a character string into an operation

来自分类Dev

Why is the operation on k might be undefined?

来自分类Dev

R rstats How to replace a single character in one place in a string

来自分类Dev

Why \g<0> behaves differently than \0 in re.sub?

来自分类Dev

Performing operation using Generic and primitive type not working

来自分类Dev

WSO2 EMM - WIFI operation not working

来自分类Dev

Why custom fields are used in Braintree?

来自分类Dev

JISON: why in regexp used "." instead of \.?

来自分类Dev

Where to look for to get the possible values of String constants used in Java class library

来自分类Dev

Why indexOf in javascript not working?

来自分类Dev

why is this sed command not working?

来自分类Dev

How to input unicode character and get its numeric value

来自分类Dev

Why won't Ubuntu Terminal accept "\" character?

来自分类Dev

Why is this Bootstrap checkbox not working with jQuery?

来自分类Dev

Why is custom system classloader not working?

来自分类Dev

Meteor React - Why are React Components defined differently in React Mounter vs React Layout from Kadira?

来自分类Dev

Place a lock on a record when inserting in mysql during a transaction

来自分类Dev

Which integral promotions do take place when printing a char?

来自分类Dev

The 'inherits' keyword is not allowed when a 'model' keyword is used

来自分类Dev

drc: Error in drm when used interaction terms

来自分类Dev

Hexadecimal Constants

来自分类Dev

Android Studio : My Layout appears Differently when the app installed on different screen size Help me someone

来自分类Dev

Why does Ruby use its own syntax for safe navigation operator?

来自分类Dev

Why does the pattern ignore the space inside character class

来自分类Dev

Why can't jQuery 3 identify the '#' character in an attribute selector?

来自分类Dev

Why isn't overflow: hidden working?

Related 相关文章

  1. 1

    Why isn't my class constructor initializing its member variables?

  2. 2

    Why does a space preceding a non-combining diacritic function differently when using IndexOf(string) and IndexOf(char)?

  3. 3

    How can I delete a user in linux when the system says its currently used in a process

  4. 4

    In R, transform a character string into an operation

  5. 5

    Why is the operation on k might be undefined?

  6. 6

    R rstats How to replace a single character in one place in a string

  7. 7

    Why \g<0> behaves differently than \0 in re.sub?

  8. 8

    Performing operation using Generic and primitive type not working

  9. 9

    WSO2 EMM - WIFI operation not working

  10. 10

    Why custom fields are used in Braintree?

  11. 11

    JISON: why in regexp used "." instead of \.?

  12. 12

    Where to look for to get the possible values of String constants used in Java class library

  13. 13

    Why indexOf in javascript not working?

  14. 14

    why is this sed command not working?

  15. 15

    How to input unicode character and get its numeric value

  16. 16

    Why won't Ubuntu Terminal accept "\" character?

  17. 17

    Why is this Bootstrap checkbox not working with jQuery?

  18. 18

    Why is custom system classloader not working?

  19. 19

    Meteor React - Why are React Components defined differently in React Mounter vs React Layout from Kadira?

  20. 20

    Place a lock on a record when inserting in mysql during a transaction

  21. 21

    Which integral promotions do take place when printing a char?

  22. 22

    The 'inherits' keyword is not allowed when a 'model' keyword is used

  23. 23

    drc: Error in drm when used interaction terms

  24. 24

    Hexadecimal Constants

  25. 25

    Android Studio : My Layout appears Differently when the app installed on different screen size Help me someone

  26. 26

    Why does Ruby use its own syntax for safe navigation operator?

  27. 27

    Why does the pattern ignore the space inside character class

  28. 28

    Why can't jQuery 3 identify the '#' character in an attribute selector?

  29. 29

    Why isn't overflow: hidden working?

热门标签

归档