📄 085-088.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Java Programming: The Basics</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=2//-->
<!--PAGES=085-088//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="082-085.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="088-093.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Wrapper Classes</FONT></H4>
<P>The wrapper classes are used to promote a primitive data type into a reference data type. After this promotion, the reference type permits a series of operations on the primitive data type that are not otherwise possible. Some things can be done only with an instance, such as the addition of an element to a vector. Primitive values cannot be the target of method invocations. All the wrapper types support a method invocation that converts them to a string representation, and all of them can be constructed from a string. All wrapper types support the following statement to check an object for equality of value:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean equals(Object obj)
</PRE>
<!-- END CODE SNIP //-->
<P>Keep in mind that the value of a wrapper object is stored in a private class field. When comparing two strings, use <I>equals</I> and never <I>==</I>. For example:</P>
<!-- CODE SNIP //-->
<PRE>
if (arg.equals(“this is right”) {...
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding code will check the value of the string in <I>arg</I> against the value of “this is right”. The following example checks the value of the <I>arg</I> reference with the compile-time constant string reference. This is probably not what the programmer wants:</P>
<!-- CODE SNIP //-->
<PRE>
if (arg == “this is wrong”) {...
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>Always use the <I>equals</I> method to check value when comparing wrapper instances.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Boolean</B></FONT></P>
<P>The <I>Boolean</I> class promotes the primitive <I>boolean</I> type to a reference type. Construction is overloaded to handle both the primitive <I>boolean</I> type and a string. The string must be equal to “true”, ignoring case, before the <I>Boolean</I> instance will be true. For example:</P>
<!-- CODE SNIP //-->
<PRE>
Boolean b = Boolean(“true”);
Boolean b = Boolean(“True”);
Boolean b = Boolean(“TRuE”);
</PRE>
<!-- END CODE SNIP //-->
<P>The preceding code lines will all lead to <I>b</I> representing true. Two static conversion methods are available for use in the <I>Boolean</I> class:</P>
<!-- CODE SNIP //-->
<PRE>
public static Boolean valueOf(String s) - returns a Boolean instance.
Boolean b = Boolean.valueOf(“true”); // sets b to true
Boolean b = Boolean.valueOf(“yes”); // sets b to false
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>This class is different from <I>getBoolean</I>, which has as its return a primitive <I>boolean</I> type. Note that sections 20.4.9 and 20.4.10 of the Java specification appear to show both of these functions returning the primitive <I>boolean</I>. This is an error in the specification.<HR></FONT>
</BLOCKQUOTE>
<!-- CODE SNIP //-->
<PRE>
public static boolean getBoolean(String name)
boolean b = Boolean.getBoolean(“yes”); // sets b to false
</PRE>
<!-- END CODE SNIP //-->
<P><FONT SIZE="+1"><B>Character</B></FONT></P>
<P>The <I>Character</I> class is a wrapper class for the primitive Java <I>char</I> data type. When embedded in the Java code, the ‘a’ character is surrounded by single quotes, whereas strings use double quotes. Any Unicode character is permitted as an argument for the construction of a <I>Character</I> instance. In addition, there are methods for converting to and from <I>Character</I> and <I>char</I>. For example:</P>
<!-- CODE SNIP //-->
<PRE>
Character aCharacter = new Character(‘[pi]’);
char aChar = ‘©’;
a = new Character(aChar);
aChar = aCharacter.charValue();
</PRE>
<!-- END CODE SNIP //-->
<P>Neither an array of <I>char</I> nor an array of <I>Character</I> constitutes a <I>String</I>. The <I>Character</I> class supports several public static methods that are useful for testing and processing <I>chars</I>. All <I>chars</I> are Unicode, so all comparisons are written for Unicode. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public static boolean isDigit(char ch)
return true if ch is a Unicode digit
</PRE>
<!-- END CODE SNIP //-->
<P>Java version 1.0.1 supports ISO-LATIN-1 (0 through 9), Arabic-Indic, Extended Arabic-Indic, Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, Malayalam, Thai, and Lao digits.
</P>
<P>The <I>Character</I> class has a series of public static <I>Char</I> test facilities that return a <I>boolean</I>. For example:</P>
<!-- CODE //-->
<PRE>
boolean aBoolean;
char aChar = ‘a’;
aBoolean = Character.isDefined(aChar);
aBoolean = Character.isLowerCase(aChar);
aBoolean = Character.isUpperCase(aChar);
aBoolean = Character.isTitleCase(aChar);
aBoolean = Character.isDigit(aChar);
aBoolean = Character.isLetter(aChar);
aBoolean = Character.isLetterOrDigit(aChar);
aBoolean = Character.isJavaLetter(aChar);
aBoolean = Character.isJavaLetterOrDigit(aChar);)
aBoolean = Character.isSpace(aChar);
</PRE>
<!-- END CODE //-->
<P>The <I>Character</I> class also supports a series of <I>char</I> conversions that are public and static. These conversions can be accessed without making a <I>Character</I> instance. For example:</P>
<!-- CODE //-->
<PRE>
aChar = Character.toLowerCase(aChar);
aChar = Character.toUpperCase(aChar);
aChar = Character.toTitleCase(aChar);
// returns an ‘A’, hex representation of 10.
aChar = Character.forDigit(10, 16);
// returns 10, the hex value of ‘A’
int digit = Character.digit(aChar, 16);
int i = Character.MIN_RADIX // i = 2;
int i = Character.MAX_RADIX // i = 36;
char aChar = Character.MIN_VALUE // aChar = ‘\u0000’;
char aChar = Character.MAX_VALUE // aChar = ‘\uffff’;
</PRE>
<!-- END CODE //-->
<P>About the only thing you can do with a <I>Character</I> instance is:</P>
<!-- CODE SNIP //-->
<PRE>
Character aCharacter = new Character(‘[pi]’);
String aString = aCharacter.toString();
aBoolean = aCharacter.equals(aCharacter);
int anInt = aCharacter.hashCode();
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="082-085.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="088-093.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -