Data Types

Data types are used to store values in program.

What are Bits?

Bit is smallest unit of data. (Increases as Bits, Bytes, Kilobytes, Megabytes) Bit calculation is done with 2ⁿ. For example, 1 bit takes 2 values, 8 bits take 256 values.

Truth (1 bit)

It takes 2 values. (true, false) Often used in conditional statements. For example, print Welcome if logged in. This means that if LoginDate data type is correct, it will say Login.

Example:

Truth loggedIn=true;

if(loggedIn) print("Welcome");

//Output: Welcome

Integers

There are 4 integer data types. Usually Int data type is used.

Byte (8 bits)

128 negative, 128 positive (together with 0) takes a total of 2⁸=256 values.

Example:

Byte num1=-128; //Correct

Byte num2=127; //Correct

Byte num3=128; //Incorrect

Short (16 bit)

2¹⁵ negative, 2¹⁵ positive (together with 0) takes a total of 2¹⁶ values.

Example:

Short num1=32500; //Correct

Short num2=33000; //Incorrect

Int (32 bit)

2³¹ negative, 2³¹ positive (together with 0) takes a total of 2³² values.

Example:

Int num1=512*1024*2048; //Correct

Int num2=1024*2048*4096; //Incorrect

Long (64 bit)

2⁶³ negative, 2⁶³ positive (together with 0) takes a total of 2⁶⁴ values.

Example:

Long num=100;

Decimals

There are 2 decimal data types. It takes an integer value of up to 7 digits.

Decimal (32 bit)

It takes 8 digits. (excluding 0) If 8 digits are exceeded, number is rounded up. To indicate that value written is decimal, "d" is written at end.

Example:

Decimal num1=0.12345678d;

Decimal num2=1.1234567d;

Decimal num3=10.123456d;

Decimal num4=1234567.5d;

Number (64 bit)

It takes 17 digits. If 17 digits are exceeded (excluding 0) number is rounded up. Takes both Decimal and Int values.

Example:

Number num1=0.12345678912345678;

Number num2=1234567.1234567891;

Number num3=10;

Texts

There are 2 textual data types. It is written in double quotes. Usually Text data type is used. Exception characters are written with "\".

Exception Characters

New line: \n

Tab: \t

Quotes: \"

Open Curly Brackets: \{

Close Curly Brackets : \}

Character

Takes 1 character.

Example:

Character char1="a";

Character char2=":";

Character char3="\"";

Text

It consists of a combination of characters. If a character is used in text data type, "t" is written to right of double quotes. If a value is written in double quotes, it is written in curly brackets.

Example:

Text text1="Welcome";

print(text1.length); //Output: 11

Text text2=":"t;

Text text3="5+2={5+2}";

Arrays

It is used to store multiple values in a single variable. Its first element is 0.

DataType[] variable; // Array is created.

Usage 1: DataType[] variable=new DataType[ElementsCounts];

Example:

Text[] fruits=new Text[3]; //We created a text array and specified that it has 3 elements.

fruits[0]="apple";

fruits[1]="pear";

fruits[3]="strawberry"; //It gives an error. Because 3 elements can be written.

print(fruits[2]); //It outputs null. Because we didn't give any value.

print(fruits.indexSize); //Output: 3

Usage 2: DataType[] variable=new DataType[]{elements};

Example:

Int[] scores=new Int[]{40,70,95,100};

scores[1]=49;

print(scores[1]); //Output: 49

print(scores[3]); //Output: 100

print(scores[4]); //It gives an error. Because there are 4 elements.

print(scores.indexSize); //Output: 4