Sei sulla pagina 1di 13

Instead of a normal decimal number, you can initialize an int variable with a hexadecimal

number. The number must be preceded with 0x. Here is an example:

public class Exercise {


public static void main(String[] args) {
int number = 0xA2EE;
System.out.print("Number: " + number);
}
}

This would produce:

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:

public class Exercise {


public static void main(String[] args) {
int number = 41710;
System.out.printf("Number: %d", number);
}
}

To display the number in hexadecimal format, use the %x or %X expression. Here is an


example:

public class Exercise {


public static void main(String[] args) {
int number = 41710;
System.out.printf("Number: %x", number);
}
}

This would produce:

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;

public class Main {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
short numberOfStudents;

System.out.print("Enter number of students: ");


numberofStudents = scnr.nextShort();

System.out.print("Number of Students: ");


System.out.println(numberOfStudents);
}
}

After declaring a short variable, to initialize it, assign the desired number to it. Here is an
example:

public class Exercise {


public static void main(String[] args) {
short numberOfStudents = 3428;
System.out.print("Number of Students: ");
System.out.println(numberOfStudents);
}
}

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.

You can concatenate a string a short variable in the parentheses of System.out.print() or


System.out.println(). To do this, use the + operator. Here is an example:

public class Exercise {


public static void main(String[] args) {
short numberOfStudents = 3428;
System.out.print("Number of Students: " + numberOfStudents);
}
}

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:

public class Exercise {


public static void main(String[] args) {
short number = 0x4A8;
System.out.print("Number: " + number);
}
}

To display the v alue of a short variable using System.out.printf(), use the %d expression.
Here is an example:

public class Exercise {


public static void main(String[] args) {
short number = 30208;
System.out.printf("Number: %d", + number);
}
}

If you want the result to appear as a hexadecimal number, use %x or %X.

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.

To retrieve a long value, apply nextLong to the Scanner variable.

After declaring a long variable, to initialize it, assign the desired value to it. Here is an
example:

public class Exercise {


public static void main(String[] args) {
long distance = 64564;
System.out.print("Distance: ");
System.out.print(distance);
}
}

This would produce:

Distance: 64564

In the parentheses of System.out.print() or System.out.println(), you can concatenate a


string with a long variable. This is done using the + operator. Here is an example:

public class Exercise {


public static void main(String[] args) {
long distance = 64564;
System.out.print("Distance: " + distance);
}
}

You can also initialize a long variable with a hexadecimal number. Here is an example:

public class Exercise {


public static void main(String[] args) {
long number = 0xACE882;
System.out.print("Number: " + number);
}
}

To display the v alue of a long variable using System.out.printf(), use the %d expression.
Here is an example:

public class Exercise {


public static void main(String[] args) {
long number = 30208;
System.out.printf("Number: %d", + number);
}
}

If you want the result to appear as a hexadecimal number, use %x or %X.

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:

public class Exercise {


public static void main(String[] args) {
long days = 24;
System.out.print("Days: " + days);
}
}

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:

public class Exercise {


public static void main(String[] args) {
long days = 245885475475;
System.out.print("Days: " + days);
}
}

This would produce:

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:

public class Exercise {


public static void main(String[] args) {
long days = 245885475475L;
System.out.print("Days: ");
System.out.print(days);
}
}

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

One of the main concerns wit


Wawan
weding
Wawan
wedding
Wawan
weding

Potrebbero piacerti anche