Skip to main content

Variables, Data Types and Math Operations

 

In order to do anything interesting in a program, we must have the ability to store and manipulate values. What allows us to do that are what we call variables. Now a variable, simply put, is just named data storage. Now Java is a stronglytyped language. Now what that means is that when we declare a variable like, in this case, we have a variable named data Value, we have to specify the type of that variable, in this case, it's what we call an int, something that can store integers. So, what that means is that the data Value variable can only store things that are compatible with the type int. As we go through the course, we'll talk about this idea of one type being compatible with another. Now when we declare a variable, we can, of course, then assign a value to it, so our variable data Value now holds the value 100. Now when we use variables, we can do it the way we've done here where we declare it, then assign it, or as a matter of convenience, we can declare them and assign them in a single statement. Java is a language that allows you to modify the values stored in a variable, so you can store an initial value in a variable, and throughout the course of your program, you can modify the value that's stored inside that variable. When it comes to naming variables in Java, we follow a combination of rules and conventions. The rules allow us to use letters, numbers, the dollar sign, and the underscore. But by convention, we only use letters and numbers. The rules require that that first character is not a number, but since conventions say that we never use a dollar sign or an underscore, that means that we always end up starting it with a letter. So, some examples are like If I wanted to declare a variable called total, right, TOTAL, right, that's valid. Or grade4, because the four is not the first character, that's a valid variable name. By convention, we follow the naming style known as camel case. What that means is that the first letter of the variable name is a lowercase letter. We start each word after the first with an uppercase letter and all other letters are lowercase. An example would be bankAccountBalance. Alright, the first word is bank, so that starts with a lowercase, second word is account, so we have a capital A, next word is Balance, it's a capital B. Another example, level2Training. First word, level, is all lowercase. We still have the number 2 in there, that's valid, but when we start the next word, training, we use a capital T. So, let's look now at a simple case of using some variables. So, I've got a program stubbed out here with my entry into my main method. I'm going to get a declare variable here called myVar whose type int. We can also call this a local variable just because it's local to the method main, and we'll see some other ways to store information that are not local throughout this course. Here we have this local variable, myVar, and if I go ahead and try to use myVar right now instead of trying to print that out, that would be an error because local variables don't automatically get a value. There's nothing defined stored inside of them, so we don't want to do that. Before we use a variable, we want to make sure and put something inside of it. Alright, so in this case, I have stored the value 50 inside of myVar. So, of course, now if I print that out, I'll get the value 50. Now I can go ahead and declare another variable here, I'll call it another. And you notice that my variables don't have to be all declared at the top. I can declare them anywhere in my flow of logic that I want to. Alright, so my anotherVar has the value 100 stored inside of it. If I print that out, of course, I get 100. I can assign one variable to another. Alright, so I've assigned anotherVar to myVar. What I'm saying is that I'm assigning the value in anotherVar to myVar. So, if I print out myVar, I get the value 100. And one thing that's interesting to note is that if I go out here and I change anotherVar, so I assign it to 200, if I print it out, it has 200 in it, but if I print out myVar, it still only has 100 in it. And basically, we're doing what's called copying by value there. We'll talk a little more shortly about what's happening under the covers that allows it to assign between one variable and another without them staying linked. But the key thing to remember here is that you can declare variables, we can assign values to them, we can modify our variables, and we can assign values from one variable to another.

Comments

Popular posts from this blog

Difference Between Programming and Coding

What exactly is the difference between programming and coding?  The other day, one of my friends who is not from a computer science background, asked me this question. Even after learning many different programming languages and doing several projects, I could not answer him correctly. I said both are the same. But why do we use two different terms, if both are the same? That led me to some research, and I thought I’d share what I found.   It’s not that complicated.  And their definitions allow for a lot of overlap. We often recognize the terms coding and programming as synonymous because both are often used interchangeably.   what is the difference between programming and coding? Coding is the act of expressing programmatic ideas in computer language. Programming is crafting ideas that can be executed repeatedly by a machine, not necessarily a computing device. While both the terms are synonymous with each other and are often used interchangeably, t...

Types of Recording

Types of Recording: 4 types of recording modes are available Ø   Basic Ø   Desktop Ø   Web Ø   Citrix 2 ways of Recording Ø   Automatic Ø   Manual Basic – generates a full selector for each activity and no container, the resulted automation is slower than one that uses containers and is suitable for single activities. Desktop – suitable for all types of desktop apps and multiple actions; it is faster than the Basic recorder, and generates a container (with the selector of the top-level window) in which activities are enclosed, and partial selectors for each activity. Web – designed for recording in web apps and browsers (supported: Internet Explorer, Google Chrome), generates containers and uses the Simulate Type/Click input method by default. Citrix – used to record virtualized environments (VNC, virtual machines, Citrix, etc.) or SAP, permits only image, text and keyboard automation, and requires explici...