• Post Reply Bookmark Topic Watch Topic
  • New Topic

Char and int?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I have come across some code that just confuses me,ok so I know how lastRow works it is assigned the ascii value of 'A' + 16,

but what I don't get is how can row be compared to lastRow(<= lastRow) when row is a char and lastRow is an int?


 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A char is corresponding with a integer value in a specific type of encoding. For example in ascii table, the character 'a' will corresponding with value 97. So that basically you can compare a char with an int value.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct; a char is not stored as a letter but as an unsigned integer. It is actually the only unsigned type in Java®.
Don't use magic numbers like 16 without explanation. Avoid the <= operator in a for loop as far as possible because there is a risk of exceeding the limits of the size of an array. Without running the loop, but just from reading the code, please tell us how many times that loop will run.

Always start writing a for loop with this format:-That is the basic format which you shou‍ld always use for the simple reason that it works. You may in special circumstances have to change that format, for example if you compare myArray[i] with myArray[i + 1].
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so a char is actually an unsigned int?

so this code would be legal?




and also this code seems to work



it prints out the number 101 which I assume is the ASCII value for 'e' but I also see people use a cast before this operation int tNum = (int) t; when I do this I get the same value printed so what's the point of casting when nothing changes?

thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:when I do this I get the same value printed so what's the point of casting when nothing changes?


An implicit cast from char to int is allowed because the range of int is a superset of the range of char.

And since an implicit cast is allowed, then an explicit cast should also be allowed.  Are you saying that it shouldn't be allowed?

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply

so why would people cast a char to an int when the cast can be done implicitly?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

also how come this isn't legal?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
also how come this isn't legal?


Line 4 isn't valid Java syntax.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
so why would people cast a char to an int when the cast can be done implicitly?


Well, perhaps some people find it easier to read? Regardless, the compiler doesn't care. If it can be cast implicitly, the compiler will allow you to do it explicitly.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote:
also how come this isn't legal?


Line 4 isn't valid Java syntax.

Henry


how come it isn't valid?

why can you do the other way around
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:so a char is actually an unsigned int? . . .
No. An int occupies 32 bits and a char 16 bits. You can use a char in arithmetic, as you are doing, because it undergoes promotion to 32 bits, thereby becoming equivalent to an int. That is one of the widening primitive conversions which can be done automatically; there is a list of them in the Java® Language Specification. All nineteen of them.
If you try printing a char unchanged, it is usually converted back to its Unicode counterpart (character) but you can promote it yourself by doing several things. Four shown here:-
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
how come it isn't valid?


Can you elaborate, what this line...... is supposed to be doing? And why you think the Java compiler should be able to compile it?

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote:
how come it isn't valid?


Can you elaborate, what this line...... is supposed to be doing? And why you think the Java compiler should be able to compile it?

Henry



thanks Campbell and Henry

as for that line I want to be able to print the next letter after that one so I thought if I did
               


result would be equal to the next letter as I'm adding the ascii number + 1 but for some reason it doesn't work
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also sorry Henry that was a typo

I was supposed to type char result = b + a;
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this, as long as you stay within the limits of a char:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
result would be equal to the next letter as I'm adding the ascii number + 1 but for some reason it doesn't work


The result of the "b + a" expression is an int, and an int can't be implicitly cast to a char -- unless it is a compile time constant (and within range), which it isn't. And of course, the reason the implicit cast isn't allowed is because the range of a char is not a superset of the range of an int.... perhaps, you meant to explicitly cast here?

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys that makes sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!