Commit 188f7540 by Adam Gerber

merged lab01g

parents bca93c8c 135298fe
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));
}
}
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++);
}
}
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment