1

I am studying for my exam and i just found out that converting from byte to char in java is considered narrowing. According to the book, the storage of char is the same as byte, but it's still considered narrowing. Can somebody explain why?

Also, Converting from Byte to char is also a narrowing! I thought that the if A>B then B<A this doesn't apply here right ?

I know it's a stupid question.

enter image description here

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
elGreato
  • 140
  • 1
  • 14
  • Are you sure this is correct? I thought a char in Java was UTF-16, alternatively this could be byte -> unsigned char issue but thats a C/C++ issue these days. Character encodings in files might also make the original statement true but this seems unlikely. – PeterI Jan 13 '16 at 15:02
  • 5
    "According to the book, the storage of char is the same as byte" - then the book is wrong. – davmac Jan 13 '16 at 15:02
  • Is this a strictly Java question? That's implied by how you worded your question, but you don't say it clearly, and you didn't use the `Java` tag. – Andrew Henle Jan 13 '16 at 15:02
  • i edited it now, sorry for not adding the tag, i also attached the picture from the book – elGreato Jan 13 '16 at 15:05
  • 2
    Tell us what book this is so we can make sure never to read it. – Mad Physicist Jan 13 '16 at 15:05
  • Also, `Byte` and `byte` are very different things. – Mad Physicist Jan 13 '16 at 15:07
  • Looks like a typo to me. Note that in some contexts i.e. byte -> 7 bit ASCII character narrowing would occur but this is unlikely to be the issue the author was thinking of. – PeterI Jan 13 '16 at 15:07
  • so is it wrong ? from byte to char is not narrowing ?? – elGreato Jan 13 '16 at 15:07
  • 2
    Your question is not stupid at all when you consider that your intuition is correct and you are being confused by bad information. – Mad Physicist Jan 13 '16 at 15:08
  • http://stackoverflow.com/questions/3737100/why-does-a-byte-in-java-i-o-can-represent-a-character – Gaël J Jan 13 '16 at 15:08
  • 3
    A `byte` is 8 bits, a `char` is 16 bits, so the storage for `char` is not the same as for `byte`. The book is wrong. – Jesper Jan 13 '16 at 15:08
  • thank you guys, so i guess it's better to delete this question in this case. – elGreato Jan 13 '16 at 15:11

3 Answers3

3

byte to char may effectively be considered a narrowing conversion in Java because it introduces the possibility for information loss. While it is true that byte is an 8-bit quantity and char is 16-bit, keep in mind that char is the only unsigned primitive type in Java. This means that converting a byte to a char will truncate off any negative values. The reverse conversion is also narrowing because any numbers greater than 127 will be truncated when converting to byte. Java does conversion differently than C++, where signed to unsigned just means that the compiler will interpret the bits differently, but the actual number does not change.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
2

Actually conversion from byte to char requires both a widening and a narrowing conversion (one after the other). This is precisely what the Java language specification requires. In section 5.5:

Casting contexts allow the operand of a cast operator (§15.16) to be converted to the type explicitly named by the cast operator.

Casting contexts allow the use of one of:

(...)

  • a widening and narrowing primitive conversion (§5.1.4)

The table from the book looks incorrect (I'm assuming the caption is supposed to be "Java narrowing conversions"). There is no narrowing conversion from byte to char. The actual widening and narrowing primitive conversions are listed in the JLS sections 5.1.2 and 5.1.3.

Note that char can be converted to byte by a narrowing conversion alone, but byte cannot be converted to char by a widening conversion alone. (And, to make things just that extra bit complicated, conversion between char and short is a narrowing conversion in both directions!).

To answer your question succintly: Yes, primitive conversion to char is always a narrowing conversion - but it may require a widening conversion to be performed first!.

davmac
  • 20,150
  • 1
  • 40
  • 68
0

Byte is a signed primitive type which means it can represent both negative and positive from values of -128 to 127.

Char is an unsigned primitive type which means it cannot represent negative values.

I’m reading the same book and my professor just covered this.

Therefore the book is not wrong it is correct.

Lio
  • 39
  • 8