130 likes | 247 Views
Java. Primitive Data Types. Non-Decimal Numbers are integers. The types byte, short, int , and long are used to store Non-Decimal N umbers. Non-decimal numbers. byte. Non-Decimal N umbers. Size 8 bits Range -128 to 127 (inclusive) Use
E N D
Java Primitive Data Types
Non-Decimal Numbers are integers. The types byte, short, int, and long are used to store Non-Decimal Numbers. Non-decimal numbers
byte • Non-Decimal Numbers • Size • 8 bits • Range • -128 to 127 (inclusive) • Use • - Only use if you KNOW you will not need to include numbers outside the range. • Only use if memory saving is a top priority. Implementation byte b = 98;
short • Non-Decimal Numbers • Size • 16 bits • Range • -32,768 to 32,767 (inclusive) • Use • - Only use if you KNOW you will not need to include numbers outside the range. • - Only use if memory saving is a priority. Implementation short s = 7456;
int • Non-Decimal Numbers • Size • 32 bits • Range • -2,147,483,648 to 2,147,483,647 (inclusive) • Use • - This is the default data type for non-decimal numbers. • - Use this type unless numbers in your program exceed the range. Implementation inti = 82945;
long • Non-Decimal Numbers • Size • 64 bits • Range • -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive) • Use • - Use only if numbers in the program exceed the range of int. Implementation long l = 28947302;
Decimal Numbers can be used to store any number. The types float and double are used to store Decimal Numbers. Decimal numbers
float • Decimal Numbers • Size • 32 bits • Use • Smaller than standard data type. • Use only if memory usage needs to be very small. • DO NOT use for values that require great precision. Implementation float f = 12.54;
double • Decimal Numbers • Size • 64 bits • Use • This is the default data type for decimal numbers. • DO NOT use for values that require great precision. Instead use the java.math.BigDecimal class. Implementation double d = 823.9483;
These primitive data types are used to store other values. They include boolean and char. Other values
boolean • Other Values • Use • Can only contain true or false. • Use only to track and flag true/false conditions Implementation boolean b = true;
char • Other Values • Size • 16 bits • Use • Used for storage of characters other than numbers, such as letters, spaces, and other symbols. Implementation char = ‘h’;
A Final Note No primitive data type needs “new” to be initialized since all are built into JAVA. Also, although string is not considered a primitive data type, programmers often think of it as so, since it is also built into JAVA.