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,




How To Use Variables In Java Programs Post 1

Hi All...!

Today I'm going to show you how to use Variables in Java Programming. I have shown you how to declare a variables and initialize a variable in my previous posts (How To Initialize Variable In Java, How To Declare A Variable In Java ). In this post I'm going to show you how to use Variables created by using "byte" data type.

Let's see how to do this coding,



This is the output,





Let's see how to add two byte values, here I'm going to use simple addition operator. Don't worry about that operator I will show you how to use those operator in Java in my upcoming posts.



 

Here comes the output,





Let's try to do this in another way,




Output,





Saturday, September 5, 2015

How To Initialize Variable In Java

Hi All...!

Let's see how to initialize a variable in Java, In my post  "How To Declare Variable In  Java"  I told you how to declare a variable.

Variable initialization means assigning a value to a variable, we can do it in two ways,


Method 1


We can declare a variable first and then we can initialize it,

int i ;

i = 10;


Method 2


We can declare and initialize variable at the same time,


int i = 10;