Download Java Fundamentals: Understanding Primitive Types and Reference Types and more Assignments Computer Science in PDF only on Docsity!
CS162: Introduction to Computer
Science II
Java Fundamentals
1
Primitive Types
2
Primitive types
• Primitive types:
bytebyte , short, int, long,short int long
float, double, char, boolean
• Example:
int size = 42;
- size is a primitive variable, i.e., a variable that
t i d t l f th d l d t
3
contains a data value of the declared type.
The eight primitive types in Java
4
Result of logical operators
AND OR NOT
7
DeMorgan’s Laws
!(x && y) == !x || !y
!(x || y)y == !x && !yy
Short-circuit evaluation
int x = 3;
8
t 3;
int y = 5;
if ((x == y) && (y > 0)) ...
if ((x < y) || (y < 0)) ...
Short-circuit evaluation
int x = 3;
i tint y = 5 5;
int z = x / y;
if ((z != 0) && ((y / z) < 5))
// OK
9
if (((y / z) < 5) && (z != 0))
// crash if DivideByZeroException
// is not handled
Reference types
• All types that are not primitive are reference
or class typesor class types
• Example:
String greeting = "Howdy";
- greeting is a reference variable, i.e., a
variable that contain a reference to (address of)
th l ti f th d t
10
the memory location of the data.
Classes
• A class defines the characteristics for all objects
of its typeof its type
• A class gives a general description of
- what an object of the type is (instance data)
- what an object of the type can do (methods)
13
Objects / Classes
• An object is an instance of a class.
• A program may have many instancesA program ma ha e man instances
(objects) of one class.
• All instances of the same class have
- The same kinds of data
- The same methods
14
The same methods
An Example of a Class
etc. 15
Three instances of the Automobile
class
16
Instantiation
bobsCar “Sedan”
A memory cell that contains the memory address of the data for
19
bobsCar
Aliases
- It is possible for two variables to reference the same
object. They are aliases
- Example:Example: Automobile myCar = bobsCar;
bobsCar “Sedan”
20
myCar 0.
If an object has two (or more) aliases, the object may be referenced and/or modified through any of those aliases.
Invoking methods in a program
• Instead of calling a function to “do something
to” an object (imperative programming), tell
the object to perform one of its actions (object-
based programming).
• Examples:
bobsCar.accelerate(5); int currentSpeed = bobsCar.getSpeed();
21
int currentSpeed bobsCar.getSpeed(); if (bobsCar.getFuelLevel() < 0.1) bobsCar.decelerate(10);
Invoking methods in a program
• Valued methods return a single value, and
should be used in an expression.
- Return type of method must be assignment-– Return type of method must be assignment-
compatible in the context of the call
• void methods do not return a value, and
should be used as a single statement.
E l
22
• Examples:
bobsCar.accelerate(5); int currentSpeed = bobsCar.getSpeed(); if (bobsCar.getFuelLevel() < 0.1) bobsCar.decelerate(10);
Before calling matchSpeed
bobsCar “Sedan”
2000
suesCar “SUV” 2001
25
Call matchSpeed
bobsCar.matchSpeed(suesCar);
bobsCar “Sedan”
2000
suesCar “SUV” 2001
suesCar is associated withother
other is an alias ofsuesCar.
26
other 0. 35 9864
After executing
other.accelerate(diff);
bobsCar “Sedan”
2000
suesCar “SUV” 2001
suesCar.speed is changed
27
other 0. 55 9864
After matchSpeed terminates
bobsCar “Sedan”
2000
suesCar “SUV” 2001
other no longer exists
28
other 0. 55 9864
Before calling matchSpeed *
bobsCar “Sedan”
2000
suesCar “SUV” 2001
31
Call matchSpeed *
bobsCar.matchSpeed(suesCar);
suesCar is associated withother
bobsCar “Sedan”
2000
suesCar “SUV” 2001
other is a parameter, an alias ofsuesCar
otherCar is a local variable with the same instance values
asother
32
other 0. 35 9864
After executing the statement
other = new Automobile( … );
other refers to a new
suesCar “SUV” 2001
bobsCar “Sedan” (^) otherCar “SUV”
other refers to a new
memory location
other 0. 35 9864
33
bobsCar Sedan 2000
55 21405
otherCar SUV 2001
55 9864
After matchSpeed * terminates
suesCar “SUV” 2001
other no longer exists
otherCar and the object it
f d l i t
bobsCar “Sedan” otherCar “SUV”
other 0. 35 9864
referenced no longer exist
suesCar.speed is unchanged
Memory allocated by new is garbage
34
otherCar SUV 2001
55 9864
bobsCar Sedan 2000
55 21405
Wrapper Classes
To treat primitive types as objects, you must use
wrapper classes
Primitive Class Wrapper Class byte Byte boolean Boolean char Character double Double
37
float Float int Integer long Long short Short
Wrapper Class Example
int size = 42; Integer sizeWrapper = new Integer(size); String sizeString = sizeWrapper.toString();
Integer
38
Integer size = 42 value = 42
Wrapper Class Functions
There are lots of other functions you can call on a
wrapper class:pp
- doubleValue() – returns the value as a double
- intValue() – returns the value as an integer
To see the complete list, check out the Java 5.0 API
39
Auto-boxing
• In Java 5.0, conversion between primitive
types and the corresponding wrapper classesyp p g pp
is automatic
• This is called auto-boxing
Integer i = 42; // auto-boxing // same as Integer i = new Integer(42)
40
int x = i; // auto-unboxing // same as int x = i.intValue();