Java programming lesson 2 : Variables

in #programming7 years ago (edited)

The most important thing in programming are variables. The reason is that they build the basic building blocks of programming. In this lesson you will learn about variables and what they really are and how you define them.

Variables

variables are the building blocks for every programming language. The reason for this is, because you need to be able to save things in order to make calculations with those values at a later point or make a decision with a variable at a later point in the application. You will see variables everywhere in code. I will show you the most important variable types you have in Java and how to use them.

First let me start by typing in the type of variables you can create in Java.
-int
-float
-double
-String
-char
-short
-long
-byte

I will explain to you what those types are and how to create them in code.

int -> This type is used for whole numbers, so numbers that are not comma separated. The minimum number is  -2^31 and the maximum number is  2^31-1.
float -> This type is used for comma separated numbers.
double -> This type is also for comma separated numbers, but this one is more used in Java than float. The reason why is, because double is more accurate than float.
String -> This type is a line of characters.
char -> This is one character.
short -> This type is used for smaller number the highest number a short can have is  32,767 and the minimum is  -32,768 .
long -> This type is used for larger numbers. The minimum number is   -2^63 and the maximum number is  2^63-1.
byte -> This type is also used for smaller numbers. The minimum is  -128 and the maximum is  127 .

If you want more detailed information about the types, you can go to the following website:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Let's create

Now let's see how you can define a variable in Java. In order to make a variable in Java you first need to specify the type. These are the types that we talked about earlier. After you have done that give the variable a name. Choosing the right name is very important, because if another programmer ever reads your code, he needs to understand what the meaning is of the variable. I will give a simple example of this.

Please remember to use a capital letter when you define a String type. Also you can see the slashes. These slashes are used to make comments. Comments are used to explain what you are doing with a specific line of code. This will make sure everybody understands what you want to do with a particular line of code. When you want to append a variable to the System.out.println();, you need to add a  + sign to let Java know that you want to append something to the sentence. One more thing never ever forget to add ';' at the end of each statement. If you forget that, you will get nasty errors. When you start making big applications you will have big troubles finding them.

So that is it for variables. Now you know how to create them and how to ouput them to the console. In the upcoming lessons we will learn some more stuff. We will take little steps.

Sort:  

Congratulations @seyful! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!