Understanding Variables in Programming Languages
Variables play a crucial role in programming by storing information for later use. They are like containers with symbolic names that hold known or unknown quantities. Different programming languages handle variables uniquely based on types like boolean, byte, char, and more. Knowing how to define and use variables correctly is essential for writing effective programs.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let s get started
Variables Control Structures Data Structures Syntax Tools HERE ARE THE 5 BASIC CONCEPTS
WHAT IS A VARIABLE Variables are the backbone of any program, and thus the backbone of any programming language. Generally defined in Computer Programming as a storage location with an associated symbolic name which contains some known or unknown quantity or information (a value) Summing it up a variable is simply a way to store some sort of information for us to use later, we can retrieve this information by referring to a word that describes this information.
Java behaves differently because of the type of the variables. These are the Java variables we will work with: boolean byte char float int long short double String
LETS TRY AN EXAMPLE Let s say you visit a website and the first thing they want to do is get your email and name so that the next time you visit the site will remember you and welcome you back. To do this they would put in a little pop-up text box on the screen and ask you for your name that textbox is going to represent the variable . Let s call the text box yourName that becomes the symbolic name (or word) for your variable. (remember our original definition)
You will type your name into the text box, that information is stored in the variable yourName Next comes the value of the variable Value is defined as an expression which cannot be evaluated (this is what will be returned) any further The program you write using variable would return whatever was typed into that text box. Examples where this is used: Facebook, Twitter, anyplace you go online and sign up/ sign in
Variables have different types Example type in a name as a variable that is recognized as a String Type in your Age that becomes and Integer or Int I as for the year you were born (4 digits) that is stored as a Double Why is this important? LET S GET MORE SPECIFIC
Java has to know what kind of information is being stored in a variable. Tying in Java means the program knows exactly what is being stored in a variable Example: Integer An Integer in Java means you have a number that won t have any decimal places, it will be a whole number like 5, 20, -40, 4000, or -16000. JAVA IS A STRONGLY TYPED LANGUAGE
An int variable holds an integer int num = 2; Unlike double, an int can ONLY hold integers. You cannot store a decimal inside of an int. You can store negative numbers in both ints and doubles however. INT
You want to add two number together lets say the number 22 and the number 3. Java will behave differently depending on the type of the variable that s storing this data. Let me show you what I mean: If you have defined your variables to be of type Integer, then adding 22 and 3 together will result in the Integer 25. Makes perfect sense right? Of course, this is simple Math. But what happens if your variables are not Integers, but are Strings? SIMPLE EXAMPLE
So what if you enter something that isnt an Integer as an Int Example you enter a value of $34.75 Java would give you an error code when you compile and you would have to fix it WHAT HAPPENS IF
A boolean is one of the special Java variables that stores either true or false. I say special because in true and false are actually values that the language recognizes. Here is an example: Boolean x = true; BOOLEAN
A char variable is a variable that holds a single character. A character is a single letter, number, or symbol. For example, 'A' is a character, so is '1' and '&'. Those are all characters. Let's see what a character variable looks like. Char c = g ; capital letters are not the same as lower case letters and vice versa. CHAR
A double variable is one that holds a decimal number. For example: double number = 4.67; DOUBLE
String is not a basic variable it is a little more complicated. String name = John ; A string variable must have quotes around the value in order for it to be a string. Strings can be made up of one letter, one word, whole sentences, or even entire paragraphs. You can also have empty strings. STRING
A String in Java is a different kind of data type and it behaves differently BECAUSE it is a different type of data. When we refer to a String in Java (and in many other programming languages) we are treating the data like it s just a plain old sentence in the English language. A String just represents words (or more specifically letters) all placed in a certain order. That s all the English language (or any language) is, a series of characters/letters placed in a certain order to give meaning to what you re writing down. STRINGS
If you were to have two variables, each defined as Strings and they stored the data 22 and 3 (respectively), what would happen if we added them together? We would get the String: 223 What if the two Stringvariables, aren t storing numbers, we re storing words. So in variable 1 we store the String Hello , and in variable 2 we store the String World . EXAMPLE OF STRINGS
WHY ARE WE TALKING ABOUT ANY OTHER PROGRAMMING LANGUAGES THIS CLASS IS ABOUT JAVA Many of the languages are very similar: C+, C++, Python, Objective C Know one language like Java and the rest can be easily learned