Results 1 to 2 of 2

Thread: forced casting

  1. #1
    hen1610 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default forced casting

    Hi I wnat to know the diffrent between the 2 code:
    why a' doesnt work and b' does?
    a.
    { public class PlusExample
    { public static void main(String[] args)
    {
    int i = 0;
    double d = 1.0;
    i = i + d;
    System.out.println(i);
    }
    b.
    {
    public class PlusExample
    {
    public static void main(String[] args)
    {
    int i = 0;
    double d = 1.0;
    i += d;
    System.out.println(i);
    }

    Thanks
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    (double posted in advance section as well)
    b: i assume the compiler is smart enough to autocast d to int then add to i. but in a: it adds i and d first so you get a double value. then trying to assing an int to a double is an error.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 05:31 PM
  2. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  3. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 09:03 PM
  4. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •