Pages

Sunday, June 10, 2012

Data types


In this lesson I am going to explain the basic data types that can be used in Java. Here are the data types with its lenghts:

Type Lenght Default value Examples value Min/Max values
boolean 1 false false/true -
byte 8 0 1, 2, 3 -128 / 127
short 16 0 1, 2, 3 -32,768 / 32767
int 32 0 1, 2, 3 -2,147,483,648 / 2,147,483,647
long 64 0L 1, 2, 3 -9,223,372,036,854,775,808 / 9,223,372,036,854,775,807
float 32 0.0f 1.1, 1.2, 1.3 1.4E-45 / 3.4028235E38
double 64 0.0d 1.1, 1.2, 1.3 4.9E-324 / 1.7976931348623157E308
char 16 '\u0000' 'a', 'b', 'c' '\u0000'(0) / '\uffff'(65,535)
String - null "Hello" -

Furthermore there are the arrays. The array can be made by any type of the previous one, and they can also be made by objects. The arrays can be of one or more dimensions. The arrays must be instancied, and have a static size, defined when their are instancied.

Example of arrays:
byte[] a = new byte[10]; //size of array = 10
int[][] b = new byte[5][5]; //array of two dimensions, each one with a size of 5

When assiging a value to non-floating numeric variables, it can be assigned from multiples bases: 2(binary), 10(decimal), 16(hexadecimal).

Examples:
int a = 0b010101; //binary format. Atention, only for JDK 1.7 or grather
int b = 1234; //decimal format
int c = 0x1234abcd; //hexadecimal format

No comments:

Post a Comment