• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaring character variable  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is not a character supposed to be defined using single quotes ? char z = '1' , is not this is hoe c chould be declared ? char z=1 ; , this is declared like an int, why is it not declared in single quotes ?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because a char is also an integer variable. it can hold values from 0 up to 65,535.

if you set it like this:

char z = 1;

it will literally hold the value of 1. if you set it like this:

char z = '1';

it will hold the value that the character maps to, which in ASCII is 49.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!