20-CS-4003-001 Organization of Programming Languages Fall 2017
Java Types

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

Data types in Java

   
Primitive types
boolean -  values from the set {truefalse}.
byte -  8 bit two's complement number: -128 to 127.
short -  16 bit two's complement number: -32,768 to 32,767.
int -  32 bit two's complement number: -2,147,483,648 to 2,147,483,647.
long -  64 bit two's complement number: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
float -  single-precision 32-bit IEEE 754 floating point: 24 bits signed magnitude (binary), mantissa between 1/2 and 1 (plus 1st bit implied as 1), 8 bits two's complement exponent.
double -  double-precision 64-bit IEEE 754 floating point: 53 bits signed magnitude (binary), mantissa between 1/2 and 1 (plus 1st bit implied as 1), 11 bits two's complement exponent.
char -  16 bit unicode: 0 to 65,535.
 
Type classes
    Use new to create objects of these types. Such objects have referential properties similar to pointers in C++. Thus,
   String a[] = new String[10];
does not contain any String objects, only possible references to 10 String objects. Something like the following
   for (int i=0 ; i < 10 ; i++) a[i] = new String("A");
is done to put objects in the string array. Methods can be invoked on objects to manipulate data - examples are shown for each type class. The = operator is overloaded for the String class to insure copies of String objects are made in assignments, the same way that copies of numbers are made when assignments are made to primitive data types. The + operator is also overloaded for the String class and means concatenation.
 
String - The operator == compares the locations of the operands, not the contents. The methods compareTo, match, regionMatches, and equals perform comparisons on values. Other methods pinpoint, return, or replace substrings, make conversions, for example to byte arrays, or determine string length.
AtomicBoolean - A new value is assigned to an object of this class only if its current value matches its "expected" value via compareAndSet. Not a replacement for Boolean.
AtomicInteger - A new value is assigned to an object of this class only if its current value matches its "expected" value via compareAndSet. Not a replacement for Integer.
AtomicLong - A new value is assigned to an object of this class only if its current value matches its "expected" value via compareAndSet. Not a replacement for Long.
BigDecimal - Arbirary precision signed decimal numbers. Use method divide instead of operator / and so on.
BigInteger - Arbirary precision integers. The internal representation of a BigInteger may be signed magnitude, unsigned, or two's complement. This has an important effect in case the BigInteger object is converted to another type such as a byte array.
Byte - Wraps a primitive byte in a class object. Provides methods for conversion to and from other types.
Float - Wraps a primitive float in a class object. Provides methods for conversion to and from other types.
Double - Wraps a primitive double in a class object. Provides methods for conversion to and from other types.
Integer - Wraps a primitive int in a class object. Provides methods for conversion to and from other types.
Long - Wraps a primitive long in a class object. Provides methods for conversion to and from other types.
Short - Wraps a primitive short in a class object. Provides methods for conversion to and from other types.