CS1335 Java and Processing
  • CS 1335 Computer Science 1
  • Getting Started
    • Processing IDE
    • Java vs Javascript
    • Review: Processing, Functions
    • HSB Color Mode
      • HSB Color Wheel
        • Example Code
      • HSB Color Palette Tool
    • Recursion
      • Recursion Call-Stack
      • Example Code
        • Example Code Feb 5 S20
        • Feb 12 Code
  • Project 1
    • Subjective Modeling of Emotions
    • Emotions represented using color, form, space
      • Kandinsky Color - Emotion
      • Emotional Intelligence
    • Project 1: PShapes
      • Example Code
      • Inspiration
    • PShape with Cutout - Inner Contour
    • VertexShape - Recursion
    • Project 1: Recursive Drawing
    • Project 1: Programmatic Variations in Color
      • Recursion with rotate, scale
      • Plan Region Size, Color
    • Map Function
    • Transforms for Mirroring
    • Project1-Steps
  • Grid Based Designs
    • Computational Design
      • Generative Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • OOP vs Data-Flow
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Example Code S20
      • Code Wed Apr 1
      • Code Wed Apr 8 v1
      • Code Wed Apr 8 v2
      • Code Mon Apr 13
      • Code Wed Apr 15
      • Code Mon Apr 20
      • Code Wed Apr 22
      • Code Mon Apr 27
      • Code Wed Apr 29
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Java Syntax
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
      • Learning Science
    • UML Class Diagram
    • Glossary
  • Resources and References
    • Resources
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page
  • Integer Division
  • Implicit Type-Casting: Widening Conversion
  • Explicit Type-Casting: Narrowing Conversion

Was this helpful?

  1. Java Syntax
  2. Java Syntax

Float - Integer Conversion Errors

When using mixed-types of data - integers and decimal values, it's important to understand how java handles data-type conversions when math expressions are evaluated.

Integer Division

When dividing 2 integer values, the result must be an integer value, so any fractional remainder is truncated.

We can think of integer division as long division where we have remainder values, the remainder is truncated:

5 / 2 = 2 remainder 1

5 / 2 = 2 (remainder is truncated or thrown away)

//integer division - truncation occurs
int myIntValue = 5 / 2;  //myIntValue = 2;

The code statement above has 2 components:

  1. A math expression: division operation:5 / 2

  2. Assignment operation: myValue =

The right-hand-side: math expression is evaluated first. The left-hand-size: assignment operation occurs after the math expression has been evaluated.

  1. Expression - code that is evaluated to result in a value.

  2. Assignment - operation that initializes or modifies the value stored in a variable.

// integer division - truncation occurs
float myFloatVal = 5 / 2;
println( "myFloatVal " + myFloatVal); // myFloatVal = 2.0

In the code above, the expression: 5 / 2 is evaluated first, it is integer division, since both 5 and 2 are integer literals. The result of 5 / 2 = 2, as shown above. Then the result of 5 / 2 is assigned to the float variable. Since float variables have a decimal component, the value of myFloatVal is 2.0 after the assignment operation has been completed.

//this is equivalent code, showing intermediate integer result 

int tempResult = 5 / 2;
float myFloatVal = tempResult;
println( "myFloatVal " + myFloatVal); // myFloatVal = 2.0
  1. Mathematical Operators - must operate on operands of the same data-type. Java will do implicit type-conversions. Java will convert operands of smaller (narrower) data-types to larger (wider) types, to match the size of the largest operand. This applies to both literal and variable operands.

    In the code below, we have 2 different types of operands, 5.0 is a decimal value, (a double by default), and 2 is an integer value. The java compiler does a conversion, it converts the smaller data-type: integer value, to match the other operand. So the actual expression that's evaluated is 5.0 / 2.0; This is floating point division, the result is 2.5, and it's valid for 2.5 to be stored in a floating point type variable. No error occurs

float floatVal = 5.0 / 2;  
println("floatVal " + floatVal); // floatVal = 2.5;

///Error
int intVal = 5.0 / 2; ///error occurs

In the code above, the error occurs when the result of the expression is assigned to a integer value...since the integer has no place to store the decimal part of the number, we're given an error to notify us that we're 'throwing away data'.

Implicit Type-Casting: Widening Conversion

Java does automatic type-casting when a math operator has operands of 2 different data-types, it will convert a smaller data-type to a larger data-type, for example int -> float, so the operator can operate on 2 values of the same data-type. This is hidden from us, it happens automatically, so it's called implicit.

float result = 5.0 / 2;   // 2 is auto-converted to 2.0
// result = 5.0 / 2.0; this is result of implicit conversion done by java

Explicit Type-Casting: Narrowing Conversion

If we know that we're ok with losing data - due to truncation issues, we can use explicit type-casting, to let the compiler know that we are intensionally discarding the truncated data. We are converting from a wider/ larger data-type to a smaller / narrower data-type: Example float to integer conversion

Explicit Type-Casting: 2 methods

int myIntVal1 = int( someFloatVal ); 
int myIntVal2 = (int) someFloatVal;  

//Example

int myIntVal = (int) 5.0 / 2;   //no error 

myIntVal = int( 5.0 / 2.0 );  //no error

myIntVal = (int) 5.0 / 2.0; ///error occurs since 2.0 hasn't been converted to an int
PreviousTyped-VariablesNextModulus

Last updated 5 years ago

Was this helpful?