Now we're going to get into writing some actual Java programs. To get started what we need to do is talk about the types of data that you can use and manipulate in Java programs. We're going to start from the beginning with the data types that are built-in to the Java language. So, what is a data type? Well, it's a very well-defined concept. A data type is a set of values and a set of operations on those values. Right now in this table we'll look briefly at the built-in data types for the Java language. So, there's a type called char, and that's the set of values as the characters that I'll say we type on the keyboard. So like A or @ sign or one, two, three, four, any character that you can type is of type char. Now those are the values, the operations that you can do on those values, well, you can compare characters and we do that to put words in order and other things like that, we'll see. One thing that we do with characters is we put them together to make a type of data called string. A string is a sequence of characters. So examples of a string, in a Java we put them in double quotes is "Hello World" or "CS is fun", any string of any sequence of characters within double quotes is a string. The operation that we perform on strings is called concatenation, and we'll look at that operation in detail. Then there's numeric data types, at the beginning we write programs that manipulate numbers, it's the same way in history computers were developed in order to write programs to manipulate numbers for scientific, commercial, and engineering applications. One type of number is an int, so it's just an integer. So, like 17 is an integer, one, two, three, four, five are integers. The operations that we perform on them are this standard arithmetic operations like add, subtract, multiply, and divide. We also work with real numbers in Java, the data type double this represents floating point numbers that you're familiar with from your science, and so that's like 3.145 or 6.022 times 10 to the 23. Floating-point numbers are approximations, are real numbers that usually approximate the numbers that we want, and again, we can perform the operations of add, subtract, multiply, and divide. Then there's a data type called boolean. The set of values for boolean is just truth values, true or false, and the operations that we can perform are and, or, not. We'll talk about each one of these in more detail next, along with programs that use basic operations to manipulate variables that are of these types of data. So, everyone has to know the answer to this question on any exam that I write, what is a data type? It's a set of values and a set of operations on those values. You really internalize and understand this definition you go a lot further in programming than if you don't. So, now in terms of manipulating data, we have some basic definitions that appear in all our programs. So first thing is a thing called a variable, that's a name that refers to a value, so in this case, a and b are variables. This is a short program that adds two numbers. Then there's a thing called a literal, that's a programming language representation of a value, so for an integer literal is a string of digits one to nine and zero to nine. Then there's a thing called a declaration statement, that associates a variable with a type. So these two declaration statements say that a and b are of type int. So the set of values that they can take on is the integers and the set of operations you can perform on are add, subtract, multiply, and divide. The other kind of statement that we're going to use for most of the programs today is called an assignment statement, and what that does is associate a value with a variable. So this one associates the value one, two, three, four with a and the second one associates the value 99 with b. So, then this last one you can actually combine a declaration and an assignment statement. So, let's look at a really simple example of a complete program that uses all of these concepts. So, what happens is that we have two variables a and b, one, two, three, four, and 99 that we set up like on the previous slide and they're both int values, and then this is a program that exchanges the values of a and b. Usually at the beginning to understand what our programs are doing we write a table called a trace. This just gives the variable values after each statement. So, we start out with none of the variables declared, we say int a equals one, two, three, four, then one, two, three, four is associated with a, that's what the table shows. B equals 99, 99 is associated with b, that's what the table says. Then t equals a takes the value that's one, two, three, four, and associates that with t, so after that statement a t is one, two, three, four. Now the assignment statement is not equals like mathematical equality, it really says associate the value with the variable, which is different. So now we say a equals b, so we find the value associated with b that's 99, and we can see that from the trace and then we associate that with a. Then b equals t then we have the one, two, three, four associated with t and then we give that value to b. So that trace of these five statements tells us what that program does. So, the question is what does this program do? Actually, if you type in this program and run it, well, nothing's going to happen, there's really no way to tell what it did. We need some output for that, so we have to put a print line like for "Hello World", and we'll talk about that in just a second. But what the program does is exchange the values. So in order to provide that output so that we see it when we run the program, that's what we use strings for. Again, the string data type, the values are sequences of characters in between quotes., our operation is concatenated and we use the plus sign to indicate that we want to concatenate strings, and so, if we write an expression using the operation, if we take two strings and put a plus sign between them, then it just puts the second one after the first, it makes a new string that contains all the characters in the first string followed by all the characters in the second string. It doesn't matter any string at all, so this is a string one concatenated with the string space two space, concatenated with the string one and then you get one space two space one and like that. So, concatenation operation, so you have to pay a little attention because the meaning of each character depends on the context. If a plus is within quotes it means the character plus, if it's not within quotes it means the concatenation operation. So, in this first case we get the plus character and the second case we just get a bigger, a longer string of digits. So again, the interpretation depends on the context and I just gave that one example where two of the pluses are operators and the other one is a character and this is another one with spaces. If the spaces are outside the quotes, Java doesn't care about them, you can put any number you want, if they're inside the quotes they're part of the string and they're significant. So, that's computing with strings. Why do we compute with strings? Well, we want to put together output that we print on our terminal window so that we can see what our program did. That's why we talk about strings right away, we use them for input and output. But you can also write programs that compete with strings. Here's a program and the goal of this program is to print the heights of the divisions of a ruler. So, the little line under the two is, say, four units high. The ones at the half inches are three units high. The ones at the quarter inches are two units high. The ones at the eighth inches are one units high. If you write out the string, 121312141213121, now we want to write a program that does that. This is a really simple program that accomplishes that goal just by using concatenation to build up a big string. Now, all the operations are concatenations and again, we can write a trace to see what this thing does. So, the first thing sets ruler one to just the one string, and then ruler two is that one plus space two space plus another one, and then the next one takes that string and then puts a three and then another copy of that string, and then ruler four does it to get four. So, simple four-line program and then if we want to see what string we got, all we have to use is print line just like we did for "Hello world." With "Hello World", we had the string "hello world" now we've got the string "ruler4," and so when we run this program, we get this string printed out and we've got a program that computed something interesting just with string operations. So we type Java ruler, we get that string up. Computing with strings. Okay. So, we need input and output and we use it for the result of our computations. The thing is especially at the beginning, very often, our programs are working with numbers and we're working with strings. So for output, we use system.out.println to print the string, and even if you print numbers, Java will automatically convert the numbers to strings to output them. For input, if you type Java in a program name, any strings you type after the program name are available to the program and we'll look at how to get information into our program as strings from the command line. So those are the two ways at the beginning that we're going to use input and output to get information to our program and to learn what it did. So, those things that we type, or strings because we're typing them, how does Java know if we type 1234 whether we mean the string 1234 or the integer 1,234? Well, there's a system method that converts that and we'll look at the use of that in just a second. Before very long, we're going to look at lots of different options for input and output and also lots of different options for converting types, but we need to talk about these few just to get started on the rest of the programs that we're going to look at today. So, as I said, when we had this exchange values program, we don't know what it did unless we print out the results. So if we add these two print statements to this program, again, a and b are integers, they're data of type int, they're integers, but Java will automatically convert them to string for output. So if we type Java exchange, and the other thing is that this program takes from the command line two arguments, arg zero and args one. They come in as strings, we call parseInt to get them converted to numbers, and that's what we set to a and b. So they start as strings, they become numbers, then the code exchanges them, and then when we print them, the result gets converted back from numbers to strings. So, what does this program do? It reads two integers from the command line and then prints them in the opposite order. It'll actually read strings, converts them to integers, exchanges them, prints them out in the opposite order. So just looking at this program now, it's a template for lots of programs that we can write where we provide parameters as strings that easily convert to numbers, process the numbers, and then print them out with the system automatically converting them back to a string. So now, we can look at computing with integers. So the int data type, again, what's the set of values? In a computer, it's got to be finite and we can talk about the details but in Java, the set of values for an int are the integers between minus two to the 31st and two to the 31st minus one. This range has to do with the way the integers are represented within the computer and it's useful for typical applications. Again, literals are just strings of digits that represent integers in this range. Now operations; add, subtract, multiply, divide, and remainder. Again, there's only two to the 32 different int values. So they're not quite the same as real integers which could be bigger than that but for many purposes, they're useful enough. Then we can do operations like add two integers, subtract them, multiply them. If we divide two integers, then the result we get drop the fractional part so that the result is an integer. If you take the remainder, you get the remainder of the division or the mod operation, and if you divide by zero, that's an error. You'll get a run time error from the Java system. We can write longer expressions and there's a concept called precedence. If you write an expression that's got a couple of operations, which one's going to get performed first? Well, multiplication and division are going to happen before addition and subtraction. So in the first case, the multiplication gets done. So that's 15. They subtracted two, that's 13. In the second case, the division happens first and it's two, drop the fractional part, and then add the three to get five. If you write a couple of operators that have the same precedence, Java's expressions or so called left associative which means the one on the left gets done first. But it's better style to use parentheses to express your intent as a programmer and not try to trick the person reading the code. So, we're going to see lots of examples of programs that manipulate integers and we're going to use them for math calculations and in other applications. So this is an example of computing with integers. Again, just to make sure that everybody is thinking carefully about the difference between integers and strings and type conversion. So, take a look at this program for a minute and try to see what it does. Again, one thing that I didn't mention is that if you take a string and you add it to an integer using the plus operation, Java figures out that really what you meant to do was convert that integer to a string and then go ahead and concatenate. That's another automatic conversion that Java does, convert the int values to string for concatenation. Then with that piece of knowledge, you can see what this program does is it takes two arguments from the command line, then it performs the basic plus, times, divide, mod, int operation, the sum, the product, the quotient, the remainder. Those are then computed and they're a part of Java's int datatype. So, they're integers, those values, and then it prints out a string that represents the expression that it computed. It takes the int a, and so in this case, it's going to be 5 and then it concatenates that with plus space plus space and then puts the b and then space equals space and then puts the sum and it does that for each of the four operations. So, you get a little table defining the result of Java's computation for these operations. This is a nice program to make sure that you understand what's going on in terms of type conversion and integer and string operations. Now that's just the computing the remainder for you. Okay. Next, we're going to look at a datatype for computing with floating point numbers, the double datatype. So, again, the values for double datatype are floating point numbers or scientific notation. So this one is 6.022 times 10 to the 23rd over 3.14159 and like that. Again, the operations are the familiar arithmetic operations. Now it's important to remember that often, typically, double values are approximations to the actual number you might be thinking about. Like there's no double value for pi. Representation within the computer can only have a finite number of values or for square root of 2 or even for one-third. In Java's representation, there's no double value. There's one that's extremely close but it's not exactly equal to one-third and that's something that we have to take into account when we're writing programs that are working with approximations to not real numbers. So, these are the types of expressions that we write with double and again we get the results pretty much that we expect for math computation, the good approximations to the numbers that we want. In the case of double, there's some special values. So, if you say 1 divided by 0 with doubles, there's a special value called infinity, and you can compute with infinity and get the kinds of results that you expect. But there's also not a number. If you take the square root of minus 1, that's not a real number and there's a value not a number that you can work with as well. Those are the basic values and operations for the double datatype. Again, we use this for scientific calculations and we'll see plenty of examples of programs that do scientific calculations with this type of data. There are other built-in types for representing numbers in Java that we don't use too much but sometimes. There's one called short, which is same operations as int but smaller set of values and another one called long, which is same operations but a much larger set of values. There's one called float that is like double but it's less precise. So the reason that we have different numeric types is that it's a trade off between how many bits do we want to use to represent the number. For integers, it's a straight trade-off is what's the range? For real numbers, it's how much precision do you want to get with the number? I guess the range as well. So, without going into a lot of detail, that short is 16 bits. Int and float are 32 and long and double are 64. It's important to be aware of these kinds of restrictions in really knowing what the set of values is because you can get into situations where it's important and we'll talk about a situation like that in a bit. Now with doubles, we have lots of built-in functions that we can use to compute on double values. Actually, a lot of them work for other types as well. So you can get the absolute value, you can get the larger two values or the smaller of two values, you can compute the sine function, or the cosine, or the tangent, or the arc sine and so forth, and it's done in radians but there's functions to convert back and forth to degrees. There's the exponential function. There's the log function, which is the natural log. There is a to the bth power. There's round to the nearest integer. There's random which gives a random number that's between 0 and 1 but less than 1. There's square root. We're going to actually talk about how we might implement the square root function later on. You can get the value of e and pi. So with these kinds of functions, we can write programs that do lots of familiar computations from science and math. One thing that you can do is if you have a calculator, throw it away because you don't need it because you can write Java programs to do anything that you calculator does, and the advantage is the Java program can be long and complex but it's a text file and also you can save it and use it again quite easily the same way that you save all kinds of other information on your computer. So with the math library, we're already past the calculator. So here's just an example. So, if you want to have a polynomial x squared plus bx plus c, then you can find the roots using the quadratic equation. So what are we going to do? Well, we're going to take the coefficients from the command line, that's the b and c, and these are of type double so they're going to be strings but we don't want them to be integers, we want them to be of type double because we're going to do things like take the square root. So, we use Double.parseDouble whereas before we use integer.parse in. So we take the two things from the command line, put them in b and c and then we do the quadratic formula. So first, we compute b squared minus 4c. So, that's going to be a value of type double that we call discriminant. We write the expression b-squared minus four c. A lot of times when we're computing with doubles, and we have constants and an integer constants even, we write 4.0 rather than just four to just make sure that it's a double literal. Then, we take the square root, that's d. Then, there's two roots minus b plus the discriminant over two, and minus b minus the discriminant over two, and then we write them out. These things are double, but again if you're writing it out, Java automatically converts it to a string. So, roots of x squared minus three x plus two are two in one. That factors into the x minus two, x minus one, as you remember from grade school. So, this is x squared minus x minus one are in one roots of that is the golden ratio, x squared plus x plus one, which are complex so we get not a number. If we type something that's not a double, I want to know the roots of x squared plus x minus hello, that's not a number, I get a runtime error saying it's not a number. If I leave off an argument, well, it's not always the case that the error message is crystal-clear telling you what it means, in this case, what it means is I wanted two arguments so you just gave me one. You're certain to get this error message. People get it all the time not really remembering how many arguments are they supposed to type. We'll later on learn exactly what it means, but a lot of times you're going to get error messages you don't know what they mean. You're going to have to study your program to figure it out. I'll be talking about that in a lot more detail later on. So, that's a problem I computes with floating point numbers. Again any math equation now you've got the tools to do the calculation, any kind of calculation that you might want. Maybe a little less familiar is the boolean and data type. It's kind of simple, but it's also kind of fundamental. We'll use it immediately, and then place a more profound role in computing later on. For boolean the values are true and false, the literals are true and false. There's only two in the operations or the logical operations and, or, and not. The operators that we use for these operations are double ampersand for and, double bar for or, and exclamation point for not. They are completely defined by these tables. So, if a is true not a is false, if a is false not a is true. For the other operations, this table gives all four possibilities for a and b, a and b is true when they're both true or false otherwise. A or b is true if either one of them is true, and false only when they're both false. There's other boolean operations, for example there's an operation called XOR. In that one, actually any boolean operation can be expressed in terms of and, or, not. I'll quite take the time enough to go through this, but for any boolean function in this case, exclusive or is false if they're both the same and true if they are different, in this little table is a proof that that's operation is expressed by that boolean expression. We'll get back to this and see other examples later on. Right now, we just want to use really simple examples with that and, or, not and so forth. We're going to use these to control the logic of a program with a flow from one statement to another. They play, boolean operations play an important role in that, and we'll see that very soon. So, for each type of data, there's fundamental operations built into Java that allow us to compare their values, and that's very important in lots of computations. So, these operations, the operands are two expressions of the same type, and the result is a boolean. So, usually that's either true or false. So, just for example, there's the equal equal comparison operator. So, you've got two ints and like two equal equal two, that's true. Two equal equal three is false. Then, there is not equal which is the opposite, and it's less than, less than or equal, greater than, and greater than or equal. So, we can take values from a data type, two values of the same type, then, we can perform these kinds of operations on all our types, the way they're compared as natural or numbers as natural or the numbers on the string is the lexicographic or alphabetical order. So, this is an example of when you might want to use these types of operations in a program, like maybe you want to test in the program that the, for the quadratic equation that the discriminant was positive. So, you might want to know is b squared minus 4ac positive or non-negative or not. So, you just say is a greater than or equal to zero. Now, you have to be a little careful because double values are approximations, but something like that is probably going to be alright. So, you want to know you've got a variable year that gives a year, and you want to know if that's the beginning of a century. So, you test whether when you divide it by 100 you get a remainder of zero or not. Thus again, these things have a value of true or false. Then, in a minute we're going to see how we might compute with those true and false values. Or you have a variable month is between 1-12 and then you can say okay, is it greater than or equal to one and less than or equal to 12, just to check that it's legal. So, there's lots of times when we want to check the values of things and get a true or false result. That's what comparison operators do for us. So, here's an example of computing with booleans. Is a given year a leap year? Well, so the rule is it's definitely a leap year if it's divisible by 400, or if it's divisible by four but not 100. So, that's the rule so, here's how to write a program to implement that rule. Again, we've got leap year.java, its namely a leap year, it's got a mean and now we write our program. So, we're going to take the year from the command line, and it's going to be an integer value. We're going to compute a boolean value that says true if its a leap year and false otherwise. So, it's divisible by four but not 100? Well, we'll say year divided by four, the remainder when you divide by four is that equal to zero, is either true or false, so, we want to be divisible by four so if it's really four that will be true, and year remainder when you just divide by 100 not equal to zero. So, this whole expression now sets leap year to true if it's divisible by four but not by 100 and false otherwise. Well, if it's divisible by 400 then we also want to make it true. So, we say it's either that thing or if the remainder when we divide by 400 is equal to zero. So, then we print out that value, and that's going to be either true or false. Now, we have a program that we can keep on our computer, and anytime we can type and say, okay, "Is 2016 a leap year?" and it says "Yes" and "1993," "No," "1900" "No," but "2000," "Yes." Example of computing with boolean data type.