Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Adam Gerber
/
labJava
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dc1d6d12
authored
May 11, 2015
by
Joe Student
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casting primitives example
parent
6eb61a92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
8 deletions
src/lec07/glab/casting/CastPrimitivesDriver.java
src/lec07/glab/casting/CastPrimitivesDriver.java
View file @
dc1d6d12
...
...
@@ -21,28 +21,39 @@ public class CastPrimitivesDriver {
//if we read the above from right-to-left, we get
//2^0 + 2^1 + 2^2 + 0 + 2^4 + 0 + 0 + 2^7 + 0 etc, which == 151
short
sMe
=
151
;
short
sMe
=
407
;
// short sMe = 151;
//Rather than pass the primitive directly into println, I'm going to use the wrapper class .valueOf(), which enforces the type
System
.
out
.
println
(
Short
.
valueOf
(
sMe
));
System
.
out
.
println
(
"Short value of sMe : "
+
Short
.
valueOf
(
sMe
));
//Let's promote this to an int which is a 32-bit signed integer ranging from -2^31 to 2^31 - 1
int
nMe
=
(
int
)
sMe
;
//no problem, we don't really need all the precision, but memory is cheap, and the value stays the same.
//when we copy the bits, we just put those bits into an 32-bit int like so ->
//0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 1
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 1
//upcasting is automatic (no cast required) and looses no precision
System
.
out
.
println
(
Integer
.
valueOf
(
nMe
));
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1
//0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1
System
.
out
.
println
(
"Integer value of sMe : "
+
Integer
.
valueOf
(
nMe
));
//and the original value stored in sMe is unchanged.
System
.
out
.
println
(
Short
.
valueOf
(
sMe
));
System
.
out
.
println
(
"Short value of sMe : "
+
Short
.
valueOf
(
sMe
));
//Let's demote this to an byte which is an 8-bit signed integer ranging from -128 to 127
//notice that 151 is out of this range, so when you copy the bits over, you will get erroneous results.
//notice that 407 is out of this range, so when you copy the bits over, you will get erroneous results.
//downcasting requires a cast and you loose precision and potentially erroneous results.
//you flipped the sign-bit, so now, the result is -128 + 1 + 2 + 4 + 16, == -105. This is obviously erroenous.
//0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1
// 1 0 0 1 0 1 1 1
byte
yMe
=
(
byte
)
sMe
;
System
.
out
.
println
(
Byte
.
valueOf
(
yMe
));
System
.
out
.
println
(
"Byte value of sMe : "
+
Byte
.
valueOf
(
yMe
));
//If we cast to a double (64-bit floating point value), we lose no precision. The number just becomes 151.0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment