CS 18000 Lecture Notes - Lecture 4: Newline, Downcasting, Exclusive Or

36 views5 pages
Real Number Types in Java
- Represent subsets of the real numbers
- 2 types: float (32 bits, single precision) and double (64 bits, double precision)
- I.e. x = 1532.7896 (or .15327896 * 10^4)
- Float: 10^(-38) ~ 10^(38) ~ 9 digits
- Double: 10^(-308) ~ 10(308) ~17 digits
Operations on Real Types
- Usual mathematical: +, -, *, /
- Math.pow(base, exponent)
- Math.log10(number)
- Trig functions, logs, etc.
Declaring Variables: Syntax
- Various possibilities supported
- int x; // declared only
- int x = 5; // and initialize
- int x, y; // two at once
- int x = 5, y = 10;
- Include a comment describing its purpose
- I.e. int mass ; // mass of the particle
Expressions
- Expressions built by combining:
- Variables (x, y) and literals (1, 2,) with operators (+, -, *, /)
- Usual mathematical precedence
- Multiplication and division first, addition and subtraction second.
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Type Promotion
- Mixing value types in expressions is allowable
- 3 + 5.4 -> 3.0 + 5.4 = 8.4
- 3 + 5.0 -> 3.0 + 5.0 = 8.0
- 1 / 2.0 -> 1.0 / 2.0 = 0.5
- 9.4 * 2 -> 9.4 * 2.0 = 18.8
- Values are “promoted” when it makes sense
- Short to int, float to double
- Integers to reals
Casting
- Cast: convert from a value of one type to a value of another
- Upcast: from “narrower” to “wider” (more bits)
- short -> int
- float -> double
- int -> double
- Downcast: from “wider” to “narrower” (fewer bits)
- int -> short
- double -> short
- double -> int
Casting Rules
- Upcasting is fine
- Nothing lost
- Java handles automatically (“promotes”)
- Double x = 1 / 2.0 -> 0.5
- Downcasting is dangerous
- Precision is lost
- Java prevents by default
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

2 types: float (32 bits, single precision) and double (64 bits, double precision) Various possibilities supported int x; // declared only int x = 5; // and initialize int x, y; // two at once int x = 5, y = 10; I. e. int mass ; // mass of the particle. Variables (x, y) and literals (1, 2,) with operators (+, -, *, /) Multiplication and division first, addition and subtraction second. Mixing value types in expressions is allowable. Values are promoted when it makes sense. Cast: convert from a value of one type to a value of another. Upcast: from narrower to wider (more bits) short -> int float -> double int -> double. Downcast: from wider to narrower (fewer bits) int -> short double -> short double -> int. Double x = 1 / 2. 0 -> 0. 5. Trust me; the expression can be converted to the indicated type .

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents