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

Type Storage Requirement Range
int 4 bytes  –2,147,483,648 to 2,147,483, 647
short 2 bytes  –32,768 to 32,767
long 8 bytes  –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
byte 1 bytes  –128 to 127
Floating-Point Types
The floating-point types denote numbers with fractional parts. The two floating-point types are shown in

Floating-Point Types

Type Storage Requirement Range
float 4 bytes 

approximately ±3.40282347E+38F

(6–7 significant decimal digits)

double 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;
  • letter=‘A’;
  • System.out.println(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.

String Literals

  • 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.

For example
"Hello World"

Character Literals
  • 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
Description Escape Sequence
Line feed  \n  
Carriage Return \r
Horizontal tab \t
Backslash  \\
Single quote \'
Double quote \"

Boolean Literal

  • 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++.

Numeric Literal

  • 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.


Latest Tutorial

Most Visited Tutorial