180 likes | 315 Views
ASCII char N int Character class char N String Bitwise operators homework. Agenda. ASCII codes. ASCII is an acronym for American Standard Cod for Information Interchange All characters type are stored as numbers.
E N D
ASCII • char N int • Character class • char N String • Bitwise operators • homework Agenda
ASCII codes • ASCII is an acronym for American Standard Cod for Information Interchange • All characters type are stored as numbers. • Characters like 0…9, all upper/lower case letters, special symbols like <,>,{,}, _, -, ?, : ; ) • Refer to BPJ Appendix D for more information
Why do we need ASCII code? • Earlier we had EBSIDIC(Extended Binary Code Decimal Interchange Code) codes that was capable of accommodating English Letters, Punctuations and Digits. • Then came the need of accommodating European alphabets and mathematical signs, box drawing characters ... so ASCII was formed which could accommodate 256 codes. • Now Unicode is introduced so that we can accommodate the alphabets of all the languages in the world and additional signs and symbols
What is Unicode? • Programs are written using the Unicode character set. • The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding. • Except for comments, identifiers, and the contents of character and string literals, all input elements in a program are formed only from ASCII characters
How to convert a character to ascii? char aChar = ‘A’; int asciiCode = (int)aChar; How to convert an ascii to a character? int aNum = 65; char aChar= (char)aNum;
illegal statements Charater type char and String types can’t be stored into each other. char ch = “A”; String Str = ‘A’; or String str = “B”; char ch=str;
Surprising legal statements • int and char can be added and store into int variable int x = 10; char y = ‘a’; int z = x + y; What z will be?
Is this legal? int aInt = 10; char aChar = ‘a’; aInt = aChar; => ok? aInt = ‘a’; => ok? aInt = 10 + aChar; => ok? int aInt = 10; char aChar = aInt; => ok? aChar=(int)aInt; char aChar = 10; => ok?
You can do this • Look at ASCII chart, guess what will be the output for the following statement. char aChar = ‘A’; //65 int num = aChar + 10; //75 char bChar = (char)num; what is bChar?
Character class • Provides many methods isAlphabetic() isDigit() isLowerCase() isUpperCase() return type?? isWhiteSpace() toUpperCase() toLowerCase() • How to call the method?
char N String char aChar = ‘A’ ; String aStr = “B” ; aChar = aStr; => ok? aStr = aChar; => ok? aStr = aChar + “ “ ; => ok • Conversion from char to string. • Conversion from string to char aChar = aStr.CharAt(0); aChar = aStr.CharAt(1); => ok?
Bitwise operators • Applied to binary only • Boolean operators- && || • Bitwise operators- & | • Bitwise AND - & 0 1 1 0 0 1 0 0 1
Bitwise OR : | 0 1 1 0 0 1 0 1 1 • Bitwise exclusive OR: (XOR) ^ 0 1 1 0 0 1 0 1 0 • Short circuit evaluation: the left operand is evaluated first. If the result of the entire expression can be determined by the value of the left operand, then no other operands will be evaluated
Bitwise NOT- ~ 0 => 1 1 => 0 • AND (9010 & 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 0 • OR (9010 | 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1
XOR ^ (9010 ^ 10710 ) 9010 0 1 0 1 1 0 1 0 10710 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 1 • NOT ~ • 00000000 00000000 00000000 00101110 11111111 11111111 11111111 11010001 most significant bit(msb) 0 – positive 1 - negative
Bitwise operations • Decimal shifting to the right 283.0 -> 28.3 dividing by 10 • Decimal shifting to the left 283.0 -> 2830 multiplying by 10 • Binary shifting to the right: >> 32 >> 3 shift the bits(representing number 7) to the right 3 times 2*2*2=8 32/8 = 4 • Binary shifting to the left: << 7 << 2 shift the bits to the left 2 times
A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition
Homework • Preview BPJ 31, not 27 as specified in lesson plan; Barron’s p166-169 • Do all the exercises on BPJ 13 • Convert a String to an array of characters. • Hard code the text to a variable: “Programs are written using the Unicode character set. The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding.” • Declared an char array with the size of the text. • Using a for loop and charAt(..) to store each symbol(letter, space, period..) into the char array • Use enhanced for loop to print out the char array