BASICS OF JAVA (PART 2)

DATA TYPES IN JAVA

A data type represents the different values to be stored in the variable. In java, there are two types of data types:

  • Primitive data types
  • Non-primitive data types

Primitive data types

These are built in data types provided by the programming language.

Java uses six predefined data types, called primitive numerical data
types. They include: int, long, short, byte, float, and double.

While java also uses 2 predefined data types called primitive non numerical data types which include: char and boolean.

Primitive numerical data

i. int

This data type stores whole numbers int stands for integer.

Int ranges from -2147483648 – 2147483647.

This data type is preferred when creating variables with numeric values.

Int are of 32 bits.

The default value of int is 0.

Example

int student =23456;

ii. long

These data types also store whole numbers.

Long ranges from -9223372036854775808 – 9223372036854775807.

Long are of 64 bits.

This data type is used if int is not wide enough in storing the numbers.

End the value of a long with an L.

The default value of long is 0L.

Example

long data = 10000000L;

iii. Short

These data types store whole numbers as well.

Short are of 16 bits.

It ranges from -32,768 – 32,767.

Short types are two times smaller than int.

Example

Short a=1000;

iv. Byte

Byte can be used to store whole numbers.

They are of 8 bits.

The value stored ranges between -128 – 127.

Byte type are four times smaller than integer.

Other than whole numbers Java have other data types which store decimal numbers and they mainly include float and double.

v. Float

Float data types is used to store decimal or fractional numbers.

Float data is a 32 bit single precision

Its value range is unlimited but to be more specific it ranges from 3.4e−038 to 3.4e+038

Float data types can be used  to store memory in large arrays of floating point numbers.

This can store values from 6-7 decimal digits

End the floating number with f

Example

float length = 3.1456f;

vi Double

This data type stores fractional or decimal numbers as well.

These are 64 bits double precision.

The value range of double type is unlimited.

They can store values up to 15 decimal digits.

Its value range from 1.7e-308 to 1.7e+308.

The values in a double type should end with a d.

Example

double b=3.87965d;

Primitive Non numerical data types

i. Char

Characters in java are stored using char data types.

Char stores 16 bits Unicode characters.

The characters should be enclosed in a single quote i.e ‘a’ ‘c’.

The value of char ranges from  ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive).

Example

char grade = ‘A’;

ii. Boolean

Boolean data types are declared with the boolean keyword.

These data types only accepts two values that is either true or false.

They represent 1 bit of information.

The default value of boolean data types is false.

Example

boolean male = true;

Non primitive data types

These are user defined data types and they include Arrays, Classes, String among others which we are going to discuss in later chapters.

EXERCISE

  1. Outline the two main types of data types used in Java.
  2. In what case would you use long types instead of int types.
  3. Differentiate between float and double types.
  4. What are the keywords that can be used to declare values of characters and booloean types?
  5. Outline the value range of the following data types
  • Integer
  • Long
  • Short
  • Boolean
  • Character
  • Byte

Leave a comment

Design a site like this with WordPress.com
Get started