Sunday, October 28, 2018

Java Data Types

Data Types & Literals in Java

A data type represents the type of data we are storing into variables. The value we are storing in the variable is known as literal.

Integer:

Represent integer numbers. Integer numbers are numbers without fractional part.

Data Type
Memory Size
Min & Max values
Byte
1 byte
-128 to +127
Short
2 bytes
-32768 to +32767
Int
4 bytes
-2147483648 to +2147483647
long
8 bytes
-9223372036854775808 to +9223372036854775807


Float data type:

Float data type represents floating point numbers. Float data types are used to
handle numbers with decimal points.

Data Type
Memory Size
Min & Max values
float
4 bytes
-3.4e38 to +3.4e38
Double
8 bytes
-1.7e308 to +1.7e308
Example:
float pi = 3.14F; (by adding F JVM allocates 4 bytes otherwise JVM allocates 8 bytes.)
double distance = 1.98e8;

Difference between float and double?

float represents up to 7 digits accurately after decimal point. (single precision floating point)
double represents up to 15 digits accurately after decimal point. (double precision floating point)

Character data type:

Character data type represents a single character.
Data Type
Memory Size
Min & Max values
char
2 bytes
0 to 65535

Example:
char ch = 'x';

What is Unicode?

Unicode is a specification to include the alphabets of international languages into 
character set of java. Unicode system uses 2 bytes to represent a character.

String data type:

String data type represents a group of characters. String is also a class and We will descuss more about it in the coming lessions.

Example:
String name = "Java";
String str = new String("Java");

Boolean data type:

Boolean data type represents either true or false.

Example:
boolean response = false;

No comments:

Post a Comment