Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110 M ORE T YPES Instructor: Jason Carter. 2 P RIMITIVE T YPES int, double, boolean, long, short, float, byte char.

Similar presentations


Presentation on theme: "C OMP 110 M ORE T YPES Instructor: Jason Carter. 2 P RIMITIVE T YPES int, double, boolean, long, short, float, byte char."— Presentation transcript:

1 C OMP 110 M ORE T YPES Instructor: Jason Carter

2 2 P RIMITIVE T YPES int, double, boolean, long, short, float, byte char

3 3 P RIMITIVE T YPES Constants (Literals & Named Constants) Operations with Invocation Syntax

4 4 CHAR C ONSTANTS char {letters, digits, operations, … } 16 bits ‘a’ ‘A’ ‘1’ ‘<’ ‘ ’ ‘’’ ‘ \’’ ‘\n’ ‘ ‘\’ ‘\\’ Escape Sequencenewline character

5 5 U SEFUL E SCAPE S EQUENCES Escape SequenceCharacter Denoted \’’ \nnew line \bback space \\\ \ttab \””

6 6 O RDERING C HARACTERS ‘’…‘a’… Ordinal number(integer code) Position in ordered character list

7 7 O RDERING C HARACTERS ‘’…‘a’… ‘’…‘A’… ‘’…‘0’… ‘b’ ‘B’ ‘1’ ‘c’ ‘C’ ‘2’ ‘a’ > ‘b’ ‘B’ > ‘A’ ‘4’ > ‘0’ ‘0’ > ‘’ false true ‘a’ > ‘A’??? ‘a’ > ‘0’???

8 8 C ONVERTING B ETWEEN C HARACTERS AND T HEIR O RDINAL N UMBERS ( int ) ‘a’Ordinal number of ‘a’ ( char ) 55 Character whose ordinal number is 55 ( int ) ‘’0 ( char ) 0‘’ ( int ) ‘d’??? ( char ) 1??? ( char ) -1 ( int ) ‘c’ – ( int ) ‘a’2 ‘c’ – ‘a’2 Implicit conversion to wider type ( char ) (‘c’ - 2)‘a’ ( char ) (‘A’ + 2)‘C’ (char) (‘C’ - ‘A’ + ‘a’)‘c’

9 9 A U SEFUL C HARACTER O PERATION Character.isLetter(c) true if c is a letter Character.isLetter(‘c’) true Character.isLetter(‘A’) true Character.isLetter(‘1’) false Character.isLetter(‘ ’) false

10 10 S TRING C ONSTANTS String {sequences of characters} Variable size “hello” “123” “hello 123” “a” ‘a’ “” “hello\n\n123” “\\” Object Type “\”

11 11 A CCESSING S TRING C OMPONENTS String s = “hello world”; s.getFirstChar() s.getSecondChar() s.charAt(0)‘h’ s.charAt(1)‘e’ s.charAt(-1) s.charAt(11) StringIndexBounds Excepion s.length()11 “ ”.length()1 0 Index

12 12 A CCESSING S UBSTRING public String substring ( int beginIndex, int endIndex) s.substring(beginIndex, endIndex)  s.charAt(beginIndex).. s.charAt(endIndex-1) “hello world”.substring(4,7)“o w” “hello world”.substring(4,4)“” “hello world”.substring(7,4) StringIndexBounds Exception

13 13 C HANGING S TRINGS ? Stings are read-only (immutable) “hello” + “world”“hello world” Three different instances

14 14 U SEFUL S TRING O PERATIONS s.toLowerCase()copy of s with letters converted to lower case s.toUpperCase()copy of s with letters converted to upper case “Hello World”.toLowerCase()“hello world” “Hello World”.toUpperCase()“HELLO WORLD”

15 15 C LASSIFYING A P ERSON BY R ACE African Indian American Indian Asian Native Hawaiian White Some Other Race Data type to store a value capturing the race of a person?

16 16 C LASSIFYING A P ERSON BY R ACE int race = AFRICAN_AMERICAN; public int getRace() { return race; } public void setRace ( int newVal) { race = newVal; }

17 17 C LASSIFYING A P ERSON BY R ACE public static int AFRICAN_AMERICAN = 0; public static int AMERICAN_INDIAN = 1; public static int ASIAN = 2; public static int NATIVE_HAWAIIAN = 4; public static int WHITE = 4; public static int SOME_OTHER_RACE = 5;

18 18 E RRORS P OSSIBLE public static int AFRICAN_AMERICAN = 0; public static int AMERICAN_INDIAN = 1; public static int ASIAN = 2; public static int NATIVE_HAWAIIAN = 4; public static int WHITE = 4; public static int SOME_OTHER_RACE = 5;

19 19 E RRORS P OSSIBLE Programming environment does not know the relationship among constants!

20 20 C LASSIFYING A P ERSON BY R ACE String race = AFRICAN_AMERICAN; public int getRace() { return race; } public void setRace ( int newVal) { race = newVal; }

21 21 C LASSIFYING A P ERSON BY R ACE public static String AFRICAN_AMERICAN = "African American"; public static String AMERICAN_INDIAN = "American Indian"; public static String ASIAN = "Asian"; public static String NATIVE_HAWAIIAN = "Native Hawaiian"; public static String WHITE = "White"; public static String SOME_OTHER_RACE = "Some Other Race";

22 22 E RRORS P OSSIBLE User or programmer can make mistake! String type is space inefficient!

23 23 E RRORS P OSSIBLE User or programmer can make mistake!

24 24 D ECLARING AN ENUM Like class or an interface, an enum is declared in its own file

25 25 D ISPLAYING AN E NUM


Download ppt "C OMP 110 M ORE T YPES Instructor: Jason Carter. 2 P RIMITIVE T YPES int, double, boolean, long, short, float, byte char."

Similar presentations


Ads by Google