Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type casting

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends,

Can someone tell me about typecasting aong with an example?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In line1:
b is a variable of type byte.Here we are assigning a value of 129 to b.But since byte can hold values only from -128 to 127 we are explicitly converting 129 to byte.This is called as narrowing conversion.

In line2: We are converting the integer i to a byte value.

in line3: We are converting the float value to a integer value.This is called as Truncation because the numbers after the decimal point are lost.

In line4: We are converting a double value to a float type.

Hope you Understood.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a specific question about casting?

If not, there's a lot that can be covered. See JLS - 5.5 Casting Conversion.
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks muthu.You have given example of pritimve types ,its neat.Can you please explain me casting with refernce types.javascript: x()
Smile


Thanks marc for the link i just wanted to know how type casting works with refernce types.As i am new to java it confuses me a lot.
reply
    Bookmark Topic Watch Topic
  • New Topic