0% found this document useful (0 votes)
8 views7 pages

Notes7 (Typecasting and Array)

Uploaded by

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

Notes7 (Typecasting and Array)

Uploaded by

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

Typecasting

1)data type casting/ primitive type casting


The process of Converting one primitive type in to another primitive type we call it as
primitive typecasting.

There are 2 types of primitive typecasting


1)widening
2)narrowing

1)widening:
The process of converting data from a smaller primitive type to a larger primitive type is
known as widening.
Widening can occur in two ways:
automatically by the compiler (also known as auto-widening or implicit widening), and
explicitly by the programmer using the type cast operator.

Automatic Widening (Auto-Widening or Implicit Widening):


The compiler automatically converts a smaller primitive type to a larger primitive type where
necessary.
This process ensures that there is no data loss and that the operation is performed with the
correct type.
Example:
byte smallValue = 42;
int largerValue = smallValue; // Automatically converts byte to int

Explicit Widening:
Although widening is generally automatic, the programmer can also explicitly perform
widening using the type cast operator.
Example:
byte smallValue = 42;
int largerValue = (int) smallValue; // Explicitly converts byte to int

Type-Cast operator
the typecast operator is used explicitly convert a data from one data type to another datatype.
It is commonly used for explicit typecasting, which is necessary whenever the compiler
cannot typecast automatically or when there is a risk of data loss.
Syntax:
(targetType) value/variable/expression;

2)narrowing:
The process of converting data from a larger primitive type to a smaller primitive type is
known as narrowing
Narrowing, also known as explicit typecasting which can lead to data loss, so it must be done
explicitly by the programmer using the typecast operator.
Note:
Compiler will not perform narrowing due to the risk of data loss.

Example:
double doubleValue = 9.78;
int intValue = (int) doubleValue; // Explicitly casting double to int
System.out.println(intValue); // Output: 9

remember: class type casting or non-primitive type casting will be studied in the future
classes

Array:
An array is a contiguous block of memory which is used to store multiple values of the same
type (homogeneous values).

Characteristics of an array
1)array is fixed in size
2)array is a homogenous collection of data
3)array is index based, the index starts from 0 and ends with size-1
4)To store or fetch elements in an array we require 2 things
a) array reference
b) index

syntax to declare an array:


1) dataType[] arrayName;
or
2) datatype []arrayName;
or
3) datatype arrayName[];
example:
1) int[] numbers; // Declares an array of integers
2) double[] prices; // Declares an array of doubles
3) String[] names; // Declares an array of strings
Array can be created in 2 ways:
1) With new operator
2) Without new operator

1)with new operator


Syntax: new datatype[size];

Example:
new int[5];
new: it is a keyword is used to dynamically allocate memory for objects in Java. When used
with arrays, it allocates memory for the array elements.
dataType: This specifies the data type of the elements that the array will hold. It can be a
primitive data type (such as int, double, char, etc.) or a non-primitive data type (String,
classname).
size: This specifies the number of elements that the array can hold. It must be a non-negative
integer value.

Syntax to declare and instantiate an array in a single line

dataType[] arrayName = new dataType[size];

Example:
int[] a1 = new int[6];
Program:
public class ArrayDemo1 {

public static void main(String[] args) {

int[] a1 = new int[6];

a1[0]=15;
a1[1]=27;
a1[2]=21;
a1[3]=13;
a1[4]=44;
a1[5]=17;

//System.out.println(a1[3]);

//System.out.println(a1[0]);
//System.out.println("===================");

//System.out.println(a1.length);

//System.out.println("======================");

for(int i=0 ; i<=a1.length-1 ; i++)


{
System.out.println(a1[i]);
}
}

Output:
15
27
21
13
44
17
2)Without new operator:
Syntax:
datatype[] arrayname = {value1,value2, value3,……..,value n};

example:
int[] a2 = {40,55,63,17,22,68,89,97,89};

Program
public class ArrayDemo2 {
public static void main(String[] args) {

int[] a2 = {40,55,63,17,22,68,89,97,89};

for(int i=0 ; i<a2.length ;i++)


{
System.out.println(a2[i]);
}
}
}

Output
40
55
63
17
22
68
89
97
89
❖ When to create an array with new operator and without new operator?
• Array with new Operator
Dynamic Initialization: When the array size is known, but the values are not known at
compile-time and need to be assigned at runtime.

• Array without new operator


Initialization with Known Values: When you already know the values that the array
should contain at the time of writing the code.
Readability: It makes the code shorter and more readable when initializing an array
with a fixed set of values.

You might also like