Core Java Volume I-3.3. Data Types

Source: Internet
Author: User
Tags integer numbers ranges square root java se

3.3. Data Types
Java is a strongly typed language (strongly typed voice). This means , every variable must has a declared type (each variable must declare a type). There is eight primitive types in Java (Java has 8 primitive types). Four of them is integer types; Floatingpoint number types; One is the character type char, used for code units in the Unicodeencoding scheme; and one is a Boolean type for truth values.
3.3.1. Integer Types
The integer types is for numbers without fractional parts (decimal part). Negative values are allowed. Java provides the four integer types shown in Table 3.1.

in the very situations, the int type is the most practical. If you want to represent the number of inhabitants in our planet, you'll need to resort to a long. The byte and short types is mainly intended for specialized applications, such as low-level fi Le handling, or for large arrays if storage space is at a premium.
under Java, the ranges of the integer types do not depend on the machine on which you'll be running the Java cod E (the value range of the integral type is machine independent). This alleviates a major pain for the programmer who wants to move software from one platform to another, or even between O Perating Systems on the sameplatform. In contrast, C and C + + programs use the most efficient an integer type for each processor. As a result, a C program this runs well on a 32-bit processor could exhibit integer overflow on a 16-bit system. Since Java programs must run with the same results on allmachines, the ranges for the various types is fixed.
Long integer numbers has a suffix L (Long integer in L suffix) (for example, 4000000000L). Hexadecimal numbers has a prefix 0x (for example, 0xCAFE). Octal numbers has a prefix 0. For example, 010 is 8. Naturally, this can is confusing, so we recommend against the use of octal constants.
starting with Java 7, you can write numbers in binary, with a prefix 0b (starting with the JAVA7 can represent binary in 0b prefixes). For example, 0b1001 is 9. Also starting with Java 7, you can add underscores to number literals (can be underlined between numbers starting from Java7), such as 1_000_000 (o R 0b1111_0100_0010_0100_0000) to denote one million. The underscores is for human eyes only. The Java compiler simply removes them.
3.3.2. Floating-point Types
The floating-point types denote numbers with fractional parts. The floating-point types is shown in Table 3.2.

the name double refers to the fact that these numbers has twice the precision (double precision is twice times float) o f The float type. (Some people call these double-precision numbers.) Here, the type to choose in most applications is double. The limited precision of float is simply not sufficient for many situations. Seven significant (decimal) digits may is enough to precisely express your annual salary in dollars and cents, but it won ' T is enough for your company president ' s salary. It only makes sense to use float in the rare situations where the slightly faster processing of singleprecision numbers are Important, or when you need to store a large number of them.
Numbers of type float has a suffix f (float type with F suffix) (for example, 3.14F). floating-point numbers without an F suffix (such as 3.14) is always considered to be of type double (no F suffix defaults to double type , the D suffix is optional). You can optionally supply the D-suffix (for example, 3.14D).
All floating-point computations follow the IEEE 754 specification. In particular, there is three special floating-point values to denote overflows and errors:

    • Positive Infinity
    • Negative infinity
    • NaN (not a number)

For example, the result of dividing a positive number by 0 is positive infinity. Computing 0/0 or the square root of a negative number yields NaN.
3.3.3. The char Type
The char type is used to describe individual characters (char types are used to describe a single character). Most commonly, these'll be character constants. For example, ' A ' was a character constant with value 65. It is different from "a" and a string containing a single character. The Unicode code units can be expressed as hexadecimal values, the run from \u0000 to \UFFFF. For example, \u2122 are the trademark symbol and \U03C0 are the Greek letter pi.
besides the \u escape sequences(escape character) that indicate the encoding of Unicode code units, there is SEv eral escape sequences for special characters, as shown in Table 3.3. You can use these escape sequences inside quoted character constants and strings, such as ' \u2122 ' or ' hello\n '. The \u escape sequence (but none of the other escape sequences) can even be used outside quoted character constants and St Rings. For example,

 Public Static void Main (string\u005b\u005d args)

is perfectly legal-\u005b and \u005d were the encodings for [and].

to fully understand the char type, you have to know about the Unicode encoding scheme. Unicode is invented to overcome the limitations of traditional character encoding schemes. Before Unicode, there were many different standards:ascii in the states, ISO 8859-1
For Western European languages, KOI-8 for Russian, GB18030 and BIG-5 for Chinese, and so on. This causes the problems. A particular code value corresponds to different letters in the different encoding schemes. Moreover, the encodings for languages with large character sets has variable length:some common characters is encoded a S single bytes, others require, and more bytes.
Unicode is designed to solve these problems. When the unification effort started in the 1980s, a fixed 2-byte code is more than sufficient to encode all characters us Ed in all languages in the world, with a spare for future expansion-or so everyone thought at the time. In 1991, Unicode 1.0 is released, using slightly less than half of the available 65,536 code values. Java is designed from the ground-to-use 16-bit Unicode characters, which is a major advance over other programming LA Nguages that used 8-bit characters.
Unfortunately, over time, the inevitable happened. Unicode grew beyond 65,536 characters, primarily due to the addition of a very large set of ideographs used for Chinese, J Apanese, and Korean. Now, the 16-bit char type was insufficient to describe all Unicode characters.
We need a bit of terminology to explain about this problem are resolved in Java, beginning with Java SE 5.0. A code point is a code value, which is associated with a character in an encoding scheme. In the Unicode standard, code points is written in hexadecimal and prefixed with u+, such as u+0041 for the code point of The Latin letter A. Unicode have code points that is grouped into the code planes. The first code plane, called the basic Multilingual plane, consists of the "classic" Unicode characters with code points U +0000 to U+FFFF. Sixteen additional planes, with code points u+10000 to U+10FFFF, hold the supplementary characters.
The UTF-16 encoding represents all Unicode code points in a variable-length code. The characters in the basic multilingual plane is represented as 16-bit values, called code units. The supplementary characters is encoded as consecutive pairs of code units. Each of the values in such an encoding pair falls to a range of 2048 unused values of the basic multilingual plane, call Ed the Surrogates area (u+d800 to U+DBFF for the first code unit, U+DC00 to U+DFFF for the second code unit). Rather clever, because you can immediately tell whether a code unit encodes a single character or it's the first or second part of a supplementary character. For example, the mathematical symbol for the set of integers ZZ have code point u+1d56b and are encoded by the-the-code unit S u+d835 and u+dd6b. (see http://en.wikipedia.org/wiki/UTF-16 for a description of the encoding algorithm.)
in Java, the char type describes a code unit in the UTF-16 encoding.
Our strong recommendation isn't to use the char type in your programs unless you are actually manipulating UTF-16 cod E units. You is almost always better off treating strings as abstract data types.
3.3.4. The Boolean Type
The Boolean type has two values, false and True (a Boolean type has two value, respectively, false and True). It is used for evaluating logical conditions. You cannot convert between integers and Boolean values.

Core Java Volume I-3.3. Data Types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.