Showing posts with label Data Types In Java. Show all posts
Showing posts with label Data Types In Java. Show all posts

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.

Monday, August 31, 2015

Data Types In Java



Hi All...!

Today I'm going to show you what are the "Data Type" we can use in Java programming, In Java there are two types of data types. One is "Primitive Types" or "8 Basics" and other type is "An Unlimited Number Of Object Type"


Primitive Types Or 8 Basics


Following 8 data types are known as primitive types or 8 basics,

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean

Note


When we type any of above data type in our Java program we must use only simple letters.



Unlimited Number Of Object Types (Non - Primitive)


For this type "String" is a good example. You will get to know about these data type in future.



8 Basics Or Primitive Data Types Chart




In my next post I will show you how to use these data types in Java programs.