Type casting with cast operator - Java Language Basics

Java examples for Language Basics:Number Cast

Introduction

To cast a primitive value, use a cast operator.

A cast operator is the name of a primitive type in parentheses placed before the value you want to cast.

Demo Code

public class Main {
  public static void main(String[] args) {
    double pi = 3.1314;
    int iPi;//  w w  w.  j av a2  s  .com
    iPi = (int) pi;

    System.out.println(iPi);
    System.out.println(pi);
  }

}

Related Tutorials