6_Array_Coversion_CLA
6_Array_Coversion_CLA
• Array
o One-Dimensional Arrays
o length variable for an array
o Multidimensional Arrays
o Alternative Array Declaration Syntax
o Exercises
• Array of objects
• Using Command-Line Arguments
Reference Data Types
• In Java, a reference data type is a variable that can contain the reference or an
address of dynamically created object.
• These type of data types are not predefined like primitive data type.
• The reference data types are :
▪ Classes
▪ Arrays
▪ Interfaces
▪ Enum
▪ Annotation
• These are made and handle according to a programmer in a java program which
can hold the values like:
array type
// Points to an array instance
class type
// Points to an object or a class instance
interface type
// Points to an object and a method, which is implemented to the
corresponding interface
etc..
• Because objects are passed by reference, two different variables may refer to the
same object:
Button p, q;
p = new Button(); // p refers to a Button object.
q = p; // q refers to the same Button.
p.setLabel("Ok"); // A change to the object through p...
String s = q.getLabel(); // ...is also visible through q. // s now contains "Ok."
Array Type:
For example:
• In the first statement, an array variable "a" is declared of integer data type that
holds the memory spaces according to the size of int. The index of the array starts
from a[0] and ends with a[9]. Thus, the integer value can be assigned for each or a
particular index position of the array.
• In the second statement, the array "b" is declared of string data type that has the
enough memory spaces to directly holds the three string values. Thus each value is
assigned for each index position of the array.
interface Type:
Runnable r;
r = new Thread();
enum Type:
• The enum types were introduced in Java programming from Java Version 5.0.
• These are special classes that have a fixed number of instances.
• All these instances are known at the compile time.
• The name of an interface can be used to specify the type of a reference.
• An enum type is a type whose fields consist of a fixed set of constants. Common
examples include compass directions (values of NORTH, SOUTH, EAST, and WEST)
and the days of the week.
• Because they are constants, the names of an enum type's fields are in uppercase
letters.
• In the Java programming language, you define an enum type by using the enum
keyword. For example, you would specify a days-of-the-week enum type as:
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
public static void main(String[] args)
{
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}
The output is:
One-Dimensional Arrays
type var-name[ ];
int month_days[];
• To link month_days with an actual, physical array of integers, you must allocate one
using new and assign it to month_days. new is a special operator that allocates
memory.
• That is, to use new to allocate an array, you must specify the type and number of
elements to allocate.
• The elements in the array allocated by new will automatically be initialized to zero.
Example : 1 :
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.");
}
}
Example : 2 :
An array initializer is a list of comma-separated expressions surrounded by curly braces.
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
length variable for an array
• Specifically, the size of an array—that is, the number of elements that an array can hold—is
found in its length instance variable.
• All arrays have this variable, and it will always hold the size of the array. Here is a program
that demonstrates this property:
Output:
Multidimensional Arrays
.
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
Alternative Array Declaration Syntax
type[ ] var-name;
Here, the square brackets follow the type specifier, and not the name of the array
variable.
This alternative declaration form is included as a convenience, and is also useful when specifying
an array as a return type for a method.
//Creating instance for each element of an array for Dog class
for (int i=0; i<7 ; i++)
{
pets[i] = new Dog();
}
Conversion of Numeric Types
class Test {
public static void main(String[] args)
{
int big = 1234567890;
float approx = big;
System.out.println(big - (int)approx);
}
}
output
-46
Narrowing Conversion
The narrowing conversion occurs from a type to a different type that has a smaller size,
such as from a long (64 bits) to an int (32 bits).
• Sometimes you will want to pass information into a program when you run it. This is
accomplished by passing command-line arguments to main( ).
• A command-line argument is the information that directly follows the program’s name on
the command-line when it is executed.
• To access the command-line arguments inside a Java program is quite easy—they are
stored as strings in the String array passed to main( ).
• For example, the following program displays all of the command-line arguments that it is
called with: