introduction to java primitive types operators basic input and output

12
Introduction to Introduction to Java Java Primitive Types Primitive Types Operators Operators Basic input and output Basic input and output

Upload: grant-booker

Post on 13-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Java Primitive Types Operators Basic input and output

Introduction to JavaIntroduction to Java

Primitive TypesPrimitive Types

OperatorsOperators

Basic input and outputBasic input and output

Page 2: Introduction to Java Primitive Types Operators Basic 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

Page 3: Introduction to Java Primitive Types Operators Basic input and output

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;

Page 4: Introduction to Java Primitive Types Operators Basic input and output

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

Page 5: Introduction to Java Primitive Types Operators Basic input and output

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 );

Page 6: Introduction to Java Primitive Types Operators Basic input and output

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 );

Page 7: Introduction to Java Primitive Types Operators Basic input and output

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) );

Page 8: Introduction to Java Primitive Types Operators Basic input and output

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) );

Page 9: Introduction to Java Primitive Types Operators Basic input and output

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)

););

Page 10: Introduction to Java Primitive Types Operators Basic input and output

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 );

Page 11: Introduction to Java Primitive Types Operators Basic input and output

Casting exercisesCasting exercises

Page 12: Introduction to Java Primitive Types Operators Basic input and output

Casting Exercises Cont’Casting Exercises Cont’