aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple concept using *= but ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple concept using *= but ..." Watch "simple concept using *= but ..." New topic
Author

simple concept using *= but ...

venkatesh badrinathan
Ranch Hand

Joined: Aug 03, 2008
Posts: 77
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.....


SCJP1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

well when you use compound assignment operator, the compiler automatically narrows a broader type...

so char*=int
will become char = (char)char * int;


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
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

Joined: Aug 03, 2008
Posts: 77
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

Joined: Aug 03, 2008
Posts: 77
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???
 
 
subject: simple concept using *= but ...