cs102 data types in java cs 102 java’s central casting

22
CS102 Data Types in Java CS 102 Java’s Central Casting

Post on 20-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Data Types in Java

CS 102

Java’s Central Casting

Page 2: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Are You Java’s Type?

• Type: a set of values that are semantically similar

• Java is a strongly typed language– Every variable and every expression has a

type that is known at compile time.

– Strong typing helps detect errors at compile time.

Page 3: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

What’s the Role of Types?

• Types limit the:

– Values that a variable can hold or that an expression can produce

– Limit the operations supported on those values

– Determine the meaning of the operations.

Page 4: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Java Types

• Primitive types– boolean

– numeric •Integral: byte, short, int, long, and char

•Floating-point: float and double

• Variables of primitive types hold the actual value

Page 5: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Inside a Primitive Type• Actual values for integral types:

byte: -128 to 127

short: -32768 to 32767

int: -2147483648 to 2147483647

long: -9223372036854775808 to 9223372036854775807

char: from '\u0000' to '\uffff’ (from 0 to 65535)

• Why use int instead of long?

Page 6: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Boolean Type

• boolean type represents a logical quantity with two possible values, indicated by the literals true and false

Page 7: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Building a Boolean from a Number• Can’t say (why not?):if (x) System.out.println(“Congratulations, it’s a Boole!”);

• Convert an integer x (following the C language convention that any nonzero value is true):

if (x != 0) System.out.println(“Congratulations, it’s a Boole!”);

Page 8: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Building a Boolean from an Object

• Object reference obj can be converted (any reference other than null is true): obj! = null

Page 9: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

The Other Kind of Type

• Reference types– Variables of reference types don’t hold

values, but references to values– Classes, interfaces and arrays are all

reference types

Page 10: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

A Graphical View

0010010

1110010

int counter

Airport midway

The data of the midway object

Page 11: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Classes, Interfaces and Arrays, oh my!

• Classes we’ve already seen

• Interfaces are programming contracts– An interface is a set of constants and

methods– In Java, a class implements an interface

Page 12: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Implementing an Interface• An example from the Comparison

applet:public class Comparison extends Applet implements ActionListener

• Comparison promises to do everything an ActionListener does

Page 13: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Comparison Does What It Has To

• What does an ActionListener have to do?actionPerformed public abstract void actionPerformed(ActionEvent e)

• Provides an implementation for the interface:public void actionPerformed(ActionEvent e) { number1 = Integer.parseInt(input1.getText() );

number2 = Integer.parseInt(input2.getText() );

repaint();

}

Page 14: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Array Types• If T is a primitive type, then a variable of type

"array of T"can hold:

– Null reference

– Reference to any array of type "array of T"

• If T is a reference type, then a variable of type "array of T" can hold:

– Null reference

– Reference to any array of type "array of S" such that type S is assignable to type T

Page 15: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Object is the Root of All Java

• Variable of type Object can hold:– Null reference

– Reference to any object, whether class instance or array.

Page 16: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Class Type

• Variables have types

• Objects (and arrays) don’t have a type, but belong to a class

• Usually we’ll consider them the same

Page 17: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Casting Against Type• A value could be two different types

– Is 12 an int or a float?

• Compiler isn’t smart, so it’s conservative (signals an error)

• Override the compiler with a cast– Cast says: Treat this variable as the type I

say– To cast in Java, write:

(newType) variable

Page 18: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Examples of casting

// Casting a float literal to a type int.

// Without the cast operator, this would be a

// compile-time error, because it’s a narrowing

// conversion:

int i = (int)12.5f;

// From class average applet (Figure 2.9)

if ( counter != 0 ) {

average = (double) total / counter;

System.out.println( "Class average is " + average );

} else ...

Page 19: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Can’t Always Cast

• Can’t do this:if ((boolean) x) System.out.println(“Congratulations, it’s a Boole!”);

• Sometimes casts are automatic, and are called conversions

Page 20: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

One of Two Ways

• Create an expression in a context where the type of the expression is not appropriate and either:– Error at compile time (if statement has

any type other than boolean)– May be able to accept a type that is related

to the type of the expression

Page 21: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Automatic Conversion• For convenience, Java performs an

implicit conversion– From the type of the expression to a type

acceptable for its surrounding context

• Kinds of conversions:– Identity, Widening primitive, Narrowing

primitive, Widening reference, Narrowing reference, String

Page 22: CS102 Data Types in Java CS 102 Java’s Central Casting

CS102

Coming Attractions

• Arrays: Grouping related values together