#28 Creation of Array in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)How many way to create array in java?
2)default value store when we creating array with object notation
3) fetching the value assign different value array element

#1
Ways to create array in java
a)Literal notation
Literal notation: int[] arr = {1, 2, 3};
b)Object notation
Array constructor: int[] arr = new int[]{1, 2, 3}; // this is not literal notation ,this is object notation with assignment of value
Array constructor with size: int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; //in this we manually assign value but by default 0 is assign in this case

#2
default value which store array when we create using object notation for primitive datatype.

-- When you create an array of primitive data types in Java using the object notation, the default value stored in the array depends on the data type:

-- int arrays: default value is 0
-- boolean arrays: default value is false
-- char arrays: default value is '\u0000' (null character)
-- byte, short, long arrays: default value is 0
-- float arrays: default value is 0.0f
-- double arrays: default value is 0.0d

code for you --
char ch[]=new char[3]; //declaration and initialization

}

check result--

#4
fetching the element of array :
-- for traversing whole array, you need to know either length of array or know length property of array
-- using length property we get length of array
-- using index we can fetch all value of array

suppose we create int nums[]={2,3,4,5};
access first element then nums[0],
access second element nums[1],
access third element nums[2],
access fourth element nums[3];

-- if you match pattern for accessing the element
you get nth element is nums[n-1];
for 7th element nums[7-1]; i.e is nums[6]

-- in array position start from 0,1,2, go till n-1 if n is length of array
change value of given position

int nums[]={2,3,4,5};

} //traversing whole array -- means fetching all elements of array

nums[0]=10;
nums[1]=11;
nums[2]=22;
nums[3]=33;

}

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

just a mention, int nums[ ] = new int[4]; is C style array declaration. java style is int[ ] nums = new int[4]; Both are correct btw

justdevi
Автор

how to create new array as user input as identifier array ?

ricotabaque
Автор

i have question why index value start with zero.

MadhukumarAp
Автор

public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5};
for(int i : a){
System.out.println(i);
}
}

mso_studio_official