aspose file tools*
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 Java 8 in Action this week in the Java 8 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
Why is this behaviour?
Doubt with assignment
a question about conversion
cast of int to char
convert char to int in a string?