Data Types, Variables and
Arrays
Introduction
Java Is a Strongly Typed Language.
Indeed, part of Java’s safety and robustness comes from this fact.
There are no automatic coercions or conversions of conflicting types as in some
languages.
The Java compiler checks all expressions and parameters to ensure that the types
are compatible
Any type mismatches are errors that must be corrected before the compiler will
finish compiling the class.
The Primitive Types
Java defines eight primitive types of data: byte, short, int, long, char, float, double, and
Boolean .
These can be put in four groups:
1. Integers : This group includes byte, short, int, and long, which are for whole-valued signed
numbers.
2. Floating-point numbers: This group includes float and double, which represent numbers with
fractional precision.
3. Characters: This group includes char, which represents symbols in a character set, like letters
and numbers.
4. Boolean: This group includes boolean, which is a special type for representing true/false
values.
Literals
Any constant value which can be assigned to the variable is called literal.
Types: Integer Literals, Floating-Point Literals, Character Literals, String Literals
Escape Sequence
A character preceded by a backslash (\) is an escape sequence and has a special
meaning to the compiler.
Variables
A variable is defined by the combination of an identifier, a type, and an optional
initializer.
Java is a strongly typed language, so every variable must be declared and
initialized before it is used
Declaring a Variable In Java, all variables must be declared before they can be
used.
type identifier [= value][, identifier [= value] …];
Arrays
An array is a group of like-typed variables that are 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 offer a convenient means of grouping related information.
type var-name[ ];
var-name=new type[size];
Multidimensional Array
multidimensional arrays are actually arrays of arrays.
To declare a multidimensional array variable, specify each additional index using
another set of square brackets.
For example, the following declares a two-dimensional array variable called
twoD:
int twoD[][] = new int [4][5];
This allocates a 4 by 5 array and assigns it to twoD.
Internally, this matrix is implemented as an array of arrays of int.