Tuesday, September 8, 2015

How To Use Variables In Java Programs Post 4

Hi All...!

I hoped to teach you guys how to use casting from this post but now I think it is good to teach you how to use all types of data types before we learn casting. So today I'm going to teach you how to use "long" data type.

Let's see example code,


Output,




Normally in this way we can use "long" data type but if we assign a value that not inside the "int" value range, we will get a compilation error, to recall your memory about data types value range see the Data Types In Java.

Let's take a look at the error I mention above.




Read that error code "integer number too large", we used "long" data type but compiler says integer number too large, don't worry this happens because of the auto casting, I will teach you what is casting and auto casting in my upcoming posts,

Let's try to fix this error,

It is easy to fix this error we have to inform to compiler this value is a "long" value don't try to take this value as "int". To do that we have to type "L" or "l" after the value we typed,

Let's see the code,


Output,



Now try to add to "long" values,(This time 3 in 1 screen print)





Another way to do this,




Following is another way to do above coding, if you remember we failed to do following method to "byte" and "short" because compiler take those values as "int" values, but here we have informed compiler that values we are using not "int" values they are "long" so no compilation error occurs.




Note

If you guys find any difficulties when reading above three in one photos please let me know.

No comments:

Post a Comment