characters
play

Characters } The Java char primitive type } Represents a single - PowerPoint PPT Presentation

Characters } The Java char primitive type } Represents a single character, and is given values using single quotes : char ch1 = 'a'; char ch2 = '8'; char ch3 = '#'; } Has increment/decrement operators, just like an integer: char ch = 'a'; Class


  1. Characters } The Java char primitive type } Represents a single character, and is given values using single quotes : char ch1 = 'a'; char ch2 = '8'; char ch3 = '#'; } Has increment/decrement operators, just like an integer: char ch = 'a'; Class #19: Characters, Strings, ch++; and Elementary Debugging System.out.println( ch ); // gives output: b ch = '8'; Software Design I (CS 120): D. Mathias ch—-; System.out.println( ch ); // gives output: 7 2 Software Design I (CS 120) Converting between char and int Converting between char and int } The char type is stored using 16 bits (2 bytes) of memory } One actually handy use of char arithmetic and int } Will automatically widen to any larger numerical types ( int, conversion is when we want to compute the n -th letter in the long, float, double ) alphabet sequence (which comes in handy surprisingly often!) } Can be cast down to generate a char from an int , etc. char letA = 'a'; } Note : casting up & down with char can be surprising until you learn the basic character/number equivalencies! char let13 = (char) ( letA + 12 ); char ch = '2'; int i = ch; System.out.println( i ); // output: 50 [A] We have mixed- [C] Finally, do [B] Now do cast step, back mode arithmetic , so assignment . to character that is 12 ch = (char) i; we will widen all positions higher than ‘a’. When the code values to largest System.out.println( ch ); // output: '2' Note : this is only done type ( int ). is complete, i = 99; after the math due to the value of This takes position ch = (char) 99; let13 is now parentheses on the of ‘a’ (97) and adds the char ' m '. arithmetic. System.out.println( ch ); // output: c 12 to it (109). 3 4 Software Design I (CS 120) Software Design I (CS 120) 1

  2. Converting between char and int Printing & Storing Longer Sequences } The Java String class char letter13 = (char) ( letA + 12 ); } A special “basic” class that is fundamental to the language } The reason something like this works is that certain parts of the } Has some behavior that is similar to primitive types character set Java uses are encoded in sequential order : } Can be specified as a sequence of characters in double quotes a b ... y z char char bunch of stuff bunch more stuff } Use of new and constructor is optional and not required pos pos- 0 1 ... 96 97 98 ... 121 122 123 ... String s = "Hello"; ition ition String s2 = new String( "Hello There!" ); } We can use this fact, plus the auto-coercion of the smaller type String s3 = "Hello There!\n"; ( char ) to the larger type ( int ), to “slide” up and down in the order } Basic concatenation ( + ) operation: takes any values, converts } This is a much better idea than trying to remember these numbers ourselves (or looking them up every time we need them)! to String objects if needed, and chains them together: } Same trick works for upper-case letters A–Z , and digits 0–9 String s = "Hello" + "There!"; // "HelloThere!" char capital26 = (char) ( 'A' + 25 ); // 'Z' String s2 = "Hello" + " " + "There!"; // "Hello There!" char digit4 = (char) ( '0' + 3 ); // '3' System.out.println( "x = " + 100 ); // "x = 100" 5 6 Software Design I (CS 120) Software Design I (CS 120) Strings are Not Primitive Types Converting Other Types to String } String is a reference type (class) in Java } Basic concatenation ( + ) operation can force the conversion of } There is no direct conversion between String and any primitive value other values to a String : String s = "3"; This is OK . int number = 3; String s2 = 3; String s1 = "Number is " + number; // ”Number is 3" These are all compile errors , String s3 = '3'; String s2 = "" + number; // "3" even with char type! int number = "3"; } Basic concatenation ( + ) operation can force the conversion of a } Be careful: where you place quotation marks matters! primitive to a String : } Anything in quotes is reproduced exactly as typed int number = 3; int number = 3; String s1 = "Number is " + number; // ”Number is 3" String s1 = "" + number; // "3" String s2 = "" + number; // "3" String s2 = "" + "number"; // "number" This is the empty String. It is a String, but has no characters in it. 7 8 Software Design I (CS 120) Software Design I (CS 120) 2

  3. Strings are Immutable Methods in the String Class } Strings are objects with many methods } In Java, once created, a String does not change String } Thus code like this will run fine, but will not change the Outputs character at input position <<constructor>> first char at : 0 content of the String in any way last char at : length() – 1 String( String ) String name = ”David"; <<query>> Outputs length of String char charAt( int ) name.toUpperCase(); int length() Outputs sub-part of String, System.out.println( name ); // David String substring( int ) starting from input position, String substring( int, int ) going all the way to end } If we want to change the String , we must replace it <<translate>> Outputs subpart of String, starting with a new one, using an assignment : String toLowerCase() from first input position, String toUpperCase() going to second - 1 String name = ”David"; name = name.toUpperCase(); Output lower/upper case version Full list online at: System.out.println( name ); // DAVID (Note: do not change original) http://docs.oracle.com/javase/8/docs/api/java/lang/String.html 9 10 Software Design I (CS 120) Software Design I (CS 120) String Displaying Text with System.out.println Methods in the String Class <<constructor>> String( String ) } Debugging programs often requires knowing what the values <<query>> char charAt( int ) of some variables are during run-time int length() String first, last; String substring( int ) } Explains performance issues (e.g., “Why is the window in the wrong first = "Ronald"; String substring( int, int ) place when I draw it in my program?”) last = "McDonald”; <<translate>> } Can be complex and error-prone to calculate by hand String toLowerCase() System.out.print( first.charAt( 1 ) ); // o String toUpperCase() } One possibility: have the program print out the values as it System.out.print( last.length() ); // 8 runs, so you can see what it is doing System.out.print( last.substring( 2 ) ); // Donald System.out.print( first.substring( 0, 3 ) ); // Ron } One simple way to do this is with a “print” or “print-line” command (these each do the same thing, but the second one first = first.toLowerCase(); adds a line-break at the end of what it prints out): last = last.toUpperCase(); System.out.print( “Hello” ); System.out.println( first + " " + last ); // ronald MCDONALD System.out.println( “Hello” ); 11 12 Software Design I (CS 120) Software Design I (CS 120) 3

  4. Using System.out.println() Elementary Debugging with println() } This “print-line” method writes characters to the default } When you run into difficulties in a program, it is useful to output location (usually a terminal screen) know the values of some of your variables } In Eclipse, text will usually appear at the bottom of the editing area, but in other contexts, when the program runs, the text may appear } If you are having trouble determining why a graphical somewhere else entirely object is not where you want it, for example, you might } It will automatically convert most primitive types to a print out its location variables after some calculation: printable representation of their value: System.out.println( 100 ); System.out.println( 38.7 ); int windowWidth = 500; int windowHeight = 400; win.setSize( windowWidth, windowHeight ); } The command also has many uses in writing simple programs that give output back to the user: int ovalSize = 100; int ovalLoc = windowWidth / 2 - ovalSize; System.out.println( 3 * 4 + 5 / 6 – 3 * 2 ); System.out.println( ovalLoc ); 13 14 Software Design I (CS 120) Software Design I (CS 120) Code Examples: Debugging In class, we’ll debug some actual Java code examples… 15 Software Design I (CS 120) 4

Recommend


More recommend