Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Came across this accidentally

 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I was creating a small program to create some random letters, and I came across this actually through a typo, but was interested how it actually happened



AS you can see this is a simple code where random characters are being generated. Instead of typing System.out.print(a+","), I typed System.out.print(a+',') and I got the output as a long list of numbers. Could anyone tell me how the numbers got generated?

PS: I did not get an error, I got the output as a large list of number.

Thanks in Advance.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are adding 2 chars which gets cast to an int. You are seeing the int value of a letter added to the int value for a comma. So 'b' + ',' = 98 + 44 = 142
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhhh, stupid me, Should have seen that one, Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic