1 / 18

Agenda

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.

dylan-brown
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASCII • char N int • Character class • char N String • Bitwise operators • homework Agenda

  2. 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

  3. 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

  4. 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

  5. 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;

  6. 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;

  7. 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?

  8. 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?

  9. 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?

  10. Character class • Provides many methods isAlphabetic() isDigit() isLowerCase() isUpperCase() return type?? isWhiteSpace() toUpperCase() toLowerCase() • How to call the method?

  11. 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?

  12. Bitwise operators • Applied to binary only • Boolean operators- && || • Bitwise operators- & | • Bitwise AND - & 0 1 1 0 0 1 0 0 1

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related