It's not a secret anymore!
The moose likes Beginning Java and the fly likes adding to letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding to letters" Watch "adding to letters" New topic

adding to letters

peter huff

Joined: Nov 14, 2005
Posts: 14
Hi, in C you can add a number to a letter and it will change the letter, ie A+1=B (not exactly cos you have to take into account asci). How would I go about doing the same thing in java?
marc weber

Joined: Aug 31, 2004
Posts: 11343

I don't know C, but from what you've posted, it sounds like it's basically the same, since a primitive char is fundamentally a numeric value that you can perform arithmetic on. Just note that a literal char needs to be enclosed by single quotes...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
In other words, Java works (mostly) the same way that C does in this regard.

[ January 29, 2006: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
If you are running Java or C in a USS subsystem under zOS, with the system using the EBCDIC character set, you are not always going to get what you expect with code like this. Same with OS400.

peter huff

Joined: Nov 14, 2005
Posts: 14
ok.... dont really understand that. I was under impression most systems used asci, i mean ebcidic is english and therefore i didnt think in common use as asci was more standardised???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15099

Peter, yes, most systems use ASCII to encode characters. What Guy was trying to tell you is that not all systems use ASCII. There are some operating systems that use EBCDIC, for example. There might be other systems that use other encodings.

So adding 1 to a character does not give you the same result on all systems. The result depends on the character encoding that the system uses.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: adding to letters
It's not a secret anymore!