Essential Java Programming - Using Arrays
Essential Java Programming
The course is part of this learning path
This training course takes you through many of the essential Java programming features. We’ll review in depth features like Language Statements, Using Strings, Subclasses, Fields and Variables, Using Arrays, Java Packages and Visibility, and much more.
- Understand key Java language statements and keywords
- Be able to develop confidently with Strings
- Be able to implement specialization using subclasses
- Work with fields and variables
- Implement Arrays to store multiple values in a single variable
- Learn how to structure your Java code using Packages and Visibility
- A basic understanding of software development
- A basic understanding of the software development life cycle
- Software Engineers interested in learning Java to develop applications
- Software Architects interested in learning Java to design applications
- Anyone interested in basic Java application development and associated tooling
- Anyone interested in understanding the basics of the Java SDK
- Okay, welcome back. In this lecture we'll familiarize yourself with arrays and how to use them to maintain multiple values. We'll cover items such as, declaring an array reference, allocating an array, initializing the entries in an array and writing methods with a variable number of arguments. For starters arrays are true objects. They can hold primitive values or object references. Use of arrays is fail fast meaning that if your program attempts to index an array outside of its allocated bounds an array index out of bounds exception is immediately thrown. Array indexes are zero based. A program will fully allocate an array in three logical steps. The java language allows the syntax that can perform these steps in fewer statements. We've illustrated the three steps separately here for clarity. First, the array reference type is declared. At this point we may have an array of type string yet we haven't yet allocated the space for the end number of references. In the second stage we allocate the space for the array. Remember, that this is an array of references not an array of objects. At the end of this stage we have in references of the specific type but no objects of that type yet. And the third stage is where each of the references is set to point to a valid object instance of that type. As mentioned arrays are full fledged objects in java with special syntax built into the language to support the creation and access to arrays. Arrays are thus type safe.
The most common thing invoked on arrays is the length attribute. Regardless of whether the references within the array are valid, this attribute will always return the number of slots in the array. Note, that you cannot change the length of an array. Indexing is done using integer perimeters. Java will automatically check that you are not attempting to index past the end of the array. Multidimensional arrays are essentially arrays of arrays. They do not have to be rectangular as the example above illustrates. While an interesting idea in most cases multidimensional arrays are not used very often. To maintain more complex sets of data we generally use one of the collection based classes. When working with arrays, copying arrays is a very common operation. Rather than writing your own implementation you should always use the System.arraycopy method as it is much faster than any code you could possibly write. Even with a jet. Try to convince yourself. Array copy is extremely useful for reallocating the size of an array. Prior to Java 5 the implementation of methods that needed a variable number of arguments was done using arrays. However, with the introduction of Java 5 it became possible to make the last parameter in the method signature a variable argument. Variable arguments must be last in the define methods argument list. Be careful how you overload methods that support varargs. And arrays in a variable argument list must be cast to be tight object. Or the compiler will not treat it as a member of the varargs list. Videoing consider the following questions to test yourself on the content that we've just reviewed. Okay the answers to the above questions are. One, arrays are fixed in size once allocated. Two, the length field can be accessed on an array to determine it's size. Three, a multidimensional array can be created but only for one type. And four, the elements within an array are accessed using integer indexing starting at zero. Okay, that completes this lecture, go ahead and close it and we'll see you shortly in the next one.
Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.
Jeremy holds professional certifications for both the AWS and GCP cloud platforms.