0

This is a program in which line no 4 generate a compilation error ( possible loss of precision ,required char find int

public class test {
        public static void main(String args[]) {
            char c;
            int i;
            c = 'A';        // 1
                    char ch=32;       //2
            i = c;      //3
            c = i + 1;  //4
            c++;        //5
        }
    }

in line no. 2 char ch=32; we assign in a char variable ch a value 32 but no any error generate. I want the difference between this two line; char ch=32 and c=i+1;

6
  • 1
    Converting shouldn't be confused with casting. In this case you are only asting. Aug 31, 2012 at 9:57
  • @PeterLawrey In fact, he is converting at both line 2 and line 4. Aug 31, 2012 at 10:04
  • @MarkoTopolnik 32 converted to characters is "32", 32 cast to a charcter is ' ' or space. Aug 31, 2012 at 10:08
  • @PeterLawrey That wouldn't be the sense of the term as used by the JLS. Casting is reinterpreting the binary representation of a value in terms of another type, wheras converting is making a whole new value based on the original, according to some rules. You gave two examples of converting an int into a String, respectively char. Aug 31, 2012 at 10:24
  • 1
    @PeterLawrey OK, but in reality it's not a cast. The same Java syntax is used both for casts and conversions. Java doesn't allow any casting between primitive values as that would make it a weakly-typed language like C. Casts are only done between reference types. Aug 31, 2012 at 10:28

8 Answers 8

5

The reason of the error is that char is 2 bytes and int is 4 bytes. And java won't let you do implicit casts where the variable would loose it's high order bits. You have to make an explicit cast from int to char.
In the other direction, assigning an int value to a 'numeric' holder will work until you don't try to assign something out of range. ( More than 2^16-1 in this case)

4
  • 1
    int is 4 bytes all of the time. Aug 31, 2012 at 10:05
  • @MarkoTopolnik: I've read some time ago somewhere that not all of the time. Maybe you're right if we stick to the JVM specification. I don't know it by heart... Also, my source might be inaccurate as well...
    – zeller
    Aug 31, 2012 at 10:08
  • 1
    Well, if we don't stick to the Java Language Specification, then we're not talking about Java anymore :) You may have read something to do with the amount of memory allocated for an Integer or similar, but an int is a 32-bit signed value. Aug 31, 2012 at 10:19
  • @MarkoTopolnik: you're right, from this point of view, it is irrelevant, that an int is not four bytes in other languages...
    – zeller
    Aug 31, 2012 at 11:55
3

In both cases you are requesting a conversion from a signed 32-bit int into an unsigned 16-bit char. In this line the compiler can make sure the number 32 fits into the char:

char ch=32;

In this line the compiler only knows you are converting some int value to a char so it doesn't know for sure it can fit:

c = i + 1;

Java insists on explicit cast operator whenever it is not 100% certain that there will be no precision loss.

0
1

loss of precision means that INTEGER has a larger value and CHAR is smaller so you can't fit something which is to large for that space .

that's why you are getting that error .

you need to type cast the INTEGER into CHAR but still the precision will be lost because you just can't fit INTEGER into CHAR but if the integer value is smaller than loss precision can be ignored.

1

Converting shouldn't be confused with casting. In this case you are only casting. Some operations have implicit casting, and others do not. e.g. = does not so you have specify you can to cast. ++ and += does implicit casting.

e.g.

char ch = '0';
char ch2 = 2; // implicit casting
ch *= 1.1; // implicit casting
ch2++; // no casting required. 
ch2 += 1; // implicit casting.
ch2 = ch2 + 1; // won't compile, casting required.
0

Use this:

c = (char) i + 1;

But make sure that your i is in range of char values

0

I am not sure I understand what it is that you want....but:

  • Line 2 works because the literal number '32' can be represented in the 2-byte that is a char
  • Line 4 fails because it says that if you try to assign a 4-bytes value (int) to a 2-byte location (char), it needs a cast.

Now, if what you want is just doing this kind of char arithmetic, you can:

  • make 'i' a char.
  • add a cast to line 4.

One of the two will work but, from the snippet you show, probably making 'i' a char makes more sense....

0

The reason behind it is that compiler can not automatically convert higher data type value to lower datatype value..

Here int is Higher Datatype and Char is lower datatype...

i+1 evaluates to integer result which cannot be fit into char type..

Image for Datatype Automatic Conversion

Higher to Lower is not possible

Lower to higher is possible

0

As defined in the Character class, chars in java are UTF-16 representations of characters. So char as a size of 16 bits.

In your first assignment, you're assigning 32, which can be stored in 16 bits, to an UTF-16 character so that's fine.

But when you try to convert any kind an int as i, you're converting from a 32 bits data to a 16 bits one, an operation that can't be implicit.

short a = 32; // fine 
short b = 3243334; // can't compile
char c = 32; // fine
char d = 3243334; // can't compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.