Essential Java Programming - Using Strings
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
- [Instructor] Okay, welcome back. In this lecture, we'll familiarize yourself with working with Strings within Java. We'll cover items such as creating an instance of the String class, testing if two Strings are equal, testing if two Strings are the same object, getting the length of a String, parsing a String into its token components, performing both case-sensitive and -insensitive equality tests, building Strings using the StringBuffer, and comparing the functionality of String, and StringBuffer, and StringBuilder classes. Strings. String is a class in Java. They can be instantiated like any other class type. Common literals are often created as a constant for efficient reuse. The compiler will generate appropriate code to support String concatenation statements. Directly assigning a String is supported by the compiler, and is slightly more efficient than using a constructor. It is equivalent to constructing a new String instance. In addition, the compiler will look for other requests to create the identical String, and will generate the byte code such that all references actually refer to the same instance of String. This is allowable, since an instance of the String class is read-only. Strings are fully fledged objects, with extra support for creation and concatenation built in to the language. Strings are immutable, so when you concatenate two Strings, Java will give you a new object with the value of the two Strings as one. You will see that most classes in Java support Strings in special ways. All classes provide a mechanism to convert a class instance to a String, and many classes allow instances of the class to be created from Strings where appropriate. The String class can inherently support any language that has a Unicode representation, including such languages as Japanese and simplified Chinese. String methods. The String class provides many useful methods, as seen here.
Since Strings are objects, they have a number of very useful methods. Now, be sure to use the equals method, not the equality operator, when comparing two Strings. Probably the most commonly used methods are the equals and length methods. You will notice that there are no methods to write to the contents stored in the String object instance. It is important to understand the results of the equality operator, the double equals sign, versus the equals method of objects. The equality operator tests to see if two object references are pointing to the same identical object, whereas the equals method compares the contents of two objects and returns true or false depending on the contents. In almost all cases, you probably want to use the equals method, not the equality operator. The StringBuffer class is a mutable String. It can be used to build up a String value, and is converted to a String when done. Because Strings are immutable, you can create a lot of smaller String objects when concatenating Strings, and this incurs a performance penalty at runtime. The StringBuffer class provides a high-efficiency solution to this problem. In addition to most of the methods on the String class, the StringBuffer class contains methods to modify the contents without creating new objects. Let's quickly review the performance characteristics associated with each of the String-related types, String, StringBuffer, and StringBuilder. String. String operations are relatively costly, especially when performing concatenations.
Each concatenation creates a new String object to represent the newly concatenated String. A series of concatenations will cause a large number of objects to be created. StringBuffer. StringBuffer uses a single object to support the growing String. Much greater efficiencies are achieved if you are modifying character Strings. StringBuilder. StringBuilder increases these efficiencies even more. It has the same operations as the StringBuffer, but they are unsynchronized, and as such reduces the overhead since there is no thread synchronization. StringBuilder should be viewed as the default for use, unless thread synchronization is needed. Java 8 introduced the StringJoiner class, which provides developers with an easy way to create a String that consists of a delimited list of character values. When taking a closer look at the memory consumption of a typical Java application, you will probably discover that around 25% of the memory used by the application is consumed by a String object. Each of these String objects is backed by an array of characters. In order to be able to support all Unicode characters, two bytes are used to store each character in the array. When, in turn, you look at the actual characters that are being used by these Strings, you will also see that the majority of characters used are part of the ISO-8859-1 standard. In other words, we are only using characters that could be represented by a single byte. As a matter of fact, the majority of Java applications only use characters from this ISO standard. So, even though each one of these characters could be stored in a single byte, Java has always allocated two bytes for each character used. In Java 9, the implementation of the String class has been changed. Instead of storing the internal state of the instance in a character array, the runtime will try to store each character in a byte array. When the value cannot be stored in a byte array, the runtime will automatically use UTF-16 encoding, storing the value in a char array. Okay, and consider the following questions to test yourself on the content that we have just reviewed. The answers to the above questions are, one. The String class contains a large number of useful String manipulation methods. And two. A StringBuffer is more limited in terms of functionality, but supports updating the String content without creating new instances. A StringBuilder is not synchronized, and is therefore faster and more efficient than a StringBuffer. 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.