You are on page 1of 16

Data Types, Variables and

Arrays
What is Typed Language?
• In this type of languages every variable is strictly
defined.
• All the assignments whether explicit or via
parameter passing in methods class, are
checked for type compatibility.
• Also there is no automatic type conversions.
• Such languages are called Type Languages
• Java is also such typed language i.e. it is a
strongly typed language.
Primitive Data types
• Java defines eight primitive types of data
• Byte, Short, Integer, Long , Character, Float,
Double and Boolean
• Further they are divided into four groups
• Integers: This group includes byte, short, uint
and long which are whole valued signed
numbers.
• Flaoting point numbers: This group includes float
and double which represents number with
fractional precision
• Characters: This group includes character,
which represents symbols in a character
set, like letters and numbers.
• Boolean: This group includes Boolean,
which is a special type for representing
true or false values
Integer
• Java Defines four integer types:
• Byte:
– The smallest types is byte
– This is 8 bit type that has a range from -128 to 127
– They are useful when we are working with astream of
data from network or file or when we are working with
raw binary data
– They are declared using byte keyword
– byte a, c
• Short:
– It is a signed 16 bit type.
– It has range from -32768 to 32767.
– It is probably least used
– It is defined using short keyword
– For Example: short a, c
• Int:
– It is the most common used integer type
– It is signed 32 bit type that has a range
-2,147,483,648 to 2,147,483,647
– It is defined using int keyword
– For Example: int a, c
• Long:
– It is a signed 64 bit type and is useful when int
type is not enough to large values.
– It defined by using keyword long
– For example: long a
• Note:
– Any whole number is an integer literals
– It includes all the values with base 10 number.
– It also supports octal and hexadecimal values
– Octal values are denoted by leading 0 while
hexadecimal values are denoted by using
leading zero-x i.e. 0x or 0X
Floating Point Types
• They are also called Real Numbers, and
are used for evaluating fractional precision
• There are two kinds of floating point types
– Float:
• The type float specifies a single precision value
that uses 32 bits of storage.
• They are defined by using float keyword
• For example: float a
• Double:
– Double precision is denoted by double
keyword.
– It uses 64 bit to store a value.
– Double is best choice when we have many
iterative calculations or many manipulating
large valued numbers.
– For example: double a
Characters
• In Java, the data type used to store characters is
char.
• Java uses Unicode to represent characters.
• Unicode defines a fully international character
set that can represent all of the characters found
in all human languages.
• For this purpose it required 16 bits.
• Thus Java supports global probability
• For example: char a
Boolean
• It is used for logical values.
• It can have only one out of two possible
values, true or false.
• This type is returned by all relational
operators
• It is also used in conditional expressions
that governs control statements such as if
and for.
• For example: Boolean a
Variable
• The Variable is the basic unit of storage in
a Java Program.
• A variable is defined by the combination of
an identifier, a type, and an optional
initializer.
• All variables have scope, which defines
their visibility and a lifetime.
Arrays
• An array is an group of like typed variables
that referred to by a common name.
• Arrays of any type can be created and
may have one or more dimensions.
• A specific element in an array is accessed
by its index.
• Arrays provides a way of grouping related
information
Types of Arrays
• One Dimensional Arrays
– It is essentially a list of like typed variables.
– To create an array, we first must create an array variable
of the desired type.
– The general form of one dimensional array is
• type var-name[];
– Here type defines the base type of the array.
– We can also define array using new keyword.
• array-var=new-type[size];
– Here type specifies the type of dta being allocated, size
specifies the number of elements of the array and array-
var is the array variable
– Array Initializations
• int a[]= {10,20,30};
• int a[]=new int[10];

• Multi Dimensional Arrays


– In Java, multi dimensional arrays are actually
arrays of arrays.
– To declare an array of such type we need to
add an additional index using another set of
square brackets.
• For example:
– int twoD[] [] = new int [4][5];
– Here we have create a 4 by arrays.
– We can also initialize this arrays in following
ways.
– Int twoD[] [] = {{1,2},{3,4},{5,6}};
• Note:
– We can also create an array of character
• char[] c=new char[30];
– We can also initialize character array in
following way
– Char[] vowels={a,e,i,o,u};

You might also like