CIS 26
Object-Oriented Programming
Lecture 02 - The Java Language
- A lot like C/C++
- A lot NOT like C/C++
Character set
- Unicode character set (16 bits wide)
- locale -- accomodates customization for culture, language, country
- Identifiers are also wrttten in Unicode
Primitive Types and Their Literals
- boolean
- Literals:
true
, false
- NOT compatible with integer types
- Eliminates good ol' 'assignment instead of equality' mistake in C/C++
- On the other hand, prevents certain 'standard' C idioms
- integers - byte (8), short (16), int (32), long (64)
- Literals:
123
(decimal), 0475
(octal), 0x7D5C
hexadecimal
- Cross-platform executables requires standardized integer lengths
- Executing on the JVM (virtual machine) makes this a moot point
- No unsigned types
- Default integer literal type is int;
L
can be used as a suffix to force long
- character - char
- Literals: 'a', '\u00E6', '\n'
- floating point - float (64), double (128)
- Literals:
0.0
, .53
, 1e99
- Default floating literal type is double;
F
can be used as a suffix to force float
String Literals
- String class possesses a literal, even though not primitive type
- Sequence of characters in doublke quotes: "Hello", "\u5469 \u5561"
Operators and Expressions
- Similar to C/C++
- Numerics include integer and floating point
- Inreger operators appply to
char
as well
- As in C, integer arithmetic wraps around (rather than under/over-flowing)
- String concatenation
"Dear " + name + ": "
- Shouldn't really be there (no operator overloading in Java)
- Ubiquitous and useful, so was add
- If one of the operands is non-string, it is converted to String (more on how later)
Variables and Types
- Declaration syntax similar to C/C++
- A type specifies the legal values a variable (of that type) may contain
- primitive and reference types
- variable of reference type hold a reference to an object
- (shhh-- if you promise not to tell anyone, I'll let you think of a reference as a pointer)
- Default initial values
- Basically the value when stuffed with 0's
- Doesn't happen with locals
Type Compatibility and Conversion
- Types are compatible if each can appear in contexts that the other may appear
- Conversion involves transforming a value of one type to a value of another type
- In a widening conversion, the value is converted to one of a larger range (int -> long, int->double)
- Safe, so is done implicitly when required
- In a narrowing conversion, the value is converted to one of a smaller range (double -> float, float->long)
- Unsafe (why?), so explixit cast is required
Reference Types
- class, interface, array type
- reference variable is a variable of reference type
- contains a reference to object (of that type)
- may also contain
null
-- indication no object is being referenced
- References are NOT pointers (so forget what I said before!)
- In C/C++, pointers are recognized for what they are ((32-bit) integers voewed as addresses)
- Pointers can be cast to other pointer types
- Pointers can be manipulated via pointer arithmetic
- Pointers can be used to work with dynamically-allocated memory
- heap-allocation
- allocation and deallocation under programmer control
- None of this is allowed, provided for, or required by Java
- Java's heap is garbage-collected
Object Creation
- Declaration
- Specifies type of (reference) variable
- Allocates space for the reference
- Does NOT allocate space for an object
new
operatro creates objects
- Equality
==
indicates equality of references
equals
method is used for (semantic) equality of objects
- class designer's responsibility to code a proper
equals
method
Arrays
- Improvement over C/C++ declaration syntax
- Creating
- 'Resizing'