visual basic barcode printing 6: Java.lang The Math Class, Strings, and Wrappers in Java

Painting PDF-417 2d barcode in Java 6: Java.lang The Math Class, Strings, and Wrappers

6: Java.lang The Math Class, Strings, and Wrappers
Decoding PDF-417 2d Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
PDF 417 Maker In Java
Using Barcode generation for Java Control to generate, create PDF 417 image in Java applications.
In the previous section, we saw how the exam might test your understanding of String immutability with code fragments like this:
Reading PDF 417 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Barcode Generator In Java
Using Barcode generation for Java Control to generate, create bar code image in Java applications.
String x = "abc"; x.concat("def"); System.out.println("x = " + x);
Barcode Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
PDF417 Maker In C#.NET
Using Barcode creator for .NET Control to generate, create PDF417 image in .NET applications.
output is "x = abc"
PDF-417 2d Barcode Creator In VS .NET
Using Barcode generation for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
PDF-417 2d Barcode Maker In .NET Framework
Using Barcode maker for VS .NET Control to generate, create PDF 417 image in .NET applications.
Because no new assignment was made, the new String object created with the concat() method was abandoned instantly. We also saw examples like this:
PDF 417 Printer In VB.NET
Using Barcode creator for .NET framework Control to generate, create PDF-417 2d barcode image in Visual Studio .NET applications.
European Article Number 13 Generator In Java
Using Barcode printer for Java Control to generate, create EAN-13 Supplement 5 image in Java applications.
String x = "abc"; x = x.concat("def"); System.out.println("x = " + x);
Make 1D Barcode In Java
Using Barcode creator for Java Control to generate, create Linear Barcode image in Java applications.
Make Barcode In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
// output is "x = abcdef"
Bookland EAN Encoder In Java
Using Barcode encoder for Java Control to generate, create ISBN - 10 image in Java applications.
Print EAN / UCC - 13 In Objective-C
Using Barcode drawer for iPhone Control to generate, create EAN 128 image in iPhone applications.
We got a nice new String out of the deal, but the downside is that the old String abc has been lost in the String pool, thus wasting memory. If we were using a StringBuffer instead of a String, the code would look like this:
Scanning Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
Printing DataMatrix In VS .NET
Using Barcode generation for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
StringBuffer sb = new StringBuffer("abc"); sb.append("def"); System.out.println("sb = " + sb); // output is "sb = abcdef"
Bar Code Drawer In Java
Using Barcode encoder for Android Control to generate, create bar code image in Android applications.
Code-39 Encoder In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create Code 39 Extended image in VS .NET applications.
All of the StringBuffer methods we will discuss operate on the value of the StringBuffer object invoking the method. So a call to sb.append( def ); is actually appending def to itself (StringBuffer sb). In fact, these method calls can be chained to each other for example,
Code 39 Extended Recognizer In .NET Framework
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
Encoding Code39 In None
Using Barcode generator for Online Control to generate, create Code 39 Full ASCII image in Online applications.
StringBuffer sb = new StringBuffer("abc"); sb.append("def").reverse().insert(3, "---"); System.out.println( sb ); // output is
"fed---cba"
The exam will probably test your knowledge of the difference between String and StringBuffer objects. Because StringBuffer objects are changeable, the following code fragment will behave differently than a similar code fragment that uses String objects:
StringBuffer sb = new StringBuffer("abc"); sb.append("def"); System.out.println( sb );
In this case, the output will be
abcdef
Using the String Class (Exam Objective 8.2)
Important Methods in the StringBuffer Class
The following method returns a StringBuffer object with the argument s value appended to the value of the object that invoked the method:
public synchronized StringBuffer append(String s)
As we ve seen earlier, this method will update the value of the object that invoked the method, whether or not the return is assigned to a variable. This method will take many different arguments, boolean, char, double, float, int, long, and others, but the most likely use on the exam will be a String argument for example,
StringBuffer sb = new StringBuffer("set "); sb.append("point"); System.out.println( sb ); // output is "set point"
StringBuffer sb = new StringBuffer("pi = "); sb.append(3.14159f); System.out.println( sb ); // output is
"pi = 3.14159"
public synchronized StringBuffer insert(int offset, String s)
This method returns a StringBuffer object and updates the value of the StringBuffer object that invoked the method call. In both cases, the String passed in to the second argument is inserted into the original StringBuffer starting at the offset location represented by the first argument (the offset is zero-based). Again, other types of data can be passed in through the second argument (boolean, char, double, float, int, long, etc.), but the String argument is the one you re most likely o see:
StringBuffer sb = new StringBuffer("01234567"); sb.insert(4, "---"); System.out.println( sb ); // output is public synchronized StringBuffer reverse()
"0123---4567"
This method returns a StringBuffer object and updates the value of the StringBuffer object that invoked the method call. In both cases, the characters in the StringBuffer
6: Java.lang The Math Class, Strings, and Wrappers
are reversed, the first character becoming the last, the second becoming the second to the last, and so on:
StringBuffer sb = new StringBuffer("A man a plan a canal Panama"); System.out.println( sb ); // output is "amanaP lanac a nalp a nam A"
public String toString()
This method returns the value of the StringBuffer object that invoked the method call as a String:
StringBuffer sb = new StringBuffer("test string"); System.out.println( sb.toString() ); // output is "test string"
That s it for StringBuffers. If you take only one thing away from this section, it s that unlike Strings, StringBuffer objects can be changed.
Many of the exam questions covering this chapter s topics use a tricky bit of Java syntax known as chained methods. A statement with chained methods has the general form:
result = method1().method2().method3();
In theory, any number of methods can be chained in this fashion, although typically you won t see more than three. Here s how to decipher these handy Java shortcuts when you encounter them: 1. Determine what the leftmost method call will return (let s call it x). 2. Use x as the object invoking the second (from the left) method. If there are only two chained methods, the result of the second method call is the expression s result. 3. If there is a third method, the result of the second method call is used to invoke the third method, whose result is the expression s result for example,
String x = "abc"; String y = x.concat("def").toUpperCase().replace('C','x'); //chained methods System.out.println("y = " + y); // result is "ABxDEF"
Let s look at what happened. The literal def was concatenated to abc , creating a temporary, intermediate String (soon to be lost), with the value abcdef . The toUpperCase() method created a new (soon to be lost) temporary String with the value ABCDEF . The replace() method created a final String with the value ABxDEF , and referred y to it.
Using the Math Class (Exam Objective 8.1)
Copyright © OnBarcode.com . All rights reserved.