Search This Blog

Tuesday, July 22, 2025

Java Datatypes -1

Can you assign a fractional value to an int by typecasting? What will be the outcome of the following code?

 1. /**

 2.  * @author Nancy K.A.N.

 3.  */

 4. public class Datatype1 {

 5.     public static void main(String[] args) {

 6.         int x= (int)4.7;

 7.         System.out.println("x="+x);

 8.     }

 9. }

10.  

 

Created by Nancy N.

@12:21 22-Jul-2025

3 comments:

  1. Replies
    1. Yes. Double value is typecasted to an int value. As typecasting does not round the value. it prints the int part of the number.

      Delete
  2. Yes, we can but the output will be 4 the integer part only.

    ReplyDelete