C
C
Core Java
Search…
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.
Ranges of values stored by the signed numeric Java primitive data types
Example:
1
byte num = 100;
2
short sum = 1240;
3
int total = 48764;
4
long population = 214748368;
Copied!
1
/*The default type of a nondecimal number is int. To designate an integer literal
2
value as a long value, add the suffix L or l (L in lowercase), as follows:*/
3
4
long distanceBwEarthAnsSun = 764398609800L;
Copied!
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:

Octal to Decimal
Hexadecimal to Decimal
Binary to Decimal

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:

Problem
Output
1
public class AnnualExam {
2
public static void main(String args[]) {
3
int baseDecimal = 267;
4
int octVal = 0413;
5
int hexVal = 0x10B;
6
int binVal = 0b100001011;
7
System.out.println(baseDecimal + octVal);
8
System.out.println(hexVal + binVal);
9
}
10
}
Copied!
1
Output : 534
2
534
Copied!

FLOATING-POINT NUMBERS: (Float ,Double)

You need floating-point numbers where you expect decimal numbers.
For example:
Range of values for Decimal Numbers
1
float average = 20.129F;
2
float orbit = 1765.65f;
3
double inclination = 120.1762;
Copied!
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 modified 2yr ago