Saturday, October 18, 2008

Section 1.1: Primitive, Enumeration, and Reference Types

Requirement: Describe, compare, and contrast primitives (integer, floating point, boolean, and character), enumeration types, and objects.

Primitive Types:
There are 8 primitive types in Java, divided as follows:

  • 4 are integer types. (1.1.1) They represent positive and negative values without fractional parts. (1.1.2) The following table shows all of the 4 integer primitive types (click on the image to enlarge):

(1.1.3) The range of the integer types doesn't depend on the machine on which you will be running the Java code. (1.1.4) Long integers have a suffix L (ex: 400000L). (1.1.5) While hexadecimal numbers have a prefix 0x (ex: 0x10FE). (1.1.6) Octal numbers have a prefix 0 (ex: 010).

  • 2 are floating-point number types. (1.1.7) Represent numbers with fractional parts. (1.1.8) The following table shows the 2 floating-point numbers:

(1.1.9) The double type is used in most applications. (1.1.10) The float type is only used when processing single-precision numbers and at the same time speed processing is important. (1.1.11) Numbers of type float have a suffix of F (ex: 1.23F). (1.1.12) Numbers of type double have an optional suffix of D (ex: 23.64433533D). (1.1.13) Floating-point numbers can be written in hexadecimal (ex: 0.125 can be written as 0x1.0p-3) where the base in written in hexadecimal while the exponent is written in decimal format. (1.1.14) There are 3 special values to denote overflows and errors: positive infinity, negative infinity, and NaN (not a number) (ex: 0/0 = NaN, 12/0 = positive infinity). (1.1.15) To test whether a floating-point number equals any of the previous special values you can use Double.isNaN(x), Float.isNaN(x), etc.. (1.1.16) Floating-point numbers are not suitable for financial calculations because of round off errors.

  • 1 is the character type char, used for Unicode. (1.1.17) Used to describe individual characters. (1.1.18) Unicode values range from \u0000 to \uFFFF. (1.1.19) Only the escape sequence \u can be used outside quoted character constants and strings (ex: String[] args is exactly the same as String\u005B\u005D args). (1.1.20) The following table represents escape sequences for special characters:

(1.1.21) The char type describes a code unit in UTF-16 encoding ( (1.1.22) It is strongly recommended not to use the char type in your programs unless you actually manipulate UTF-16 code units (it is better to use Strings).

  • 1 is the boolean type for truth values. (1.1.23) It has 2 possible values true and false which are used for evaluating logical conditions. (1.1.24) You cannot convert between integer and boolean values.

Enumeration Type:
We will start by an example showing how to declare an enumerated type:

public enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE };

(1.1.25) The enumeration type just defines is actually a class. (1.1.26) The class has exactly 4 instances, it not possible to construct new objects. (1.1.27) You never need to compare enum using equals methods, just use the == operator. (1.1.28) You can add constructors, methods, and fields to enumerated types. (1.1.29) The constructors are only invoked when the enumerated constants are constructed.

Example (1.1.28, 1.1.29):

enum Size {

public Size(String abbreviation) { this.abbreviation == abbreviation; }
public String getAbbreviation() { return abbreviation; }

private String abbreviation;

(1.1.30) All enumerated types are subclasses of the class enum. (1.1.31) The toString method returns the name of the enumerated constant (ex: Size.SMALL.toString() returns "SMALL"). (1.1.32) The static valueOf method is the converse of the toString method (ex: Size s = (Size)Enum.valueOf(Size.class, "SMALL"); sets s to Size.SMALL). (1.1.33) Each enumerated type has a static values method that returns an array of all values in the enumeration (ex: Size[] values = Size.values(); fills the array with elements Size.SMALL, Size.MEDIUM, Size.Large, and Size.EXTRA_LARGE). (1.1.34) The ordinal method yields the position of an enumerated constant in the enum declaration counting from 0. (1.1.35) The enum class has a type parameter that is used in the compareTo method (ex: the enumerated type Size actually extends Enum). (1.1.35b) When you use the switch statement with enumerated constants, you need not supply the name of the enumeration in each label - it is deduced from the the switch value.

(1.1.36) A class is a template or blueprint from which objects are made (an object is an instance of its class). (1.1.37) All of the Java code is written inside classes. (1.1.38) An object has 3 basic characteristics: behaviour, state, and identity. (1.1.39) The object's behaviour is what can you do with this object, or what methods can you apply to it? (1.1.39b) The object's behaviour is defined by the methods you can call. (1.1.40) The object's state is how does the object react when you apply these methods to it? (1.1.40b) The state is represented by the information that the object stores about what it currently looks like. (1.1.40c) An object's state can change over time. (1.1.41) The object's identity is how is the object distinguished from others that may have the same behaviour and state? (1.1.42) Individual objects that are instances of a class have the same behaviour, always differ in their identities, and usually differ in their states.

(Approximately all of the material written here is taken from the references mentioned in the first post. I don't claim to have authored any part of this material . I just compile my own notes from one or many of these references).

No comments: