Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Number: 41710
To display the value of an int variable using System.out.printf(), use the %d format to
represent a character variable. Here is an example:
Number: a2ee
As you can see, if you use %x, the characters in the result would appear in lowercase. If
you want the characters to appear in uppercase, use %X.
Short Integers
An integer is referred to as short if its value is between -32768 and 32767. This number
can fit in 16 bits. To declare a variable that would hold numbers in that range, you can
use the short data type.
To retrieve a short integer, apply nextShort() to the Scanner variable. Here is an example:
import java.util.Scanner;
After declaring a short variable, to initialize it, assign the desired number to it. Here is an
example:
As you may realize, any number that can fit in a short can also fit in an integer. In order
words, you can safely use the int keyword to declare a variable that would hold numbers
intended for a short. There is no danger. Even if you think of computer memory (because
a short uses less space than an int), memory is not expensive anymore.
Just as mentioned for the byte, the value you assigned to a short variable must fit in the
allowed range, otherwise you would receive an error when you compile the program.
Instead of a normal decimal integer, you can initialize a short variable with a
hexadecimal number. Here is an example:
To display the v alue of a short variable using System.out.printf(), use the %d expression.
Here is an example:
Long Integers
A number that is higher than a regular integer can hold is called a long integer. To
support this type of number, the Java language provides the long data type. A long
integer is a variable that can hold a very large number that may require 64 bits to be
stored accurately. To declare a variable for such a number, use the long keyword.
After declaring a long variable, to initialize it, assign the desired value to it. Here is an
example:
Distance: 64564
You can also initialize a long variable with a hexadecimal number. Here is an example:
To display the v alue of a long variable using System.out.printf(), use the %d expression.
Here is an example:
As you may realize, a number that fits in a short or an int can also fit in a long. This
means that you can declare a variable as long but store very small numbers in its
memory. Here is an example:
This program works perfectly fine but may result in a significant waste of memory. When
you declare a long variable, by default, the compiler "allocates" only enough memory for
an integer so there would not be a waste. In some cases, you may want the compiler to
use 64 bits for your long variable. Consider the following program:
init:
deps-jar:
Compiling 1 source file to C:\Programs\JavaLessons\Exercise1\build\classes
C:\Programs\JavaLessons\Exercise1\src\exercise1\Main.java:24:
integer number too large: 245885475475
long days = 245885475475;
1 error
BUILD FAILED (total time: 0 seconds)
The program would not work because the compiler would have reserved space for an
integer but the assigned value needs more room. If you insist on using enough memory
for a long integer, when initializing it, on the right side of the value, type L. Here is an
example:
This time, the program works fine because the compiler was explicitly asked to reserve
enough memory.
Real Numbers
Introduction
A number is referred to as decimal when it displays a natural section and a precision.
Both sections are separated by a special character called the decimal symbol, which
depends on the language and can be verified in the Regional Options dialog box from the
Control Panel:
Wawan
wedding
Decimal
The values on both sides of the decimal symbol are represented by digits. The value on
the left side is expressed like a natural number: it can use a positive (+) or a negative (-)
sign to show whether it is higher or lower than 0. It can also be written with commas to
make it easier to read. The value on the right side of the decimal separator can be made of
only digits. This value specify the level of precision allowed on the number. Normally,
the more numbers, the more precise.
The Java language supports two types of real numbers: single precision and double-
precision.
Double-Precision Values