Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
Java gives you a couple of ways of managing ordered collections of data in memory. The simplest approach syntactically is to use simple arrays. Simple arrays are very easy to code, but they're not particularly flexible. There are couple things to know about simple arrays. First all of the items in an array must be of the same type, so you can create an array of integers or create an array of strings, but you can't create an array that contains both. Also, when you declare an array it's always of a fixed size. You set the size when you declare it and you can't change the size at runtime.
If you want to use resizable arrays you want to take a look at the class array list which I'll describe in a separate video. There are few different syntax styles that you can use to declare and initialize an array. I'm working in an empty main method in a project named simple arrays. Here is one style of syntax. Start with the data type of the array, followed by a pair of brackets, then assign, the array name, put in an equals operator, and then initialize the array using the new keyword, the data type again and the brackets again but this time the size.
So this is an array of three integers. Now, I'll add up a little bit of looping code. I'll type in for and press Ctrl+ Spacebar and I'll choose iterate over array, I'm iterating over the a1 array, Eclipse figured that out for me. And within the for loop I'll output System.out.println and I'll output the value of a1, bracket, i, bracket) meaning the item at position i. I'll save and run the application and the output is three zeros.
Now, there is a variation on this syntax that's possible and it's completely a matter of preference which one you use. I am going to select and copy this block of code and then paste it in. I'll change the name of the second array to a2. I'll make the change in three places, here, here and here. And then I'm going to take the brackets and move them from after the data type and paste them after the array name. This variation does exactly the same thing as the first version. It creates an array of three items and the items will have their default value.
Integer is just like all other primitive numeric types, have the default value of zero. And so when I print these out once again I should get three zeros, and when I run the application, I get the three items in array a1 and the three items in array a2. The third style syntax for simple arrays is to initialize the values in the declaration statement. I've used this version of the syntax previously in the course. Start off with your data type, once again you can place the brackets either after the data type or after the array name and then put in a pair of curly braces and then declare your list of items as a comma delimited list.
So I'll add in a list of 3, 6, 9. The number of items in the array and their values are determined by that list. As with the previous declarations, once the array is set to have a particular number of items it will always have that number of items, you can't change it at runtime. I'll make a copy of one of these for loops, and I'll copy and paste it down here. I'll change the references here from a2 to a3. I'll run the application and there is the result.
The last array has values of 3, 6 and 9. Once you've declared an array, you can address the items in the array using array syntax, where you referred to the index of an item using zero-based numbering wrapped inside brackets. So for example, I could say System.out. println, "The value of the first item is" and then I can address a3, open bracket, 0.
I'll save and run the application, and I see that the value of the first item is three. And as I've shown in previous videos, if I try to address an array item and I get it wrong, if I put in an index to an item that does not exist I'll generate an exception a runtime error. So that's the basics of simple arrays, once again the syntax can vary a little bit, you can place these initial brackets either after the data type or after the array name and you can either use the new keyword and an explicit number of items in the array, or you can use a comma delimited list wrapped in braces.
Either way the array must contain items all of the same data type and it is not resizable at runtime.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.