Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Primitive Types Operators Basic input and output.

Similar presentations


Presentation on theme: "Introduction to Java Primitive Types Operators Basic input and output."— Presentation transcript:

1 Introduction to Java Primitive Types Operators Basic input and output

2 Data Conversions What gets printed? What gets printed? –System.out.println(10 / 4.0 ); –System.out.println(10.5 + 4 ); –System.out.println( 10 - 4 ); –System.out.println( 10.25 * 4.0 ); Rules for math operators: Rules for math operators: –If BOTH operands are integers, then integer math is performed –Otherwise, floating point arithmetic is used

3 Data Conversions What happens if you try this in main? What happens if you try this in main? –int result = 10 / 4.0; –double result = 10 / 4; –double result = 10 % 3; –int result = 10 % 4.0;

4 Casting Rules If the conversion will lose data, Java will give an error If the conversion will lose data, Java will give an error –Going from 13.5 (double) to and int would lose information when 13.5 would become 13 –There is no problem however if you want to store 13 (int) into a double value because nothing is lost by 13.0 The way you tell Java you’re aware of the error and that it’s OK to compile is with a method called casting The way you tell Java you’re aware of the error and that it’s OK to compile is with a method called casting Casting changes the data type Casting changes the data type

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

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

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

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

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

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

11 Casting exercises

12 Casting Exercises Cont’


Download ppt "Introduction to Java Primitive Types Operators Basic input and output."

Similar presentations


Ads by Google