> Primitive Numeric Data Types in java ~ Online tutorial

Primitive Numeric Data Types in java

Primitive Numeric Data Types

Java stores simple information in what are called primitive data types. There are eight primitive data types. For us the most important numeric data types are: int – a data type suitable for storing whole numbers such as -84, 0 or 3274
o ints may range from -2 billion to +2 billion approximately .
©Daniel L. Schuster, 2011, all rights reserved Page #7
o ints are stored exactly. If you put the int 32,457 into memory, then 32,457 is what is stored, without any loss of precision. It may surprise you but this is not true for all numeric information stored in computer memory.
o Calculations with ints are exact, without error. Except that if your program has integer overflow there'll be a problem with accuracy, but we won't worry about it and you don't have to know what integer overflow is for now.
o Calculations with ints usually run faster than calculations with doubles.
o Java automatically treats whole numbers in your source code as ints.
o In your source code you must put ints in your program without a comma. 32,457 would be typed in as 32457
o When running a program that asks you to enter an int at the keyboard you must type it in without a comma. double – a data type suitable for storing real numbers such as -17.81 or 3.1415927, as well as whole numbers
o doubles may range from ±1.78x10308 to ±4.94x10-324.
o doubles are not necessarily stored exactly. 2.980 might actually be stored as 2.9799999. This is a problem of course, but it won't generally concern us in a beginning Java course.
o double calculations are generally not exact and run slower than int calculations.
o in your source code, any number with a decimal point is automatically treated as a double.
Other Java numeric primitive numeric data types are byte, short and long (which are variations of the int data type) and float (which is a variation of the double data type). They won't be important for our first course in Java, so they won't be mentioned again.

There are also two other primitive data types, boolean and char, which will be very useful to us and we will study later.

Finally, let's consider how memory is allocated when we create variables for primitive data types and assign values to them. The statement

int x;
results in a memory location big enough to hold an int with the name x. With reasonable (but certainly not perfect!) accuracy we can visual this as
The statement
int x = -57;
gives us
empty memory location


Memory allocation for things like GLabels and other objects (we'll learn about them later) is more complex and we'll put that off for a while.

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
Powered By: BloggerYard.Com