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
188f7540
authored
Mar 15, 2016
by
Adam Gerber
Browse files
Options
Browse Files
Download
Plain Diff
merged lab01g
parents
bca93c8c
135298fe
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
0 deletions
src/lec01/glab/MoreDownCasting.java
src/lec01/glab/PostFix.java
src/lec01/glab/PromotionAndCastingPrimitives.java
src/lec01/glab/MoreDownCasting.java
0 → 100644
View file @
188f7540
package
lec01
.
glab
;
/**
* Created by Adam on 10/4/2015.
*/
public
class
MoreDownCasting
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"integer values"
);
//try the following 259, 132, 128
short
sResult
=
132
;
byte
yResult
=
(
byte
)
sResult
;
System
.
out
.
println
(
yResult
);
System
.
out
.
println
(
"float values"
);
//double to float downcasting
double
lValue1
=
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012
;
double
lValue2
=
101.6546465465464321351654313465123168768546134650646546546546546545645645646545646546546546545646546654564654646546540303212
;
float
fValue1
=
(
float
)
lValue1
;
float
fValue2
=
(
float
)
lValue2
;
System
.
out
.
println
(
String
.
valueOf
(
lValue1
));
System
.
out
.
println
(
String
.
valueOf
(
fValue1
));
System
.
out
.
println
(
String
.
valueOf
(
fValue2
));
System
.
out
.
println
(
String
.
valueOf
(
lValue2
));
System
.
out
.
println
(
"trucate and then round"
);
//truncate by casting to int
System
.
out
.
println
((
int
)
lValue2
);
//round by rounding
System
.
out
.
println
(
Math
.
round
(
lValue2
));
}
}
src/lec01/glab/PostFix.java
0 → 100644
View file @
188f7540
package
lec01
.
glab
;
/**
* Created by Adam on 10/5/2015.
*/
public
class
PostFix
{
public
static
void
main
(
String
[]
args
)
{
//postfix and prefix these
int
nC
=
0
;
while
(
nC
<
10
){
System
.
out
.
println
(
nC
++);
}
}
}
src/lec01/glab/PromotionAndCastingPrimitives.java
View file @
188f7540
...
...
@@ -6,6 +6,44 @@ public class PromotionAndCastingPrimitives {
* @param args
*/
public
static
void
main
(
String
[]
args
)
{
double
dOperand1
=
5.897
;
int
nOperand2
=
8
;
//promotion works when:
//there are two operands of different primtive types; the smaller precision operand will get promoted to larger precision operand
double
dResult
=
dOperand1
/
nOperand2
;
//here nOperand2 is promoted to double
//integer division - will truncate, not round!
int
nResultIntDivision
=
23
/
8
;
//the primitive result on the left of the assignment operator is of greater precision than the result of the right expression
//in this case, the result of integer division is 2.66666 trucated to 2, then the result is assigned as (2.0)
float
fResult
=
nOperand2
/
3
;
//you concatenate a string with a primitive, the primitive is promoted to a string
String
strResult
=
"Hello"
+
dOperand1
;
System
.
out
.
println
(
"nResultIntDivision: "
+
nResultIntDivision
);
System
.
out
.
println
(
"dResult: "
+
dResult
);
System
.
out
.
println
(
"fResult: "
+
fResult
);
System
.
out
.
println
(
"strResult: "
+
strResult
);
//you can downcast a primitive.
//upcasting happens automatically; it's called promotion and you lose no precision
//downcasting loses precsion
int
nAnswer1
=
(
int
)
56.9356974
;
//long lAnswer1 = Math.max(3.856, 98798798);
long
lAnswer1
=
Math
.
max
((
long
)
3654.856
,
568211
);
//you must cast and casting primitives sometimes loses precision
System
.
out
.
println
(
nAnswer1
);
System
.
out
.
println
(
lAnswer1
);
//BASIC RULES OF INTEGER DIVISION
//keep in mind that integer division produces a TRUNCATED result, not rounded
...
...
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