introduction to java primitive types operators basic input and output
TRANSCRIPT
Introduction to JavaIntroduction to Java
Primitive TypesPrimitive Types
OperatorsOperators
Basic input and outputBasic input and output
Data ConversionsData Conversions
What gets printed?What gets printed?– System.out.println(10 / 4.0 );System.out.println(10 / 4.0 );– System.out.println(10.5 + 4 );System.out.println(10.5 + 4 );– System.out.println( 10 - 4 );System.out.println( 10 - 4 );– System.out.println( 10.25 * 4.0 );System.out.println( 10.25 * 4.0 );
Rules for math operators:Rules for math operators:– If BOTH operands are integers, then If BOTH operands are integers, then
integer math is performedinteger math is performed– Otherwise, floating point arithmetic is Otherwise, floating point arithmetic is
usedused
Data ConversionsData Conversions
What happens if you try this in main?What happens if you try this in main?– int result = 10 / 4.0;int result = 10 / 4.0;– double result = 10 / 4;double result = 10 / 4;– double result = 10 % 3;double result = 10 % 3;– int result = 10 % 4.0;int result = 10 % 4.0;
Casting RulesCasting Rules
If the conversion will lose data, Java will If the conversion will lose data, Java will give an errorgive an error– Going from 13.5 (double) to and int would lose Going from 13.5 (double) to and int would lose
information when 13.5 would become 13information when 13.5 would become 13– There is no problem however if you want to There is no problem however if you want to
store 13 (int) into a double value because store 13 (int) into a double value because nothing is lost by 13.0nothing is lost by 13.0
The way you tell Java you’re aware of the The way you tell Java you’re aware of the error and that it’s OK to compile is with a error and that it’s OK to compile is with a method called castingmethod called casting
Casting changes the data typeCasting changes the data type
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( i1 / (int)d2 );System.out.println( i1 / (int)d2 );
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( d2 / (double)i2 );System.out.println( d2 / (double)i2 );
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( d2 / (int)d4) );System.out.println( d2 / (int)d4) );
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( (int)(d1 / d4) );System.out.println( (int)(d1 / d4) );
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( (double)(i1 / (int)d4) System.out.println( (double)(i1 / (int)d4)
););
Casting ExamplesCasting Examples
int i1 = 12;int i1 = 12; int i2 = 4;int i2 = 4; int i3 = 6;int i3 = 6; double d1 = 24.0;double d1 = 24.0; double d2 = 3.0;double d2 = 3.0; double d3 = 2.0;double d3 = 2.0; double d4 = 2.5;double d4 = 2.5;System.out.println( (double)i2 / i3 );System.out.println( (double)i2 / i3 );
Casting exercisesCasting exercises
Casting Exercises Cont’Casting Exercises Cont’