File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple concept using *= but ...

 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a snippet,

char e='a';
int d=9;
e*=d;

is not that looking wierd?? for me it does. how could we multiply a char and an int?? but it compiles fine and there was no error.
whereas when we replace e*=d as e=e*d, the compiler has got some err with it.. can anyone please explain.....
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well when you use compound assignment operator, the compiler automatically narrows a broader type...

so char*=int
will become char = (char)char * int;
 
sweety sinha
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char e='a';
int d=9;
e*=d;


here e *= d; the compound assignment operator *= lets you do the calculation
without putting in an explicit cast.
is same as e = char(e * d);
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well ankit, you have given this,
char = (char)char * int;
but still then it did not compile,, have you tried it??
also, i think (char)char makes no sence as it converts a char in to a char again and then it multiplies it with int..
should not be this way(on your logic)???,
char = (char)(char * int);
 
venkatesh badrinathan
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although the compiler implicitly casts, it makes no sence, there is no output(ofcourse multiplying an int and char does not make sence), but why is this option been given to us.. is there any other logic behind this???
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic