-13

Why when I run this it gives zero instead of the character a:

public class Main {

  public static void main(String[] args) {
    int num = 79;
    char a = 'a';

    System.out.println((char)num);
  }
}
ChristophS
  • 598
  • 4
  • 23

3 Answers3

8

It isn't a zero, it's an uppercase O, which 79 is the ASCII code for.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
-1

You have an typo:

To get the lowercase letter 'a', use 97 instead of 79.

ChristophS
  • 598
  • 4
  • 23
-1

When you cast a number to a character it will look the ASCII value of that particular number which in your case is O (CAPITAL O).

If you want to print a use 97

See the ASCII chart for further reference: https://ascii.cl/

Sundeep
  • 452
  • 2
  • 12