📄 060-064.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=060-064//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="057-060.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="064-068.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Data Types</FONT></H3>
<P>Data types in Java are divided into two basic categories: reference types and primitive types. The reference types consist of three sub types: class types, interface types, and array types. We defer discussion of interface types and array types until later. The following two sections describe the primitive types and class types.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Primitive Types</FONT></H4>
<P>Java has several primitive types. Primitive types can store primitive values, each of which needs a specific number of bits of storage and has a specific precision.
</P>
<P>The primitive data types of Java are sometimes also called <I>scalar</I> types. There are two kinds of scalar types: <I>boolean</I> and <I>numeric</I>. Boolean types may have two predefined values: <I>true</I> or <I>false</I>. Numeric types can be subclassed into two subtypes: <I>integer</I> or <I>floating point</I>. There are five kinds of integer types and two kinds of real types, float and double. Figure 2.1 shows the taxonomy of the primitive types in Java.</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/02-01.jpg',458,203 )"><IMG SRC="images/02-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-01.jpg',458,203)"><FONT COLOR="#000077"><B>Figure 2.1</B></FONT></A> Taxonomy of primitive types in Java.</P>
<P>Each of the primitive data types is assigned a specific amount of storage. Its value and range are shown in Figure 2.2.
</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/02-02.jpg',454,261 )"><IMG SRC="images/02-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-02.jpg',454,261)"><FONT COLOR="#000077"><B>Figure 2.2</B></FONT></A> Values and ranges for primitive data types.</P>
<P>All variables in Java are held as undefined until set.
</P>
<P>For example, in the following code, a variable, <I>x</I>, was declared but not initialized before being accessed.</P>
<!-- CODE SNIP //-->
<PRE>
public class TrivialApplication {
public static void main(String args[]) {
int x;
System.out.println( x );
}
}
</PRE>
<!-- END CODE SNIP //-->
<P>This is a compile-time error in Java, and the compiler emits the following:
</P>
<!-- CODE SNIP //-->
<PRE>
Error : Variable x may not have been initialized.
TrivialApplication.java line 7 System.out.println( x );
</PRE>
<!-- END CODE SNIP //-->
<P>All integer data types (byte, short, int and long) are signed. All characters in Java are Unicode, an international standard [Unicode]. The character type is defined as a 16-bit unsigned unique code value. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
char c = ‘a’;
</PRE>
<!-- END CODE SNIP //-->
<P>It is possible to assign a numeric literal to a character-typed variable and assign a character literal to an integer variable:
</P>
<!-- CODE SNIP //-->
<PRE>
char theChar = 48;
integer theValue = ‘a’;
</PRE>
<!-- END CODE SNIP //-->
<P>All reals are stored as single-precision floating-point numbers, called <I>float</I>, or double-precision floating-point numbers, called <I>double</I>. Java reals use the IEEE 754-1985 format [IEEE]. The smallest and largest positive nonzero values for floats range from 1.40239846ee-45 to 3.40282347e+38 (<I>Float.MIN_VALUE</I> and <I>Float.MAX_VALUE</I>). The smallest and largest positive nonzero values for doubles range from 4.94065645841246544e-324 to 1.79769313486231570e+308 (<I>Double.MIN_VALUE</I> and <I>Double.MAX_VALUE</I>).</P>
<P>Now for a reality check. Using the Metrowerks CodeWarrior we discover the floating bugs in the Java floating-point libraries:</P>
<!-- CODE //-->
<PRE>
class fp_error {
public static void main(String argv[]) {
float fmin = Float.MIN_VALUE;
float fmax = Float.MAX_VALUE;
double dmin = Double.MIN_VALUE;
double dmax = Double.MAX_VALUE;
String b = “ “;
// bug in FP
System.out.println(fmin + b + fmax +
b + dmin + b + dmax);
}
}
</PRE>
<!-- END CODE //-->
<P>The preceding code outputs the following:
</P>
<!-- CODE SNIP //-->
<PRE>
1.4013e-45 3.40282e+38 2.22507e-308 1.79769e+308
</PRE>
<!-- END CODE SNIP //-->
<P>Please note that round(1.40239846e-45) = 1.4024e-45 and not 1.4013e-45, as CodeWarrior indicated. It has output the following:
</P>
<!-- CODE SNIP //-->
<PRE>
3.40282e+38 instead of 3.40282347e+38
2.22507e-308 instead of 4.94065645841246544e-324
1.79769e+308 instead of 1.79769313486231570e+308
</PRE>
<!-- END CODE SNIP //-->
<P>Note that the 2.22507e-308 is off from 4.94065645841246544e-324 by 16 orders of magnitude. The problem with the output from <I>println</I> is also documented in the language specification (20.10.15). In short, it says that <I>println</I> renders finite values in the same form as the %g format of the printf function in the C programming language, which can lose information because it produces at most six digits after the decimal point.” In fact, our search of the <I>println</I> source code shows that the implementation depends on a call to a <I>String</I> class conversion method called <I>toString</I>. This explains why the error exists in the <I>toString</I> method as well as in the <I>System.out.println</I>.</P>
<P>Finally, the wrong value for the <I>MIN_VALUE</I> variable is Sun’s fault. If you look at the source for <I>Double.java</I> (for the Mac and Windows versions of the 1.0.2 API), it declares <I>MIN_VALUE</I> as 2.225 e-308 instead of 4.94e-324:</P>
<!-- CODE SNIP //-->
<PRE>
public static final double MIN_VALUE = 2.2250738585072014E-308;
</PRE>
<!-- END CODE SNIP //-->
<P>We have verified this with cross-platform testing (Solaris, Windows 95/NT, and MacOS). The HTML docs, however, show the correct value (as do all books we have seen on Java). The version 1.1 source code may correct this problem.
</P>
<P>The following program shows how to code the escape sequences into Java:</P>
<!-- CODE //-->
<PRE>
package stringUtilities;
public interface Char {
char backspace = ‘\b’;
char horizontalTab = ‘\t’;
char newLine = ‘\n’;
char formFeed = ‘\f’;
char carrageReturn = ‘\r’;
char doubleQuote =’\”’;
char singleQuote =’\’’;
char backSlash = ‘\\’;
char maxOctal = ‘\377’;
char minOctal = ‘\000’;
char maxUnicode = ‘\uFFFF’;
char minUnicode = ‘\u0000’;
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="057-060.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="064-068.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 + -