This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Calculating char+= negative byte;

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone:) As the title screen said " no question is too small " , let me see if this one is .

So basically one of the things about += type of operators is that they do not expect an explicit conversion from types( i am generalizing of course, looking more into comparable signed to unsigned type),plus no implicit conversion to int, when used with only 2 operands. So we can have operations of a char and ( negative value) with no complains from the compiler/JVM even without explicit casts.
I am having problems calculating the result of the operation though.



This prints '?' , no matter how i change the b value it stays '?. Only in smaller byte values it changes as expected. For example b=-1 will make a ='B'; b=-2 will make a='A'; and so on.
As this obviously means that after some arbitrary value the char variable will stop decreasing so i tried with the following



Well the result is again '?'. So i have 2 questions , how is this actually calculated (i checked the bytecode and it did not show anything interesting) and why is '?' the value it stops decreasing at. '?' is 63 decimal so it has space to decrease into.
 
Sheriff
Posts: 13557
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not actually the '?' char that's getting printed. Rather, it's a placeholder that indicates an unprintable value. And it's not arbitrary. Every char has an equivalent integer value since char is an integer type. 'C' is \u0043 (hex) or 67 decimal. So you can't subtract anything greater because all char integer values are positive. Search for Unicode values
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , yes, after i posted it i checked a bit more to see if it is actually 63 , and yea its not , its overflowing. The part about having a placeholder about unprintable values is a bit surprising so ill look more into it. I mean if it was changing from ? to a box or smily face, or some other crazy character with the different negative values , it was going to be obvious, but with a constant '?' it got me into a completely different direction.

Thanks again
 
Marshal
Posts: 65074
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I suggest you view the value of the chars with the + operator. Rather than System.out.println use this:-
System.out.printf("0x%04x%n", +a);
The + operator turns the char into an int, so you can see the number and what the result of the subtraction is. If the result is outwith the normal range of the char datatype, you will suffer an overflow error.I tried with -100 and got \uffdf; if you look up this Unicode page, you can find out what \uffdf is.
 
Junilu Lacar
Sheriff
Posts: 13557
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just made a cast System.out.println((int)c); . Anyway , my problem was because the "unprintable character" that i saw looked like a valid one . At some moments you just disregard everything and get a tunnel vision.

Thank you both!

 
Campbell Ritchie
Marshal
Posts: 65074
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.

I was taught that underflow occurs with floating‑point numbers when you have very small numbers and an underflow error causes the result to degenerate to 0.0. I think what we have here is still called overflow (but in the opposite direction).
 
Junilu Lacar
Sheriff
Posts: 13557
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:Ah yes, Campbell is right, although since you're going negative, I think it's technically an underflow rather than overflow.

I was taught that underflow occurs with floating‑point numbers when you have very small numbers and an underflow error causes the result to degenerate to 0.0. I think what we have here is still called overflow (but in the opposite direction).


I think you're right, Campbell: http://programmers.stackexchange.com/questions/32893/is-int-min-1-an-underflow-or-overflow

Although there are certainly folks who think the way I was thinking:
https://cwe.mitre.org/data/definitions/191.html
http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!