0% found this document useful (0 votes)
55 views38 pages

Data Types, Variables, Type Conversion and Casting, Arrays

The document discusses Java primitive data types including integers, floating point numbers, characters, and booleans. It describes literals, variables, arrays, and type conversion and casting in Java. Key points include: - Java has 8 primitive data types grouped into integers, floating-point numbers, characters, and a boolean type. - Variables must be declared before use and have a defined scope and lifetime. - Arrays allow grouping of related data and can be one or multi-dimensional. Array elements are accessed via an index. - Both automatic type conversion and explicit casting can be used to change variable types, with restrictions on conversions between different groups of types.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views38 pages

Data Types, Variables, Type Conversion and Casting, Arrays

The document discusses Java primitive data types including integers, floating point numbers, characters, and booleans. It describes literals, variables, arrays, and type conversion and casting in Java. Key points include: - Java has 8 primitive data types grouped into integers, floating-point numbers, characters, and a boolean type. - Variables must be declared before use and have a defined scope and lifetime. - Arrays allow grouping of related data and can be one or multi-dimensional. Array elements are accessed via an index. - Both automatic type conversion and explicit casting can be used to change variable types, with restrictions on conversions between different groups of types.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Data types, Variables,

Type Conversion and Casting,


arrays
The Primitive Types
• Java defines eight primitive types of data:
byte, short, int, long, char, float, double, and
boolean.
• The primitive types are also commonly
referred to as simple types.
The Primitive Types
• These can be put in four groups:
• Integers
This group includes byte, short, int, and long,
which are for whole-valuedsigned numbers.
• Floating-point numbers
This group includes float and double,
which represent numbers with fractional
precision.
The Primitive Types
• Characters
This group includes char, which represents
symbols in a character set, like letters and
numbers.
• Boolean
This group includes boolean,
which is a special type for representing
true/false values
Data Types
Integers
Bytes- 8 bits
Short- 16 bits
Int- 32 bits
Long - 64 bits
Data Types
Floating point
Float- 32 bits
Double- 64 bits
Characters
• In Java, the data type used to store characters is
char.
• In C/C++, char is 8 bits wide. This is not the case
in Java.
• Instead, 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
Characters
• It is a unification of dozens of character sets, such
as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana,
Hangul, and many more.
• For this purpose, it requires 16 bits. The range of a
char is 0 to 65,536. There are no negative chars.
• The standard set of characters known as ASCII still
ranges from 0 to 127, and the extended 8-bit
character set, ISO-Latin-1, ranges from 0 to 255.
Characters
• Since Java is designed to allow programs to be
written for worldwide use, it would use
Unicode to represent characters.
• The use of Unicode is somewhat inefficient for
languages such as English, German, Spanish, or
French, whose characters can easily be
contained within 8 bits.
• But such is the price that must be paid for
global portability
boolean
Java has a primitive type, called boolean, for
logical values. It can have only one of two
possible values,
true or false
This is the type returned by all
relational operators, as in the case of a < b.
boolean is also the type required by the
conditional expressions that govern the control
statements such as if and for
Literals

Integers
Decimals 123
Octal 0123
Hexadecimal 0X123
• The range of a hexadecimal digit is 0 to 15, so A
through F (or a through f ) are substituted for 10
through 15
• Long values are to be indicated with
succeeding L.
• Other literals are converted to receiving values if the
size of literals is within the range of receiving
variables
Literals
Floating point
Standard
Scientific
• If float required append f or F to literal value
Boolean
True
False
Character Literals
• Characters in Java are indices into the Unicode
character set.
• They are 16-bit values that can be converted into
integers and manipulated with the integer operators,
such as the addition and subtraction operators.
• A literal character is represented inside a pair of
single quotes. All of the visible ASCII characters can
be directly entered inside the quotes, such as ‘a’, ‘z’,
and ‘@’.
Character Literals
• For characters that are impossible to enter
directly, there are several escape sequences
\ddd octal character
\uXXXX Hexadecimal
\’ Single Quote
\” Double Quote
\\ Back Slash
\r Carriage return
Character Literals

\n Newline
\f Form Feed
\t Tab
\b Back Space
String Literals
• String literals in Java are specified by enclosing
a sequence of characters between a pair of
double quotes.
• Examples of string literals are
“Hello World”
“two\nlines”
“\”This is in quotes\”“
• Must begin and end on the same line.
• Strings are classes.
Variables
Declaring a Variable
• In Java, all variables must be declared before
they can be used.
• The basic form of a variable declaration is
shown here:
type identifier [ = value][, identifier [= value] ...] ;
Dynamic Initialization
Although the preceding examples have used only
constants as initializers, Java allows variables to
be initialized dynamically, using any expression
valid at the time the variable is declared.
Example:
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
Scope & Life Time of Variables
• Java allows variables to be declared within any block.
• A block is begun with an opening curly brace and
ended by a closing curly brace.
• A block defines a scope. A scope determines what
objects are visible to other parts of your program. It
also determines the lifetime of those objects.
• variables declared inside a scope are not visible (that
is, accessible) to code that is defined outside that
scope.
• Scopes can be nested.
• For example, each time you create a block of
code, you are creating a new, nested scope.
• When this occurs, the outer scope encloses
the inner scope.
• This means that objects declared in the outer
scope will be visible to code within the inner
scope.
Scope & Life Time of Variables
• In Java, the two major scopes are those
defined by a class and those defined by a
method.
• The scope defined by a method begins with its
opening curly brace. However, if that method
has parameters, they too are included within
the method’s scope.
Quiz
1.Define Literals.
2. Define Scope & Life Time of Variables.
3. Identify size of float, integer.
4. Define Boolean.
5. Define Strings in java.
Type Conversion and Casting
Automatic Conversion
• Conditions:
Possible if variables of like nature
The destination type is larger than the source
type.
When these two conditions are met, a widening
conversion takes place
Type Conversion and Casting
• The following rules takes place:
– Numeric variables are compatible
– Numeric variables are not compatible with char,
Boolean.
– Char & Boolean are not compatible
– Auto conversion takes place while assigning
integers, literals to byte, short, long.
Type Conversion and Casting
• Casting
– Auto conversion is not useful when conversion like
integers to byte is needed.
– Casting is useful for narrowing conversions
– A cast is simply explicit type conversion
– Ex:
byte b;
int a;
b = (byte) a
Type Conversion and Casting
Automatic Type Promotion in Expressions
• When two operands of lower size are
operated, the resultant value may far exceed
the size of operand.
• Byte & short operands are automatically
promoted to int.
Type Conversion and Casting
• If expression is in float, then its sub expression
is also promoted to float
• If expression is in double, then its sub
expression is also promoted to double.
Array
• Like typed variables referred by a common
name
• May have one or more dimensions
• A specific element of an array is referred by an
index
• Offers convenient means of grouping related
information.
Array
• Syntax
<data type> <Variable name> [];
• Only defines variables not the size.
• Size is allocated by using a ’new’ operator.
int a[];
a = new int[10];
In Java all arrays are dynamically allocated
Array
• Array elements are referred by index.
• Index value will start from zero
• It is possible to define and allocate size
simultaneously.
int monthdays[] = new int[12]
• Arrays can be initialized
Comma separated
Enclosed in curly bracket
Array
• Array size can be defined by initialization.
int monthdays[] = { 31, 28 ………..}
• JAVA checks the boundaries at run time
Multi Dimension Array
• In Java, multidimensional arrays are actually
arrays of arrays.
• There are a couple of subtle differences.
• To declare a multidimensional array variable,
specify each additional index using another
set of square brackets.
Multi Dimension Array
• For example, the following declares a two
dimensional array variable called twoD.
int twoD[][] = new int[4][5];
LTC: one-dimensional array.

// Demonstrate a one-dimensional array.


class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + "
days.");

The output generated by this program is shown here:


April has 30 days.

End of session

You might also like