• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple concept using *= but ...

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
reply
    Bookmark Topic Watch Topic
  • New Topic