Java - Basic Datatypes - Clemson University

JAVA - BASIC DATATYPES



Copyright ? tutorials

Variables are not hing but reserved memory locat ions t o st ore values. This means t hat when you creat e a variable you reserve some space in memory. Based on t he dat a t ype of a variable, t he operat ing syst em allocat es memory and decides what can be st ored in t he reserved memory. Therefore, by assigning different dat a t ypes t o variables, you can st ore int egers, decimals, or charact ers in t hese variables. There are t wo dat a t ypes available in Java:

Primit ive Dat a Types Reference/Object Dat a Types

Primit ive Dat a Types:

There are eight primit ive dat a t ypes support ed by Java. Primit ive dat a t ypes are predefined by t he language and named by a keyword. Let us now look int o det ail about t he eight primit ive dat a t ypes.

byt e:

Byt e dat a t ype is an 8-bit signed t wo's complement int eger. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7 -1) Default value is 0 Byt e dat a t ype is used t o save space in large arrays, mainly in place of int egers, since a byt e is four t imes smaller t han an int . Example: byt e a = 100 , byt e b = -50

short :

Short dat a t ype is a 16-bit signed t wo's complement int eger. Minimum value is -32,768 (-2^15) Maximum value is 32,767 (inclusive) (2^15 -1) Short dat a t ype can also be used t o save memory as byt e dat a t ype. A short is 2 t imes smaller t han an int Default value is 0. Example: short s = 10000, short r = -20000

int :

Int dat a t ype is a 32-bit signed t wo's complement int eger. Minimum value is - 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31 -1) Int is generally used as t he default dat a t ype for int egral values unless t here is a concern about memory. The default value is 0. Example: int a = 100000, int b = -200000

long:

Long dat a t ype is a 64-bit signed t wo's complement int eger. Minimum value is -9,223,372,036,854,775,808.(-2^63) Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1) This t ype is used when a wider range t han int is needed. Default value is 0L. Example: long a = 100000L, int b = -200000L

float :

Float dat a t ype is a single-precision 32-bit IEEE 754 float ing point . Float is mainly used t o save memory in large arrays of float ing point numbers. Default value is 0.0f. Float dat a t ype is never used for precise values such as currency. Example: float f1 = 234.5f

double:

double dat a t ype is a double-precision 64-bit IEEE 754 float ing point . This dat a t ype is generally used as t he default dat a t ype for decimal values, generally t he default choice. Double dat a t ype should never be used for precise values such as currency. Default value is 0.0d. Example: double d1 = 123.4

boolean:

boolean dat a t ype represent s one bit of informat ion. There are only t wo possible values: t rue and false. This dat a t ype is used for simple flags t hat t rack t rue/false condit ions. Default value is false. Example: boolean one = t rue

char:

char dat a t ype is a single 16-bit Unicode charact er. Minimum value is '\u0000' (or 0). Maximum value is '\uffff' (or 65,535 inclusive). Char dat a t ype is used t o st ore any charact er. Example: char let t erA ='A'

Reference Dat a Types:

Reference variables are creat ed using defined const ruct ors of t he classes. They are used t o access object s. These variables are declared t o be of a specific t ype t hat cannot be changed.

For example, Employee, Puppy et c. Class object s, and various t ype of array variables come under reference dat a t ype. Default value of any reference variable is null. A reference variable can be used t o refer t o any object of t he declared t ype or any compat ible t ype. Example: Animal animal = new Animal("giraffe");

Java Lit erals:

A lit eral is a source code represent at ion of a fixed value. They are represent ed direct ly in t he code wit hout any comput at ion. Lit erals can be assigned t o any primit ive t ype variable. For example:

byte a = 68; char a = 'A'

byt e, int , long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or oct al(base 8) number syst ems as well. Prefix 0 is used t o indicat e oct al and prefix 0x indicat es hexadecimal when using t hese number syst ems for lit erals. For example:

int decimal = 100; int octal = 0144; int hexa = 0x64;

St ring lit erals in Java are specified like t hey are in most ot her languages by enclosing a sequence of charact ers bet ween a pair of double quot es. Examples of st ring lit erals are:

"Hello World" "two\nlines" "\"This is in quotes\""

St ring and char t ypes of lit erals can cont ain any Unicode charact ers. For example:

char a = '\u0001'; String a = "\u0001";

Java language support s few special escape sequences for St ring and char lit erals as well. They are:

No tatio n

Character represented

\n

Newline (0x0a)

\r

Carriage ret urn (0x0d)

\f

Formfeed (0x0c)

\b

Backspace (0x08)

\s

Space (0x20)

\t

t ab

\"

Double quot e

\'

Single quot e

\\

backslash

\ddd \uxxxx

Oct al charact er (ddd) Hexadecimal UNICODE charact er (xxxx)

What is Next ?

This chapt er explained you various dat a t ypes, next t opic explains different variable t ypes and t heir usage. This will give you a good underst anding about how t hey can be used in t he Java classes, int erfaces, et c.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download