Category: signed numeric

The numeric category defines two subcategories: integers and floating point (also called decimals).

Integers: (byte,int,short,long)

When you can count a value in whole numbers, the result is an integer. It includes both negative and positive numbers.

Example:

byte num = 100;
short sum = 1240;
int total = 48764;
long population = 214748368;
/*The default type of a nondecimal number is int. To designate an integer literal
value as a long value, add the suffix L or l (L in lowercase), as follows:*/

long distanceBwEarthAnsSun = 764398609800L;

Integer literal values come in four flavors: binary, decimal, octal, and hexadecimal:

  • Binary number system—A base-2 system, which uses only 2 digits, 0 and 1.

  • Octal number system—A base-8 system, which uses digits 0 through 7 (a total of 8 digits). Here the decimal number 8 is represented as octal 10, decimal 9 as 11,

    and so on.

  • Decimal number system—The base-10 number system that you use every day. It’s

    based on 10 digits, from 0 through 9 (a total of 10 digits).

  • Hexadecimal number system—A base-16 system, which uses digits 0 through 9 and

    the letters A through F (a total of 16 digits and letters). Here the number 10 is represented as A or a, 11 as B or b, 12 as C or c, 13 as D or d, 14 as E or e, and 15 as F or f.

Number Conversions:

Literal Values:

A literal is a fixed value that doesn’t need further calculations in order for it to be assigned to any variable.

You can assign integer literals in base decimal, binary, octal, and hexadecimal. For octal literals, use the prefix 0; for binary, use the prefix 0B or 0b; and for hexadecimal, use the prefix 0X or 0x. Here’s an example of each of these:

Find the Output:

public class AnnualExam {
	public static void main(String args[]) {
		int baseDecimal = 267;
		int octVal = 0413;
		int hexVal = 0x10B;
		int binVal = 0b100001011;
		System.out.println(baseDecimal + octVal);
		System.out.println(hexVal + binVal);
	}
}

FLOATING-POINT NUMBERS: (Float ,Double)

You need floating-point numbers where you expect decimal numbers.

For example:

float average = 20.129F;
float orbit = 1765.65f;
double inclination = 120.1762;

The default type of a decimal literal is double, but by suffixing a decimal literal value with F or f, you tell the compiler that the literal value should be treated like a float and not a double.

Last updated