Primitive Data Type
Primitive Data Types
- Java is a strongly typed language. This means that every variable must have a declared type. There are eight primitive types in Java.
- Four of them are integer types;
- Two are floating-point number types;
- One is the character type char, used for code units in the Unicode encoding scheme
- One is a Boolean type for truth values.
The integer types are for numbers without fractional parts. Negative values are allowed. Java provides the four integer types.
Java Integer Types
|| 4 bytes
||–2,147,483,648 to 2,147,483, 647
|| 2 bytes
||–32,768 to 32,767
|| 8 bytes
||–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
|| 1 bytes
||–128 to 127
The floating-point types denote numbers with fractional parts. The two floating-point types are shown in
|| 4 bytes
(6–7 significant decimal digits)
|| 8 bytes
approximately ±1.79769313486231570E+308 (15
significant decimal digits)
The char Data Type
- The char date type is used to store characters. A variable of the char data type can hold one character at a time. Character literals are enclosed in single quotation marks.
For example char letter;
- Program Output A
The Boolean Type
The Boolean type has two values, false and True. It is used for evaluating logical conditions. You cannot convert between integers and Boolean values.
- Literal is a value that is written into the code of a program.
- The String literal is always enclosed in double quotes. Java uses a String class to implement strings, whereas C and C++ use an array of characters.
- Character literals are similar to String literals excepts they are enclosed in single quotes and must have exactly one character. For example ‘c’ is character literal. A backslash is used to denote the non-printing characters such as
| Horizontal tab
| Single quote
| Double quote
- A Boolean literal can have either of the value; true or false. They do not correspond to the numeric values, 1 and 0 as in C and C++.
- There are two types of numeric literal: integers and floating point numbers.
- For example: 34 is an integer literal, and it means the number thirty four.
- 1.5 us a floating point literal.
- Float number;
- Number=23.5 //error
- Note -> you can force a double literal to be treated as Float, however, by suffixing it with the letter F or f.
- Number=23.5F // this will work
- 45.6, 76.4E8(76.4 tunes 10 to the 8th power) and 32.0 are also floating point literals.