Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Can someone please explain this char Array loop?  RSS feed

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I saw this in an example, but I don't understand the meaning behind the loop numbers.  

What is the loop beginning at 48 and going to 57 supposed to mean?

If I change the numbers from 0 to 10, it doesn't work, but all the numbers are less than 10 that it prints out in order.  However, none of the numbers are between 48 and 57.  What is the significance of those numbers of why it is looping?  I printed out the numeric value at each char as well, but it is not showing they are different than 1,9,7.  

 
Saloon Keeper
Posts: 9996
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does (char) i do?
 
Nathan Milota
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What does (char) i do?



It's just saying if the (char)i is equal to the character, it is printed.  I don't know what exactly it does though.
 
Marshal
Posts: 63827
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java® Language Specification (=JLS) about Narrowing Primitive Conversions and about the == and != operators and about binary numeric promotion, and that will tell you what the cast is doing.
 
Campbell Ritchie
Marshal
Posts: 63827
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from, to avoid copyright problems, and so we can know who uses int literals like 57.
 
Stephan van Hulst
Saloon Keeper
Posts: 9996
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:I don't know what exactly it does though.


(char) 48 does the inverse of (int) '0'. char is actually a numeric data type, but when you print them they look like text characters. The text character '0' is actually represented by the numeric value 48.

For a full list of what numeric values correspond to what textual characters, see the UTF-16 Basic Multilingual Plane. The most relevant characters for you are in the Basic Latin Block. As you can see, the character '0' is associated with the hexadecimal value 0x30, which corresponds to 48 in decimal.
 
Ranch Hand
Posts: 99
Java Linux Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running this code and looking up ascii codes.

 
Campbell Ritchie
Marshal
Posts: 63827
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I suggest you google "ascii table". . . .

Since other people have explained where the 57 comes from, I can say I would prefer to take out a contract on whoever wrote 57 rather then '9'

And if you are going to use ints instead of chars, at least write them in hex: not 57 but 0x39. Even the ints are easier to understand in hex.
 
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write this: String.format("The integer (%d) has an ascii value-> %c", i, i)

more succinctly like this: String.format("int %d is equivalent to char '%1$c'", i)
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer
When you use explicit indices and you later add a new parameter then you have to do a bunch of mental arithmetic and hope you don't introduce a bug.
 
Junilu Lacar
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:you have to do a bunch of mental arithmetic and hope you don't introduce a bug.


I'm lazy. I write tests to watch my six against bugs so I don't have to do mental arithmetic and rely on hope. Your alternative is good in this case though. However, you can't always use "<" if you're trying to reference specific parameters in a different order as they're listed.
 
Danger, 10,000 volts, very electic .... tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!