i have confusion, like when we give Char var='A';how is it stored in memory???is its value dat is 65 stored in bits??if yes wat is the value of 'a' (smallcase)...
the value of A-Z is 65-90,
i have program which prints A-Z,

class chararray{

public void createArray(){

char[] s;//declaring array

s=new char[26];//assigning size

for(int i=0; i<26; i++)

{

s[i]=(char)('A' +i);

}

for(char values : s)

System.out.println(values);

}

public static void main(String[] args){

chararray c=new chararray();

    c.createArray();

}
}

i want to print a-z in program.. if i give s=(char)('a' +i); i am not getting output as a,b,c,d......y,z. can anybody explain??please

Dani AI

Generated

Short answer for (and ): Java char is an integral type that holds a UTF-16 code unit (0..65535). Lowercase a has the numeric code 97, and arithmetic like 'a' + i works because the char is promoted to int during the operation; cast back to char if you need to store the result in a char.

Common, safe ways to produce the lowercase alphabet and quick checks:

for (char c = 'a'; c <= 'z'; c++) {
    System.out.println(c);
}

System.out.println((int) 'a'); // shows the numeric code for 'a'

Debug checklist if 'a' + i did not give the expected letters:

  • Verify you used s[i] = ... not s = ....
  • Use single quotes for char literals ('a'), not double quotes ("a").
  • Confirm the cast or target type is char when assigning the sum back into a char.
  • If output looks odd, check source file/console encoding or font; the underlying numeric codes are unchanged.

Note: char is a UTF-16 code unit, not a full Unicode code point for characters beyond the Basic Multilingual Plane; use int code points and Character.toChars(...) or the codePoints() API for those cases. See Java primitive types and the Character API for details: Java primitive types and Character (Java API).

Recommended Answers

All 2 Replies

char is an integer data type, the representation as a character is for convenience's sake only.

i know characters are represented interms of integer values. can u tel me wat is da value of 'a'. and the program i ve given prints A to Z, since A 's value is 65 i ve given ('A' + i) wer
the logic i thought is A 's value is increments so prints A,B,C,D....Z. if i give ('a' +i).. y i am not getting alphabets in small case (a,b,c,d........to z.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.