Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operators

 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body confirm me pls if I am right.
I tested through code
1) shift operators can be applied to char type operand
2) the unary +,- can not be applied to char type operand
3) ~ (bitwise inversion)can be applied to char type operand

 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body confirm me pls if I am right.
I tested through code
1) shift operators can be applied to char type operand
char is promoted to int.
2) the unary +,- can not be applied to char type operand
char is unsigned.
3) ~ (bitwise inversion)can be applied to char type operand
char is promoted to int.
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char a='s';
char b='o';
System.out.println(a);
System.out.println(b);
System.out.println(a | b);
System.out.println(a ^ b);
prints out s,o,127,28

means u can even perform logical operations on char as well.
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u Mansoor and Ego.I was just filling up a chart for what operators can be applied to what type operands...
thanks for the help..
Sathi
 
Pratiti Naphade
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they are not performed on char...
in the prnt line it is converted to int.
try this
char a = 'a';
char b = 'b';
char c = a& b;//error
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You nust cast it:
char c=(char) a&b;
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because result type of all binary operation should be at least int type.
int a;
char c='a';
char d='b';
a=c&d;// works fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic