Lecture07 - Data Comparisons and Switch Data Comparisons...

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

View Full Document Right Arrow Icon
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3 - 5.4
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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");
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
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 Right Arrow Icon
Ask a homework question - tutors are online