Sunday, December 20, 2015

Casting part 3

Hi All...!

Today I'm going to show you how to cast int data type's variables to long data type. If someone don't have any idea about Java data type please read the post Data Types In Java. Again I like to recall your memory "Casting" means converting large ranged data type's variables to small  ranged data type if it is comes under the range of the small data type.

Let's see the code,


Same thing happens if the value of the long variable exceed the limit of the int data type please read the post about casting Casting part 1 and Casting part 2

Sunday, December 13, 2015

Casting part 2

Hi All...!

Today I'm going to teach you guys how to cast "int" data type variables in to "short". Keep it in mind we do casting to convert large capacity data type's variables to smaller capacity data type's variables.

Let's cast "int" to "short"



Let's see what will happen when int value exceed range of short value (short value range is -32768 to 32767 short gets this range because short is 16 bit data type and 2^16  )


32767 is inside the short range, Let's do the same coding with the 32768 and find out what will happen.


Answer is -32768 we got this value because 2^16 is 65536 and 32768 - 65536  = -32768.

Like this if we cross the short range from minus side we will get,



-32769 + 65536 = 32767.

Wednesday, December 9, 2015

Casting part 1

Hi All...!

Today I'm going to teach you how to do casting in Java. Casting is a method we can use to covert one variable belong to specific data type to another data type. To do this that variable must be fit in to the range of the data type we are going to convert.


Let's see an example,


As we see in above code we can assign "int" value to the "byte" if that value is fit to "byte" data type range.

Range of  the "byte" data type is -128 to +127 if go out of the range you will get output -256 or +256 (2^8 byte is 8 bit data type ).

As a example if we use int value as +128 and cast it to byte you will get the out put value as -128.


Let's assign -129 to the int value after you do the casting you will get the out put value as +127.



Wednesday, November 25, 2015

How To Use Variables In Java Programs Post 9

Hi All...!

Today I'm going to teach you how to work with char data type. char is 16 bit data type if you are not familiar with char data type please read 8 Basics topic.

Let's get to coding,

Declare and initialize char variable,

There are number of way to declare and initialize char variables but I'm going to show you the most commonly using ways,

We can directly assign single character to char variable, here we have to write that character in side the single quotation marks.




The other way is using "ASCII" codes we can also initialize the char variables, when we use ascii codes we don't want to use single quotation marks.




ASCII code table is given below




Another way to initialize char variables is using unicodes,



Following is table of some unicodes,



See you guys on my next post bye...!

Tuesday, November 10, 2015

How To Use Variables In Java Programs Post 8

Hi All...!

First of all I would like to say Sorry for not publishing post for month. Today I'm going to teach you how to use boolean data type in Java. boolean is one of a 8 basics, boolean data type only can hold two type of data "true" or "false". boolean data type is very useful when we us "if" conditions in Java to make decisions. In future posts I will teach you what are the if conditions and how to use them to decision making programs. Now just learn how to declare and initialize variables using boolean data type.

Let's do coding...!




As I shown in above two codes we can directly assign "true" and "false" values to boolean variable, There are another way to assign these two values to boolean variable we can use mathematical condition to assign "true" or "false" value to boolean variable.

Let's learn how to use mathematical conditions,


Wednesday, September 30, 2015

How To Use Variables In Java Programs Post 7

Hi All...!

Today I'm going to show you how to use "String" data type in Java. As I told you earlier "String" is categorize in to "Unlimited Number of Object Type" if you don't have an idea about what is "Unlimited Number of Object Type" please read Data Types In Java.

Let's Use "String" in code,




Did you notice that I have use pair of Quotation Marks and I wrote the value which I want to assign for the "String" is in side that Quotation Marks. You must follow this value assigning method when you use "String" data type.

Let's combine to "String" values by using "+" mark, When you use "+" mark to combine two "String" values we call that "concatenation operator", don't worry about Java operators I will teach you all about Java operators in my upcoming posts.

Let's do the coding,






Let's try another way to do above task,






Monday, September 21, 2015

How To Use Variables In Java Programs Post 6

Hi All...!

This post is about how to use "double" data type in Java programming. "double" is a data type in Java which can hold decimal values up to 64-bits. To learn more about Java data type please read Data Types In Java.

Let's see how to use "double" data type in Java code.



Note:

We have assigned 1 for double d but there is 1.0 in the out put this happens because double data type dedicated for decimal values.

Let's assign 1.0 to double d and check error occurs with float data type is arise again ? Error occurs in float data type please read How To Use Variables In Java Programs Post 5




It doesn't happen because compiler by default take decimal values as double value.

Let's add two double values,





Another way to do double value addition,




Thursday, September 17, 2015

How To Use Variables In Java Programs Post 5

Hi All...!

Sorry for not publishing a post for weeks, little bit busy with my final year examination.

Okay let's get to work, today I'm going to show you how to use decimal values in Java. Previously
we worked with byte, short, int, long all these data types are using for integer values. In Java we can use "float" and "double" to deal with decimal values. Read more about data types in Java Data Types In Java.

Here I'm going to show how to work with "float" data type.

Let's use "float" in code,




Here you can see assigned value for "float f" is 1 but printed output is 1.0, this happens because float use to represent decimal values. No big deal.

Let's assign 1.0 to "float f"




Here comes the trouble for decimal values, as I told you in my early post all integer values by default take as "int" values if those values are in "int" range. In here all decimal values by default take as double values by compiler. That is why above error occurs.

To fix this error we have to type a "F" or "f" after the assigned value,






Let's write a code to add two float values together,





Another ways to do this addition,





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.

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.



How To Use Variables In Java Programs Post 2

Hi All...!

In this post I'm going to show you how to work with variables made using "short" data type

Let's see the code,




Output,





Let's try to add two short values,





Output,





Another way to do this,





Output,