This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 9291
    
  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???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: simple concept using *= but ...
 
Similar Threads
please explain. char problem.
char doubt
WHIZlabs scjp 1.4 simulator error?
Dans mock - char escape sequences
Switch statement