Presentation is loading. Please wait.

Presentation is loading. Please wait.

char is for storing single characters

Similar presentations


Presentation on theme: "char is for storing single characters"— Presentation transcript:

1 char is for storing single characters
char and String char is for storing single characters primitive type constants: a printable character enclosed in single quotes ( ‘Y’ ‘y’ ‘#’ ‘7’ ) char widens to int, long, float or double operators: ++ and -- caution: (char)7 is not the same as ‘7’ String is for storing a sequence of characters built-in Java class constants: zero or more characters enclosed in double quotes a String object is ______________ operators: + caution: new not used with constant notation. © 2006 Pearson Addison-Wesley. All rights reserved

2 Examples variable declarations private char initial, digit;
private String name, message, str; initial = ‘D’; digit = ‘0’; digit++; initial = (char)(initial + 3); System.out.println(digit); System.out.println(initial); name = “Riley”; str = “”; message = name + “ assigns programs.”; System.out.println( initial + “. “ + message ); System.out.println( “digit value is “ + digit ); © 2006 Pearson Addison-Wesley. All rights reserved

3 Class Diagram String «constructor» «query» «translate»
+ String( String ) . . . «query» + char charAt( int ) + int length() + int substring(int) + int substring(int, int) «translate» + String toLowerCase( ) + String toUpperCase( ) «infix operator» + © 2006 Pearson Addison-Wesley. All rights reserved

4 Examples variable declarations private String cheese, theory;
cheese = “limburger”; System.out.println( cheese.substring(3) ); cheese = cheese.substring(6); SYstem.out.println( cheese ); cheese = “gouda”.substring(2); System.out.println( cheese ); theory = “quantum mechanics”; System.out.println( theory.substring(4,10)); System.out.println( theory ); theory = “hadrons”; System.out.println( cheese.substring(3,5) + “ “ + theory.charAt(0) + cheese.substring(4) + ‘t’ ); Output: burger …. ger … uda … tum me … quantum mechanics … da hat © 2006 Pearson Addison-Wesley. All rights reserved

5 Differences Between Types
Strings and primitive types are largely incompatible... • You can add, subtract, multiply or divide most primitives, but not Strings String product = "75.2" * "3"; • You easily extract individual characters from Strings, but not primitives char decimalPoint = ("38.25").charAt(2); char nonSymbol = (38.25).charAt(2); • Because they are a reference type, Strings are instantiated via new, but not primitives. char letter = new char('D'); • Even if a String stores a single character it is incompatible with char. char questionMark = "?"; String asterisk = '*'; © 2006 Pearson Addison-Wesley. All rights reserved

6 Type Conversion Expressions
Any primitive  String concatenate to the empty string String realValue = ""; String logicalNegative = false + ""; String  char use charAt char initial = ("7").charAt(0); String  Any primitive excepting char 1) Instantiate Scanner passing the String to be converted 2) Call nextType() upon this Scanner object. Note that the Scanner abbreviation line replaces the last two lines and eliminates the need for a Scanner variable. The caution means that the String used to create the Scanner must represent a valid Double constant. (This is true for any of the nextType() methods.) String closeToPi = " "; Scanner converter = new Scanner(closeToPi); double approximationOfPi = converter.nextDouble(); which can be abbreviated... double approximationOfPi = © 2006 Pearson Addison-Wesley. All rights reserved

7 Enumerated Types An enumerated type is a user-defined type with its own constants. The constants are identifiers. enum FlippingCoin {heads, tails} . . . FlippingCoin flipper; flipper = FlippingCoin.tails; Every enumerated type includes two conversion methods: ordinal() returns an int (0 for first, 1 for second, and so forth) name() returns an String from the identifier flipper = FlippingCoin.tails; int flipNum = flipper.ordinal(); String flipName = flipper.name(); // assert: © 2006 Pearson Addison-Wesley. All rights reserved

8 Internal or External Declaration?
Enumerated types may either be declared within a class or in a separate file ...but not local to a method. public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() { . . . favoritePlanet = earth; } public enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } public class Driver { private Planets favoritePlanet; public Driver() { . . . favoritePlanet = Planets.earth; } © 2006 Pearson Addison-Wesley. All rights reserved

9 Q: Why use enumerated types?
A: Safety favoritePlanet = -33; is permitted using the int version. Using enumerated... Using int... public class Driver { private enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet; public Driver() { . . . favoritePlanet = earth; } public class Driver { private int favoritePlanet; private final int mercury = 0; private final int venus = 1; private final int earth = 2; private final int mars = 3; private final int jupiter = 4; private final int saturn = 5; private final int uranus = 6; private final int pluto = 7; public Driver() { . . . favoritePlanet = earth; } © 2006 Pearson Addison-Wesley. All rights reserved


Download ppt "char is for storing single characters"

Similar presentations


Ads by Google