Sunday, September 6, 2015

How To Use Variables In Java Programs Post 3

Hi All...!

This post will show you how to work with "int" data type,

Let's get to coding,




Output,





Let's try to add two int values,




Output,





another way to add two values,




Output,




There is another special way we can use to add two "int" variables, but we can't use this for "byte" and "short" data type at once,





Output,




In above code I have made a new int variable call "z" and then I assigned "i + x" to the "z" and then I put that "z" variable in side the print method.

We can't do this when we use "byte" or "short" data type, if we try this we will get a compilation error,






This error occurs because compiler take integer values by default to the "int" data type, we can fix this error by using "casting" I will show you how to do that in my next post.



No comments:

Post a Comment