01 Data in Java


[MUSIC]. We've come to the last section of the course. this at this stage we 're going to talk about how higher level languages like Java are implemented in computer systems. And we're going to leverage the knowledge we have about how things are done using C. what we focused on as our language so far. So we're going to start that process by going back to the very beginning and sort of replaying the course at a much faster pace. And not just talk about how data representations, pointers, and references procedures or methods in the case of Java are represented in a, in this higher level language. And we're also going to talk a little bit about virtual machines, a special runtime system we use for languages like java which are interpretted and not compiled directly. So we'll get, as I said we're going to do this really quick this time. because we have a lot more background to base things on now. So let's start turning out attention to Java. Before we do that just to make a meta point about this this lecture, is none of things we're going to talk about here are guaranteed by Java. What we're doing here is just trying to understand one implementation of a higher level language so we can get a sense for what the basic underlying concepts are. useful ways to think about our programs and what is going on underneath, but none of, none of what you hear in this set of videos. In this section should you take to be the gospel truth about a particular implementation. Let's begin by looking at how data is represented in Java. integers, floats doubles, all those things all those numbers that we were looking at before in the beginning of the course and see or represented exactly the same way in Java. there's no differences there. And yes a Java has pointers as well, however, there are some differences there. in Java pointers are referred to as references. And there not quite as general as a, C pointers are. remember with C we could point to any byte in memory, any arbitrary address. We'll see that in Java our references are always pointing to the start of an object, the start of a data structure. So not quite as general as C pointers. Just like in C, null is typically represented as a 0. and we're going to review characters and strings, arrays and objects. So let's start with looking at characters and strings in Java. the first thing to to note is that the character set that a Java uses has two bites per character rather then one. Rather then using the Latin alphabet focused ASCII encoding. we use two byte unit code characters which allow us to represent many more the world's alphabets. So it's much more general modern computer code for all the character sets that are out there. So the other difference is that unlike in C where strings are bounded by a special byte that has a null or a zero in it as in this example here. You'll notice the strings ends when we hit the zero. We don't know in advance how long the string is going to be. We often have to read the entire string. And keep checking every byte as we read it to know when we get to the end. when we get to that zero. On the other hand, in Java, the first four bytes are an integer that has the length of the string represented as as a number. in this case the number six for the six pairs of Unicode bytes. for the six characters in CSE351. Okay, so, a, with a, with java at the very beginning we know the length of the string already. That first thing we read is an integer that tells us how long it is and how far we'll need to go. The same approach carries through into arrays. you'll notice that arrays in Java have an integer at the very beginning that tells us how many elements there are in the array. Another difference in arrays in Java, is that they're already initialized to 0. and unlike in C, where the array is placed at some locations in memory and it's starting values are whatever was there. Whatever bits were currently at those memory locations. instead in Java there's a special effort made to put zeroes there to start. So that we know that whenever we read a value for the first time, it will in fact, be a zero. That nice first value there, the length of the array is accessed by a special method called length. That we can call on the array and that will return the value of that field. So that anytime we can find out how big this array is. And why is that there, what can that do for us. Well what's, what happens in Java is every time we access an array, every time we index an array. we actually go and take, you know in this case, for example, let's say we had written array three. we will actually go and take that value three and compare it against this one stored at the very first position in the object. And make sure that we're not addressing a location that's out of bounds. an, an element of the array that is actually defined. in this case 3 is definitely okay, it really corresponds to this element right here and we can go ahead and read that. If we had put a 7 here then that would've been somewhere out here. in C and we would've just generated an, an address and read whatever was at that memory location. In Java on the other hand, we compare that seven against the five and say, well we're clearly out of bounds. Let's throw an exception and and cause the program to stop because we're clearly addressing something in an incorrect way. So this is one nice advantage of writing Java programs that we get these extra bound checks for free. That means that there's extra code executing that we didn't bother to write to check that bound but was inserted for us automatically in the Java implementation to do that bounds checking. Okay, let's take a look at data structures in Java, or objects as they're referred to. the difference between structs in C and objects in Java, is that objects can only include primitive data types. Not composite data types. So for example, you'll remember maybe this example that we used before. Here we have a struct that has an integer as its first element, and then an array of three ints as its second element. That's not a primitive structure or primitive data type, that's a composite data type, it actually consists of three integers. And then another primitive data type that is a pointer, okay? when we access these in C, what we can do is allocate some space for for the, the struct. Get a pointer to that region of memory, and then we can refer to it using that arrow notation which says, hey take that pointer, dereference it, offset by the amount that you need to offset to get to the various pieces of the, of the struct and assign a new value to them. And you'll notice that we can not only reference a, but we can reference an element within a, and assign a value to one element of the array. here, we're assigning an address of another struct to that pointer. Okay. So, in memory, this looks like. An integer, followed by three more integers that represent the array, a, and then another four bytes that are the pointer, that is the third element. Let's see what that looks like in Java. You'll notice that in Java, yes, we do have the integer as the first element and the reference to another object a pointer like thing, as the third element, but in the middle we don't have the actual array of three integers, we have a pointer to a new structure that is an array of three integers, so rather than actually representing the three integers there. We actually just represent a pointer to the array. So to, let's just take a look at that map. So here in this case, our memory has our four byte integer at the beginning, our four byte pointer to an array and another four byte reference to another object. So that pointer a is not the actual array, but rather points off to a region of memory where the array is stored. Here are the three ints, but remember they're preceded by the length of the array. So there's an extra integer there at the beginning. That tells us how many elements there are to this array. When we reference them in code, when we reference the struct in code you'll notice theres a slightly different syntax. We don't bother doing malloc to call the memory allocation to get us a space in memory. We use the keyword new which is actually something that does that malloc for us and creates a new space in memory, in this case two new spaces in memory. One for the first level of the struct and then one for one of, another area for the composite data type that makes up that that struct. That extra array that has to be created and it initializes these, this pointer to point to that array. The references to the elements are very similar to what we did in C, but you'll notice that we've changed the notation a little bit. Instead of the arrow, we just used a dot. It's it's a little bit shorter, easier to read, and since we're always using a reference that's to the start of an object. we implicitly do the do reference so we don't bother with that extra step, and can just use the dot notation. Alright, so you've seen a, a little bit about these pointers, let's see, a, what that implications that has. So a pointer in C, again, can point to any memory address. In, in Java though, it can only point to the beginning of an object. only it's first element, not, it cannot point to the middle of it. So what implications does this have? Well you, here's our struct again and you'll notice here we're calling a function with one element of the array that makes up that struct and we can do that with a single expression. We go to that struct, look at its a array, and then the second element of that array. And the address of that element, and can pass that on to the function. So in this case what we're doing is passing a pointer to that function, some fun As an argument and that ar, that argument points to a particular integer in the middle of that array. In Java, we can't do that. Remember our pointers? Our references can only go to the beginning of an object so, we actually have to specify it in two parts. First, we have to say the array Address and then, which element of the array. we, because we can't point to the middle of the array directly. So remember, that's what our struct looks like in our object looks like in memory. These two discontiguous parts, and we cannot generate a pointer to the middle of that array so the way we have to do it is by pointing first to the array by getting that pointer stored at this location, and then providing an index that tells us which element to the array we want, and that index will be compared against the length of the array and checked for us. to make sure that we're not over stepping the bounds.

Wyszukiwarka

Podobne podstrony:
developerWorks Tutorial XML programming in Java (1999)
cracking in java
fireworks in Java
java text FieldPosition
E in T?atures & nescessity
Functional Origins of Religious Concepts Ontological and Strategic Selection in Evolved Minds
You maybe in love Blue Cafe
java
In the?rn
java text CollationElementIterator
java io InvalidClassException
tworzenie aplikacji w jezyku java na platforme android
Ghost in the Shell 2 0 (2008) [720p,BluRay,x264,DTS ES] THORA

więcej podobnych podstron