This is the second lecture of the Learn Java lecture series. In this you’ll learn about variables in Java.


Variable is like a box in which you can put a value and we got different sizes of boxes. There is a convention in java that the name of the variable should be first word should start with small letter and rest of the word will start with capital words. For Example:

This is how you can declare a variable in java and see the convention I was talking about, this is how you’ll use it. So myNumber is a type int meaning it is an integer variable and can hold an integer value. Int has the following types too:

int is 32 bit value.

short is a 16 bit value

long is used to store numbers more that int and short, it is 64 bit.

Other Types:

To store floating point number you need to we used double for it.

There is no long version of double, but there is a short one called float.

In java when you declare a float like float myFloat = 342.4 will give you an error. Because this is how double number is written so you need to add ‘f’ at the end of it in order to make it a float.

char is a type which can hold a unicode character and you can specify a character by putting a single quote around it as:

boolean is very useful variable type in java to hold true or false.

Lastly, we have byte which can hold 8-bits of data.

So in file add this code:

Run the code and you’ll see the following as output.

Chaudhary Talha

The author Chaudhary Talha

I'm an iOS developer by profession and UI/UX designer by passion. All my tutorials are based on my personal experiences and whenever I learn something new I share it with the world.