//CharTest.java /* char type. Program can input, store, process and output (individual) characters. char constants denoted by single char within pair of single quotes: 'a' //lowercase letters 'W' //uppercase letters '$' //punctuation characters '9' //digit characters '8' is not 8 is not "8" is not 8.0 ' ' //blank (or space) character '\n' //newline escape sequence '\t' //tab '\\' //backslash 'ab' is a syntax error. "ab" is a string. 'a' is different than and incompatible with "a" char String chars are 2-byte Unicode characters (65000 of them, including 20000 Chinese/Kanji and 10000 Hangul, plus all the alphabets: Greek, Cyrillic, Arabic, Hebrew, Thai, Hindi, etc Unlike a String, can switch on a char and use == != < <= etc. */ import java.awt.*; import javax.swing.*; public class CharTest extends JApplet { public static void main(String[] argv) { //public void init() { char c; //variable of type char String input; c = '$'; //assign it a char value JOptionPane.showMessageDialog(null, "It's now " + c); if (c != 'A') //can use == != < etc with chars JOptionPane.showMessageDialog(null, "It's not an A"); input = JOptionPane.showInputDialog( "Enter a character" ); //String is like an array. elements are chars. c = input.charAt(0); //first character of String JOptionPane.showMessageDialog(null, "Thanks for " + c); //syntax errors: //c = 'ab'; //can only be one char in the single quotes. //c = "ab"; //can't assign a String to a char. //c = "a"; //it's a String. can't assign a String to a char. String s; //s = 'W'; //can't assign a char to a String char c1, c2; c1 = 'X'; c2 = c1; if (c1 == c2) JOptionPane.showMessageDialog(null, "They equal"); switch (c1) { //switch on a char case 'w': JOptionPane.showMessageDialog(null, "this won't happen"); break; case 'X': JOptionPane.showMessageDialog(null, "switching on a char X"); break; } //real world data that can be char: char maritalStatus, sex, grade, middleInitial, yesNo, trueFalse; } }