About Java Primitives

     

Java primitives are not objects. They are simple values. They are used to represent single characters , numbers , and the logical values true and false.

Primitive types in Java have a non-trivial benefit over primitives in languages such as C and C++: the ranges of Java primitive types are not dependent on the underlying system. They do not change from system to system as you port your app, because of the Java Virtual Machine. This keeps you from the dangers of overflow problems you can run into in those languages. When you specify an int in C or C++, you don't know what the actual range is; your program will have to use whatever the range of the primitive is on the target platform. For example, in Java, you don't have to worry that on a 16-bit processor, your int will allow only a 16-bit range, but on a more recent processor, 64 bits. This could cause serious problems for your data, which would not be truncated, but would instead wrap .

Java stores its data as different types for different kinds of things. That's why it's called a strongly typed language.

For the numeric number, the lower range is calculated as 2 to the power of the number of bits minus 1. The upper range of the data types is calculated as 2 to the power of the number of bits it can hold minus 1, minus 1. No, that's not a typo. Consider this. How many bits are in a byte? Eight. The Java data type byte is capable of holding 8 bits of data. So now you know the range of possible values. Let's show our work like Mr. Foss made me do endlessly back in the 10th grade. He tortured me, and now you must be tortured too. To determine the range of a byte we do this math: 2 to the power of 8 bits - 1, -1. Which is: 2 8 -1 or 2 7 -1. And 2 7 is 128, minus 1 is 127. We subtract one in order to accommodate 0, which is counted as positive. So the highest value we can represent with a byte is 127. The low range is one step easier to get: 2 8 -1 is 2 7 is 128. So a byte in Java is capable of representing the values -128 to 127.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net