Data Types and Literals

Data types and literals are important elements of any programming language. In this lesson we will discuss about Java data types and its literals.

Importance of Data Types

When we write a software by using a programming language, we always look for error prevention features and reliability of output of our software. These features have to be provided by the programming language. This can be achieved by what type of data, the language supports and how it handles this data. Type of data also defines what kind of operation we can perform on that data. This makes a programming language more reliable and forces us to use data types to avoid errors. Java is a strongly typed language, that means all operations we perform using Java are type checked by the compiler. If any mismatch between types and operations are found,  compiler throws errors while compiling.

Primitive Data Types

Java contains two categories of built-in data types. They are Object oriented and primitive data types. Object oriented types are classes. We will discuss more details of object oriented types in later lessons. In this lesson we will discuss about primitive data types. These are also called simple data types.

The list of primitive data types in Java are listed in below table. Java strictly specifies range and memory space for each type. To maintain portability requirements Java is very strict about them.

Type Meaning
boolean Denotes true/false values
byte 8 bit integer
char Characters
double Double precision floating point numbers
float Single precision floating point numbers
int Integer values
long Long integer values
short Short integer values

Integer Types

Java defines 4 types of integer data types: byte, short, int and long. Each of them differ in memory size and range of values they can hold. As you can see in the below table, Java support both positive and negative values but it does not support only positive values. The first column shows keyword to use in programs for each data type.

Type Memory in Bits Range
byte 8 128 to 127
short 16 32,768 to 32,767
int 32 2,147,483,648 to 2,147,483,647
long 64 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Floating-Point Types

Integers store whole numbers. But in real world so many calculations include fractional numbers such as 6.88. We cannot use integer types to store fractional values. Then how to store them? Java provides two floating point types to store fractional values, they are float and double. The usage of these mainly depends on precision of data we want. float gives single precision data and double give double precision data.

Type Memory in Bits Range
float 32 approximately ±3.40282347E+38F
(6-7 significant decimal digits)
double 64 approximately ±1.79769313486231570E+308
(15 significant decimal digits)

Characters

In most languages, character type holds 8 bits of memory, but in Java character type holds 16 bits of memory. Because Java uses Unicode. Unicode defines a set of characters representing all the characters in all the human languages in the world.

Type Memory in Bits Range
char 16 0 to 65,536 (unsigned)

Boolean Type

There will be times when we need to take a decision whether an expression is true or false. To hold true or false Java uses boolean type. Java uses keywords true and false keywords with boolean type.

Type Memory in Bits Range
boolean 8 true or false

Literal in Java

Till now we discussed about different data types Java provides and their range. But we did not know how to represent a value in Java, for example how to represent integer 100 and character ‘A’ in Java program. These values are called literals in Java. The representation of these literals solely depends on its data type. Let us discuss now how represent these literals and what to consider when using literals.

Integer Literals

By default integer literals are int type. To represent the whole number 100 we just type 100 in source file. But to specify long we need to postfix the value with L, for example to represent 12 in long type, we need to write 12L. And we represent byte and short type values in the same way as int values as long as they are with their type range.

Floating-Point Literals

By default floating point literals are double type. And a double type value in source file is 12.1435. But to represent a float type we need to post fix with f, for example 12.45f

Character Literals

Every character value must be enclosed within single quotes, for example to represent character A we write ‘A’. char data type also takes integer values like 14 also. But when you assign an integer to char type Java converts that integer value to corresponding character from character set. For example value 65 is corresponds to character ‘A’.

Escape Sequence Characters

Sometimes we need to specify next line character, carriage return character, single quote and double quote etc. This will not work as you expected. Because some characters have special meaning in Java, so you cannot use directly them. To use these character you need prefix with backslash ‘\’, hence these are called backslash characters or escape sequence characters.

Escape Sequence Description
\’ Single quote
\” Double quote
\\ Backslash
\r Carriage return
\n New line
\f Form feed
\t Horizontal tab

String Literals

Java supports another type of literals: the strings. A string is a set of characters grouped together. These all characters enclosed with double quotes.

"Welcome to Java."

We have seen an example of string in our first Java program with println.

<<< Java VariablesOperators in Java >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .