• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Upcasting works ?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is the data type of x * y / z ?

If I'm not wrong here 14 is int type( Default in java) constant then downcasted implicitly by compiler.
Now 14 (Short) is assigned to x.
13 is int upcasted to float and assigned to y.
30 is int upcasted to double & assigned to z.

x * y / z as * and / have same priority so left to right

As x is short now it is being multiplied to y which is a float so

x is upcasted to int then that int x then upcasted to float because it is being multiplied to y which is float.

x * y = 182.0f i.e. float

Now 182.0f / y, where y is a double so 182.0f is upcasted to double then result is double i.e 6.06d

I hope I'm correct ?



 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am too new to this language ..understood the up casting but i am not sure about down casting..does it happen ?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Sachin Tripathi yes it happens
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't say upcasting and downcasting: say widening conversion and narrowing conversion. I trust you are only writing that sort of code because you think it might come up in an exam; you should never use such code in real life.
You can read about such conversions in the Java® Language Specification (=JLS), though that can be difficult to understand.
I think you are correct throughout:
  • 1: 14 is an int but it is also a compile time constant which is within the range of a short so it can be assigned to the short.
  • 2: 13 is an int but it undergoes widening conversion to a float (13.0f) in the assignment.
  • 3: 30 is an int and it undergoes widening conversion to a double (30.0d) in the assignment.
  • 4: In the formula, x is a short and undergoes widening conversion to a float
  • 5: x * y is a float and undergoes widening conversion to a double and the result is a double.
  • You should have used s, f and d for the variable names. It would have been easier to understand.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ Campbell
    4: In the formula, x is a short and undergoes widening conversion to a float


    I think x is again undergoes widening conversion to int then to float. I was sure about this. Read in OCAJP 8 by Jeanne.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you are right: the short is widened to an int before the * sign, then widened again to a float when it encounters the float on the right of the *.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes , Credit goes to Jeanne's book learning a lot of new things never experienced such things.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!