go to previous page   go to home page   go to next page hear noise highlighting

Can data type short hold the value 1,023,004 ?

Answer:

No. Data of type short can be only in the range -32,768 to +32,767.


More Bits for More Range

Larger ranges of numeric values require more bits. The different sizes for integer data enable you to pick an appropriate size for the data you are working with. Usually you should pick a data type that has a range much greater than the range of numbers you expect to deal with. If a program uses only a few dozen variables it will run just as fast and take up about as much main memory no matter what size is used for its variables.

Why do the small sized data types exist, then? Well, many real-world programs deal with massive amounts of data (billions of data items) and then using the smaller sizes may save significant amounts of space and time. But we will not use that much data in these notes. Usually you should use int or double for your numeric data.

When you write a program you do not have to know how to represent a number in bits. You can type the number just as you would on a typewriter. This is called a literal. The word "literal" means that a value is explicitly shown in the program.

For example, 125 literally represents the value one hundred twenty five. Integer literals in a program are written as in a book, except there are no commas:

    125       
    -32       
     16        
      0         
-123987

All of the above examples are 32 bit int literals.

A 64 bit long literal has an upper case 'L' or lower case 'l' at the end. However, NEVER use the lower case 'l' because it is easily confused with a digit '1'.

    125L    
    -32L     
     16L      
      0l        
-123987l

The last two examples use lower case 'l' and are very confusing. (The author once spent several hours debugging a program where this was the problem.)

In Java 7 and above, you can place the underscore character _ between digits in a numeric literal. Doing this is optional. Usually you would do this to put digits into groups of three. In the US a comma is used for this; in Europe a space is used for this. Here are more correct literals:


12_340_000

84_034.87

-45_000L
 

Don't put underscore at the beginning or end of a literal, nor before nor after the decimal point.


QUESTION 7:

Is the following an integer literal?

197.0

go to previous page   go to home page   go to next page