CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.


Primitive Types

Machine Type Java Type Width C++ Type Width
Floating Pointfloat32 bitsfloat32 bits
Floating Pointdouble64 bitsdouble64 bits
Floating Pointlong double96 or 128 bits
Signed Integerbyte8 bits
Signed Integerintword width
Signed Integershort16 bitsshort16 bits
Signed Integerint32 bitslong32 bits
Signed Integerlong64 bits
Unsigned Integerunsignedword width
Unsigned Integerunsigned short16 bits
Unsigned Integerunsigned long32 bits
Unsigned Integerchar16 bitschar8 bits
Booleanbooleanboolword width

Operator Precedence in Java

Operator Precedence in C++

Operator Description Level Associativity
[]
.
()
++
--
access array element
access object member
invoke a method
post-increment
post-decrement
1 left to right
++
--
+
-
!
~
pre-increment
pre-decrement
unary plus
unary minus
logical NOT
bitwise NOT
2 right to left
()
new
cast
object creation
3 right to left
*
/
%
multiplicative 4 left to right
+ -
+
additive
string concatenation
5 left to right
<< >>
>>>
shift 6 left to right
<  <=
>  >=
instanceof
relational
type comparison
7 left to right
==
!=
equality 8 left to right
& bitwise AND 9 left to right
^ bitwise XOR 10 left to right
| bitwise OR 11 left to right
&& conditional AND 12 left to right
|| conditional OR 13 left to right
?: conditional 14 right to left
  =   +=   -=
 *=   /=   %=
 &=   ^=   |=
<<=  >>= >>>=
assignment 15 right to left
Level Operator(s) Description Associativity
17
:: global scope (unary)
right-to-left
17
:: class scope (binary)
left-to-right
16
-> . member selectors
left-to-right
16
[ ] array index
left-to-right
16
( ) function call
left-to-right
16
( ) type construction
left-to-right
16
sizeof size in bytes
left-to-right
15
++ -- increment, decrement
right-to-left
15
~ bitwise NOT
right-to-left
15
! logical NOT
right-to-left
15
+ - unary plus, minus
right-to-left
15
* & dereference, address-of
right-to-left
15
( ) cast
right-to-left
15
new delete free store management
right-to-left
14
->* .* member pointer selectors
left-to-right
13
* / % multiplicative operators
left-to-right
12
+ - arithmetic operators
left-to-right
11
<< >> bitwise shift
left-to-right
10
< <= > >= relational operators
left-to-right
9
== != equality, inequality
left-to-right
8
& bitwise AND
left-to-right
7
^ bitwise exclusive OR
left-to-right
6
| bitwise inclusive OR
left-to-right
5
&& logical AND
left-to-right
4
|| logical OR
left-to-right
3
? : arithmetic if
left-to-right
2

= *= /* %= += -=
<<= >>= &= |= ^=

assignment operators
right-to-left
1
, comma operator
left-to-right

Explicit Type Conversion

Sometimes you need to convert from one primitive data type to another.  Numeric data can be produced by an expression.  Expressions can include variables,  literal values,  and return values from method calls.  This processes is called casting in Java and C++.  This process is sometimes called coersion.  Implicit type conversions are often called coersions.  Type conversion can be accomplished by using the following casting mechanism common to both Java and C++. 

(type) expression

Attempted Implicit Type Conversion

// Dr. Mats P.E. Heimdahl
// University of Minnesota
// Spring 2006

int i;
double d;

std::cout << "Please input an int: ";
std:cin >> i;
std::cout << "Please input a double: ";
std::cin >> d;
std::cout << "d as an int: " << (int)d << std::endl;
std::cout << "i as a double: " << (double)i << std::endl;
std::cout << "i as an int: " << i << std::endl;
i = d;
std::cout << "d assigned to i: " << i << std::endl;

Character Arithmetic

Because Java and C++ character codes are integers,  you can convert the character 1 to the integer 1 by subtacting the character 0.  Similarly,  you can convert the integer 5 to the character 5 by adding the character 0. 




Last modified: 2007 SEP 17
bnostran@syr.edu