Learning Programming #1.1 The Basics: Variables

in #coding5 years ago (edited)

In #1 I'm going to explain the basic knowledge you will need to start programming. This knowledge is usually language independent, but there may be some differences. I will only describe things that are relevant for java and python which I suggested as good languages for starters.
Screenshot from 2019-04-29 13-25-17.png
Variables take a fundamental role in programming. You cannot create a single program without using any kind of variable. There are two different kinds of variables: primitives and objects.
Primitive variables are variables that can be stored as a single sequence of bits. Those are numbers, logical values and even simple text characters.
Logical values are referred to as bool or boolean. They can only take two values: true and false. You can perform any logical operator on them (see #1.2).

There are two kinds of numbers that are used in programming:

  1. integer numbers. Those are numbers that can take integer values(…, -3, -2, -1, 0, 1, 2, 3, …) that are within a certain region depending on the type of integers you are using. In java there are 4 different types:
type namenumber of bits usedminimal valuemaximal value
byte8-2⁻⁷ = -1282⁷ – 1 = 127
short16-2⁻¹⁵ = -32,7682¹⁵ – 1 = 32,767
int32-2⁻³¹ = -2,147,483,6482³¹ – 1 = 2,147,483,647
long6402⁶⁴ – 1 = 18,446,744,073,709,551,615

Usually int is used because today most processors are 32 or 64 bit, so byte and short would just decrease the number of different values that could be stored without increasing the performance of the processor, while long can only take positive values and should therefor only be used when such big values are needed.

  1. floating point numbers. Those numbers can also take values of fractions (like -0.5 or 2749.26472782 or almost anything else). They usually also can have a few special values like infinity or not a number. They can have incredible high(up to over ±10³⁰⁰) or low(less than 10⁻³⁰⁰) values, but they are only accurate for a few digits. This means that if you for example would add 1 to 10³⁰ for 10³⁰ times you would still get 10³⁰ instead of 2*10³⁰, because 1 is below the accuracy of 10³⁰ and therefor is ignored in calculation. In java there are two types: float and double. Unless you really need an optimized program(which you don't as a beginner), you should use double, because the imprecision of float is a lot higher is visible in some applications.

You can use all mathematical operations on both types of numbers(+,–,×,÷, see #1.2 for more). On integers you can also use bit-wise logical operators and bit-shifts(see #1.2).

Then there are characters which are referred to as char. They are encoded in a binary number of 8-16 bit length depending on the encoding method. You can do the same operations with them as with integer numbers.

Now there are also objects. You usually cannot perform any operations on them, but you can call functions of them(see #1.4) and you can access internal variables of objects, if you have the permission to do so.
You can get them from other sources(like functions or other objects) or you can create new objects.
Objects are usually part of a library or an Object you created yourself. If it is part of library you need to install that library if it isn't already installed(like the standard library). Then you need to import the object. This is done at the beginning of the file using import + what you want to import. In java the latter is just the path of the object in the library or relative to your file and the name of the object you want to use.

You can give all your variables any name you want, unless you want to use the name of another variable or the type of any other variable or a keyword(like new, public, private, static, …) which will confuse the compiler.
You can create new variables anywhere you want, except in java you can only create them inside a class. Creating a variable can be done using the following scheme:
"type of the variable"+" "+"name"+"="+"value"+";"
In python this is a lot simpler, but sometimes harder to read because the type of a variable isn't as obvious:
"name"+"="+"value"
You can also put as many spaces as you want between the individual parts. In java you can also add a keyword like public, private or static in front of your variable. private and public define the access you have on the variable if you have the class stored somewhere else as an object.
When you use static then every single instantiation(every individual object that is created in your program) of your class will share this variable. If it is changed in one place, the changed version will be accessible everywhere else, while the old state of the variable is removed.

If you already have the variable and want to change it you can do it on the same way without the type definition and the keywords(because they were already specified at the creation of the variable).
You can see some of the typical ways of creating a variable in the image above(which is written in java).
When you want to create an object you can do that by using the constructor. The constructor may need additional variables(called arguments). This is defined by the creator of the object and is accessible in the API(All Programmers Interface) of the library you are using.
java:
"new"+"type of the variable"+"("+"arguments"+");"
python:
"type of the variable"+"("+"arguments"+")"

I'll tell you how you can store multiple variables of the same type with arrays or Lists in #1.5 and #1.6.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 59698.94
ETH 2303.28
USDT 1.00
SBD 2.51