Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,

Similar presentations


Presentation on theme: "Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,"— Presentation transcript:

1 Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities

2 boolean values: true, false operations: –&&“and” –||“or” –!“not”

3 int values: 0, 1, -1, 2, -2, … maximum int: 4294967295 (=2 32 -1) minimum int: -4294967295 (=2 32 ) operations: + - * / % 5+2 = 7+: (int,int)  int 5-2 = 3-: (int,int)  int 5*2 = 10*: (int,int)  int 5/2 = 2 (quotient) /: (int,int)  int 5%2 = 1 (remainder)%: (int,int)  int

4 integral types’ representations representation used differs according to whether type is signed (byte, short, int, long) or unsigned (char): –signed integral values are represented using “two’s complement” representation –unsigned integral values are represented using “binary” representation size of representation differs according to type: –byte is 1 byte wide (1 byte = 8 bits) –short is 2 bytes wide –int is 4 bytes wide –long is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!

5 types of operators for type int Notice that all of these operators take two int arguments, and produce an int result. There is hardware circuitry to perform these operations.

6 double values: 0.0, 1.0, -3.5, 3141.5926535e-3 inexact representation (!) operations: + - * / 5.0 + 2.0 = 7.0+: (double,double)  double 5.0 – 2.0 = 3.0-: (double,double)  double 5.0 * 2.0 = 10.0*: (double,double)  double 5.0 / 2.0 = 2.5/: (double,double)  double

7 floating point types’ representation both double and float use the IEEE754 representation scheme size of representation differs according to type: –the representation of a float is 4 bytes wide –the representation of a double is 8 bytes wide main point: values of different types have different representations – you can’t “mix and match”!

8 mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation too! “Good” + “ ” + “morning!”  “Good morning!” What happens in an expression which mixes values of different types? 5 + 2.5 = ???? 5 is coerced to its equivalent double value, 5.0: 5.0 + 2.5 = 7.5 Type coercion happens only from “smaller” type to “larger” type (e.g. int  double, not double  int)

9 relational operators We can form expressions like: x < y which have a value of true or false (i.e. the type of this expression is boolean) relational operators: >= ==

10 Equality testing Equality testing: –of primitive values: == –of objects: equals method Consider: Foo a = new Foo();Foo c = a; Foo b = new Foo(); what is value ofwhat is value of (a==b)(a==c)

11 assignment = ; evaluate, assign value to Example: int x = 5; int y = x + 3; // what value is assigned to y?

12 assignment Example: int x = 5; x = x + 3; // what value is assigned to x? Notice that “=” is not “equality”, but “assignment”.

13 Some other operators ++ increments the variable it is applied to Example int x = 5; x++;// value of x is now 6 (Caution: the value of the expression x++ is perhaps not what you would expect. The value of x++ is the value of x prior to the increment of x.)

14 Control structure review if ( ) true false

15 Control structure review if ( ) else true false

16 Control structure review while ( ) true false

17 Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring to many objects? No, but we saw how to build a composite object. IBehavior cc = new CompositeBehavior(cc,br); But it’s not too easy to add/remove items from a composite. Are there options?

18 Collections interface: java.util.Collection classes: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection

19 Use of a collection HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …

20 for-each loop for (String name : names) { System.out.println(name); } This would print out: Amy Cindy Dave Emma

21 Collection.shuffle Collection.shuffle(List )


Download ppt "Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,"

Similar presentations


Ads by Google