Wednesday, February 28, 2018

Basic data types and Variables in java

Basic data types and variables are the building block of any programming language , and java is also one of them .Welcome back, dear readers. I know what you’re all thinking: “Yeah, finally I wrote my first code! Woo hoo!”. Then eventually you’ll start thinking: “Wait, that’s Javaprogramming? How do use that that to make cool apps and games?”. At least that’s what I thought when I started. I know that doesn’t look like much now, but don’t worry, that was just an intro. We’ll do way more as we go along.

Before We more further , below are the previous post link of the seires
Keep in mind that the more we learn, the harder it gets. I’ll be happy to teach you a lot on programming, but for it to really stick, you’ll need to build on what you learn here for it to stick. By that I mean doing a lot of practicing and experimentation Sometimes the best way to know what to do, is to find out what not to do.
So let’s get started.

VARIABLES

Now, programs work by manipulating data that is being inputted and stored in the computer. This data can be numbers, texts, objects, lists, links to other memory areas and so on. The data is given a name, so that it can be recalled whenever it is needed. The name and data type is called a variable.
You can think of variables like a box. If I have a box, or variable, for storing food, and I call it my Fridge (because it’s obviously a fridge), then I can put stuff like milk, cheese and baloney into my fridge (because variables are variable, they can easily be reassigned new values). So in a way you can say the data type is box, the variable name is Fridge, and the data stored is, let’s say, milk.
Now the standard format for assigning variables in Java (and most other languages) is this:
(Data Type) (Variable Name) = (Data to be stored);
So using this logic, this s how we put stuff in our fridge:
box Fridge = Milk;
(Never forget the semicolons!)

See also Top 10 Interview Questions

Now like most things, the assignment of variables also has its rules (and for all rebels reading this, these rules are NOT made to be broken), especially in regards to naming your variables:
  • RULE 1: Variable names cannot begin with numbers or symbols, letters only. Basically, you can’t have a variable named “2wo” or “$ign” or anything like that. Of course, variables can contain numbers and symbols, just not in the beginning.

  • RULE 2: There can be no spacing in variable names (i.e. nothing like “Number of People). If you want to name a variable that, it’s best to delete the blank spaces (NumberOfPeople) or use the infamous underscore (Number_Of_People_).
  • RULE 3: You can’t use a variable name that also happens to be a java keyword, e.g. if the word “String” is a keyword, you can’t make the variable name “String”. You could call it “FirstString” though.
  • RULE 4: Variable names are very case sensitive i.e. “FirstString” and “firstString” are two completely different variables. Also if you make the mistake of typing “FistString” when assigning the variable, then you type “FirstString” later in the code, it will give an error.
  • RULE 5: This isn’t actually a rule, since you don’t have to do this, but in respect to rule 2, it’s common practice to start the first word with a small letter and the second with capital e.g. “firstString” instead of “FirstString”. Again, they’re both correct, but the first one is just better programming practice.

Now let’s look at some basic data types.



INTEGER VARIABLES-


Launch NetBeans, and open the TutorialProject file from the last tutorial. Without deleting the code, we wrote last time, type in this:


Now, the ‘int’ there is an integer which is a data type that stores whole numbers. To assign a number, go to the next line and type this.

int firstNumber;

        firstNumber = 20;

Or, you could directly assign the variable like this:

int firstNumber = 20;

You’ll see a grey line under the variable name. This isn’t an error message, so don’t worry, it’s just the system remind you that you haven’t called your variable. Place your cursor over the line and you should see the message. If the line just disturbs, don’t worry, we’ll remedy that in a second.
If you hadn’t deleted the print line we made, delete the “Hello World” and type in “firstNumber”, this time without quotes (because this time it’s actually code). If you run it, you will most definitely see this.

Change the number in the code and try it out, you’ll definitely see the exact same number. Now let’s try something a bit different, change your ‘println’ statement to this:
System.out.println(“The first number is” + firstNumber);

The plus means you should join the message in quotes to the value stored in the variable. This is known in programming as a concatenation.
Run your programme and look at the output. Can you see what’s wrong?


You guessed it! There’s no space between the ‘is’ and the ‘20’. This is because the quotation marks read the message as a string, including the spaces. To fix this, put a space after the ‘is’ in the code, so that it’ll look like this:
System.out.println(“The first number is ” + firstNumber);
Run it, and it should look much better.
Now a variable is just a memory space, so it has a limit to what can be assigned to it. An integer value, for example, can only take numbers between −2147483648 and 2147483648. If you want values larger or smaller than that, you can use the double variable.


DOUBLE VARIABLES

Doubles store much larger values than integers with the maximum and minimum at 17 with about 307 zeroes. It is also used to store floating point values (floating point is a fancy word for decimal value) e.g. 8.2, 3.12, 41.5, etc.
Let’s see them at work. Under the integer variable you’ve made, type this down:
double firstDouble = 20.8;
Then in the print function, change “firstNumber” to “firstDouble” and run it:
If you’re curious as to what happens if you put a decimal value in an integer, add .8 to the 20 in the integer variable and put it in the print statement, or better yet, change the double variable to int. Then just run it. You should see an error, and then you’ll know what happens.


FLOAT VARIABLES

These are very similar with doubles, since they also store floating point values (as their name implies). The main difference between the two is that the double has more memory space than the float, and so can store more values (the Double is 64 bits while the Float is 32 bits).
Another difference is the way values are assigned in Floats. Besides the change in data type name (from double to float), there is also an addition of the letter ‘f’ to the back of the value, i.e.
float firstFloat = 20.9f;
Type this in and run it. (It won’t show the f in the result). This should be your output:

Try removing the f and see what happens.
There are other data types like short, long, Boolean, string, char, etc. but for the purpose of this tutorial, we’ll stick with these three. The others will be brought forth and explained as time comes.

ARITHMETIC OPERATORS

Now in the world today, a world of games and social media and entertainment and artificial intelligence, we sometimes forget what computers actually are: oversized calculators! It sounds weird but it’s true. The original computers were built to perform basic calculations, and it’s actually through these calculations that computers have achieved what they could achieve today. You can actually perform any conceivable calculation on your computer, it just depends on if you can speak the computers language.
Fortunately, that’s why we’re here.
Now delete everything except the original integer variable and the print statement. Then add this:
int secondNumber = 28;
int answer = firstNumber + secondNumber;
System.out.println(“The sum total is ” + answer);
Now what we’ve done is store two numbers into two separate variables and tell the system to add them together (+ is the operator for addition). The system then prints out the sum total with the message in quotes. If you run it, we should get this:
If you don’t, either your codes wrong, or your computer just doesn’t know math. (That’s what you get when you by a computer from a guy in a street corner).
You can also add numbers directly into the variable. Add the figure 17 to your variable answer, so that it looks like this.
int answer = firstNumber + secondNumber + 17;
Run it and you should get 65. If you don’t, make sure you get you get your money back from street corner guy!
The other arithmetic operators include minus sign (-) for subtraction, asterisk (*) for multiplication, and backward slash (/) for division. Try them out in your code for fun. You don’t even have to confine yourself to only two variables. Make more and more, and even experiment with double and float.

Next week, we’ll continue on variables, and treat more variable types. We’ll also be sure to do more coding practice.
And speaking of practice, remember that you can only get better through it. It does make perfect.

Thanks for reading
Noeik



0 comments:

Post a Comment