© 2006 pearson addison-wesley. all rights reserved 6.4.1 char and string char is for storing single...

9
© 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 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.

Upload: cecily-mcdaniel

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.1

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.

Page 2: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.2

Examplesvariable 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 );

Page 3: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.3

Class Diagram

String

«constructor» + String( String ) . . .

«query» + char charAt( int )

+ int length() + int substring(int) + int substring(int, int)

. . .

«translate» + String toLowerCase( ) + String toUpperCase( ) . . .

«infix operator» +

String

«constructor» + String( String ) . . .

«query» + char charAt( int )

+ int length() + int substring(int) + int substring(int, int)

. . .

«translate» + String toLowerCase( ) + String toUpperCase( ) . . .

«infix operator» +

Page 4: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.4

Examplesvariable 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’ );

Page 5: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.5

Differences Between Types

Strings and primitive types are largely incompatible...

• You can add, subtract, multiply or divide most primitives, but not Strings

• Because they are a reference type, Strings are instantiated via new, but not primitives.

String product = "75.2" * "3";

char letter = new char('D');

• Even if a String stores a single character it is incompatible with char.

char questionMark = "?";String asterisk = '*';

• You easily extract individual characters from Strings, but not primitives char decimalPoint = ("38.25").charAt(2);

char nonSymbol = (38.25).charAt(2);

Page 6: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.6

Type Conversion Expressions

String charString char

use charAt

char initial = ("7").charAt(0);

1) Instantiate Scanner passing the String to be converted 2) Call nextType() upon this Scanner object.

String closeToPi = "3.14159";Scanner converter = new Scanner(closeToPi);double approximationOfPi = converter.nextDouble();

concatenate to the empty string String realValue = 41.75 + "";

String logicalNegative = false + "";

Any primitive StringAny primitive String

String Any primitive excepting charString Any primitive excepting char

which can be abbreviated...double approximationOfPi =

Page 7: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.7

Enumerated Types

An enumerated type is a user-defined type with its own constants.

The constants are identifiers.

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

enum FlippingCoin {heads, tails}. . .

flipper = FlippingCoin.tails;

int flipNum = flipper.ordinal();String flipName = flipper.name();// assert:

FlippingCoin flipper;flipper = FlippingCoin.tails;

Page 8: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.8

Internal or External Declaration?

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 enum Planets { mercury, venus, earth, mars,

jupiter, saturn, uranus, neptune, pluto } private Planets favoritePlanet;

public Driver() { . . . favoritePlanet = earth; . . . }}

public class Driver { private Planets favoritePlanet;

public Driver() { . . . favoritePlanet = Planets.earth; . . . }}

public class Driver { private Planets favoritePlanet;

public Driver() { . . . favoritePlanet = Planets.earth; . . . }}

Enumerated types may eitherbe declared within a class or in a separate file ...but notlocal to a method.

public enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto}

public enum Planets { mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto}

Page 9: © 2006 Pearson Addison-Wesley. All rights reserved 6.4.1 char and String char is for storing single characters primitive type constants: a printable character

© 2006 Pearson Addison-Wesley. All rights reserved 6.4.9

Q: Why use enumerated types?

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

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

Using enumerated... Using int...

A: SafetyfavoritePlanet = -33; is permitted using the int version.