# Lecture07 - Data Comparisons and Switch Data Comparisons...

This preview shows pages 1–6. Sign up to view the full content.

1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3 - 5.4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of certain data types Let's examine some key situations: Comparing floating point values for equality Comparing characters Comparing strings (alphabetical order) Comparing object vs. comparing object references
3 Comparing Float Values You should rarely use the equality operator ( == ) when comparing two floating point values ( float or double ) Two floating point values are equal only if their underlying binary representations match exactly Computations often result in slight differences that may be irrelevant In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Comparing Float Values To determine the equality of two floats, you may want to use the following technique: If the absolute value of the difference between the two floating point values is less than the tolerance, they are considered to be equal The tolerance could be set to any appropriate level, such as 0.000001 if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal");
5 Comparing Characters As we've discussed, Java character data is based on the Unicode character set Unicode assigns a particular numeric value to each character and this creates an ordering of characters We can use relational operators on character data based on this ordering For example, the character

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

### Page1 / 18

Lecture07 - Data Comparisons and Switch Data Comparisons...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online