File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes conversion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "conversion question" Watch "conversion question" New topic

conversion question

Serge Batov

Joined: Jul 11, 2002
Posts: 1
Hi, I do not understand that this following code compiles just fine

char c = 5;
int i = 10;
c +=i;
But this
char c = 5;
int i = 10;
c = c + i;
give a compile error: possible loss of precision...
What's a difference between those two assignments?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
See here. A compound assignment operator like += includes an implied cast, which is missing in your second example.

"I'm not back." - Bill Harding, Twister
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Knew I was forgetting something...
I agree. Here's the link:
subject: conversion question
Similar Threads
a question about conversion
Why is this behaviour?
convert char to int in a string?
cast of int to char
Doubt with assignment