Saturday, March 3, 2018

Basic Data types and Variables in java - Continued...

Before we see this post let see what all we have learned in previous posts of this course.

Okay, so last week, we discussed data structures and variables, and of course I mentioned the different data types like the Integer, Float and Double. We also looked at some mathematical operators, and we discussed how to perform some simple calculations. (I really hope you practiced, like I advised. Really, do it! It’ll stick!).

Of course the data types I mentioned are only used for numerical data, and those aren’t even all the numerical data types. There are quite a few more, but these three are the ones you’ll use the most, especially when you’re still learning how to program. While these numerical data types are useful, they won’t do much good if you want to work with letters or text. Don’t worry, there are variables that are made specifically for text purposes. They are known as String and Char variables, and I will take them as one.
As an extension, I’ll discuss another data type known as the Boolean (don’t bother too much about what it is, I’ll explain it later).

Now before we begin, I’ll have to deal with something that I should have mentioned in the last post (I’m quite sorry about this. This was kind of an afterthought). So we before we talk on Strings, we’ll need to deal with…

Operator Precedence 

You’ll probably need to reopen our last project for this. Okay, now if you had practiced the mathematical operators like I asked (if you didn’t, well, you still need to pay attention), you may have noticed that some of the answers that the system was returning seemed wrong. Let me use an example, say I delete everything in the project except the print statement, and add this:
int answer = 100 - 75 + 23;
Now, using BODMAS method, where the addition takes precedence over the subtraction, you should get 2, right? But if you run it, you get 48, and then you’re like “How did the computer get it wrong?”. Well the answer is, the computer didn’t get it wrong, at least not technically. You see if you were to calculate without the BODMAS method, from left to right, you would get 48.
The computer doesn’t obey the rule of BODMAS, it has its own form of operator precedence, where it treats the subtraction and addition operator as equals, and therefore will only be computed in the order they’re given. If you want to get the correct answer, you have to put the operator where you want precedence in a bracket, like this:
int answer = 100 – (75 + 23);
…that way, it does the addition first. Therefore, if you want the one part of the equation to take precedence of another just put it in bracket.
Now change your code to this:
int answer = 100 + 75 / 20;
So using the logic I gave, the computer should do the addition first, then the division, which should give the correct answer, right? Well, when we run it, we get… disappointed. Well actually, we get 103, when we should have gotten 8. And then we ask the computer, “Hey, what gives?!”. Well, do you remember when I said that the computer has its own rules of operator precedence, well:
RULE 1: Multiplication and division are treated as equals, and they have the highest precedence.
RULE 2: Addition and subtraction are also treated as equals, but they have lower precedence.
Therefore, the computer’s logic made it to start from the division first, before doing the addition. To fix this, you have to put the addition in brackets, like so:
int answer = (100 + 75) / 20;
Run it, you should get your answer. Try practicing this with other operators, more values, and even try changing the positions of the brackets (just for fun). You could also try saving them into different variables as practice. And next time, when you want to run a code and it doesn’t seem to be giving you the right output, remember that operator precedence is very important.
Now that that’s over…

String Variable (Though it is class in java )

Now like I said earlier, there are variables used to store text. They can store anything from one character to an array of characters. The ‘char’ variable stores only one character, while the ‘String’ stores more than one character. It’s quite likely that you’ll use the String variable more though, so more emphasis will be made on that.
You can make a new project for this, or you can do what I did and make a new class file in out ‘tutorialproject’. Just go to projects, and go to the “TutorialProject’ project. Click the plus signs until you see the package, right click it, and you should see this drop down list. Hover your pointer over ‘New’ and click ‘Java Main Class’, and this should pop up:

Change the ‘NewMain’ in the class name textbox to ‘StringVar’ and click ‘Finish’. Keep in mind that if you want to run this file after doing this, make sure you use ‘Shift + F6’ as just using F6 will run the first main class you made.
Now add this to your main method:

Note the keyword String, showing that the data type is a collection of individual characters (these characters can be numbers or letters). Also note that the word has a capital ‘S’, unlike ‘int’ or ‘float’. I don’t know why this is, but just remember it. You then give the variable names (firstName and lastName), and input your text (I’m using my name as an example, but you can use yours). Also note that the text we inputted is written in double quotation marks. Like the print statement, this is to distinguish the String from regular code.

If you run it, you should most definitely get this as your output:

If you didn’t use my name, then you won’t get this exactly.
Now, you notice that there’s no space between the first and last name in your output. This is because in programming, space is also a character, and so any point that requires spacing must be specified. Therefore, all you have to do is add this “ ” in between the two variables, like this:
System.out.println("My name is "+ firstName + " " + lastName);
The empty quotes just represent the space character, and is basically a way of telling the computer that there should be a space there. If you run that now, you should see something much better, which will be this:

I’m sure there’s really nothing much to say about this anymore, so we’ll move on.

Char Variable 

Under the string variables we’ve made, add these lines of code:

Note that char has a small letter ‘c’, and also notice the single quotes that the character is in. You cannot have a character in double quotes, or it will show an error. Also, you cannot save more than one character into a char variable. You can save a character in a string variable, though, but it has to be in double quotes.
In short, it’s:
char firstInitial = 'V';
char firstInitial = “V”;
String firstInitial = “V”;
String firstInitial = 'V';
Also, the dot I put between the variables means that there will be a dot between them in the output, not a space. If you run it, your result should be this:

A Quick Look on Boolean 

Don’t worry, despite the complicated look of the word, it is actually quite basic, and in fact is something we all learn when we’re younger, though not by that name. Boolean is a data type that stores only two values: true and false. They are mostly used in logical operations, something that we’ll discuss in a later post. I just wanted to give you a heads up on it.
If you want to try it out, though, type this out on the coding window (you can comment out the other things we’ve done first, though).
Boolean something = true;
Run it, and your result should be true.
Okay, we’re going to wrap it up here. Next time we meet; we’re going to talk more about other forms of data structures. See you then.

If you have any issue in understanding this , Leave us a comment , will be happy to help 

Thanks for reading


Post a Comment