• Skip to main content
  • Skip to primary sidebar

Founder at work

Master the skills to create and launch your next SaaS company

  • About Me
  • Reviews
  • Resources
  • Community
  • Contact
  • Lessons
  • Series
  • Search
  • Login

How to use primitive data types downward in java?

August 22, 2012 by Rick Hernandez Leave a Comment

Java is really picky on how primitive data types get converted downward so in that case java requires you to be very explicit on how the change is done when going downwards, and once again we are going to list the data types..

  • double x64 bits
  • float
  • long
  • int
  • short
  • byte

in this example we are going to be going from a double to a int so in order for this to happen we set a double with a variable name of  “doubleValue”  and set that to 2.99 then create an int with a variable name “intAnswer” once again the name is irrelevant to what i am trying to explain. now we set the int “intAnswer to the (int)doubleValue you must use this syntax to tell the compiler that you know what your doing in order for the conversion to happen or else you will get  a compile-time error. after the conversion gets done the value of 2.99 will get truncated to the number 2 integer loosing the precision of the double. here is the example code
double doubleValue = 2.99;
int intAnswer = (int)doubleValue;

Once again the take away from this is to be careful when converting downward or upward.

Rick Hernandez

I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Java, Mathematics, Programming

Primary Sidebar

Learn How To Take Your Idea And Convert It Into An App From Scratch Even If You Have Never Written A Single Line Of Code.

  • How to Find Your Next Great App Idea
  • The Secret To Getting PAID To Build Your Own App
  • How To Create Your App (SaaS, Mobile, VR, AR, Game)
  • How To Monetize Your Apps
Learn More
4.82 Ratings

Copyright © 2025 · JSecademy Blog Theme on Genesis Framework · WordPress · Log in