Data Types in Javas Central Casting CS 102

  • Slides: 20
Download presentation
Data Types in Java’s Central Casting CS 102 -02 Lecture 1 -2 April 13,

Data Types in Java’s Central Casting CS 102 -02 Lecture 1 -2 April 13, 1998 CS 102 -02 Lecture 3 -1

Are You Java’s Type? • Type: a set of values that are semantically similar

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. April 13, 1998 CS 102 -02 Lecture 3 -1

What’s the Role of Types? • Types limit the: – Values that a variable

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. April 13, 1998 CS 102 -02 Lecture 3 -1

Java Types • Primitive types – boolean – numeric • Integral: byte, short, int,

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 April 13, 1998 CS 102 -02 Lecture 3 -1

Inside a Primitive Type • Actual values for integral types: byte: -128 to 127

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 'u 0000' to 'uffff’ (from 0 to 65535) • Why use int instead of long? April 13, 1998 CS 102 -02 Lecture 3 -1

Boolean Type • boolean type represents a logical quantity with two possible values, indicated

Boolean Type • boolean type represents a logical quantity with two possible values, indicated by the literals true and false April 13, 1998 CS 102 -02 Lecture 3 -1

Building a Boolean from a Number • Can’t say (why not? ): if (x)

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!”); April 13, 1998 CS 102 -02 Lecture 3 -1

Building a Boolean from an Object • Object reference obj can be converted (any

Building a Boolean from an Object • Object reference obj can be converted (any reference other than null is true): obj! = null April 13, 1998 CS 102 -02 Lecture 3 -1

The Other Kind of Type • Reference types – Variables of reference types don’t

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 April 13, 1998 CS 102 -02 Lecture 3 -1

A Graphical View int counter Airport midway 0010010 1110010 The data of the midway

A Graphical View int counter Airport midway 0010010 1110010 The data of the midway object April 13, 1998 CS 102 -02 Lecture 3 -1

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

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 April 13, 1998 CS 102 -02 Lecture 3 -1

Object is the Root of All Java • Variable of type Object can hold:

Object is the Root of All Java • Variable of type Object can hold: – Null reference – Reference to any object, whether class instance or array. April 13, 1998 CS 102 -02 Lecture 3 -1

Class Type • Variables have types • Objects (and arrays) don’t have a type,

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 April 13, 1998 CS 102 -02 Lecture 3 -1

Casting Against Type • A value could be two different types – Is 12

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: (new. Type) variable April 13, 1998 CS 102 -02 Lecture 3 -1

Examples of casting average = (double) total / counter; April 13, 1998 CS 102

Examples of casting average = (double) total / counter; April 13, 1998 CS 102 -02 Lecture 3 -1

Can’t Always Cast • Can’t do this: if ((boolean) x) System. out. println(“Congratulations, it’s

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 April 13, 1998 CS 102 -02 Lecture 3 -1

One of Two Ways • Create an expression in a context where the type

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 April 13, 1998 CS 102 -02 Lecture 3 -1

Automatic Conversion • For convenience, Java performs an implicit conversion – From the type

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 April 13, 1998 CS 102 -02 Lecture 3 -1

Funky Conversions • What does this print? class Test { public static void main(String[]

Funky Conversions • What does this print? class Test { public static void main(String[] args) { int big = 1234567890; float approx = big; System. out. println(big -(int)approx); } } April 13, 1998 CS 102 -02 Lecture 3 -1

Coming Attractions • Arrays: Grouping related values together April 13, 1998 CS 102 -02

Coming Attractions • Arrays: Grouping related values together April 13, 1998 CS 102 -02 Lecture 3 -1