How to Declare and initialise Arrays in different ways | Java Programming Tutorial

Sharing buttons:

hi this is Anil and welcome to the video

tutorial on Java programming so in the

last tutorial you know we have learnt

about the RS in Java so what is an array

you know what is the use of it and also

you know how to create and use the RS in

our program so in this tutorial I'm

going to teach you guys the different

ways of initializing the RS so let's get

started so the first thing is you know

in the previous tutorial we have learnt

you know we can create an array by

writing that iterate first and then the

re name and let's say marks and then by

using the new operator and then a pair

of square brackets and then the new

operator and then the data type and you

know we were passing how many elements

you want to store in that array so this

is going to create an array which is

going to be called marks and it can

contain six values which are going to be

of type integers so in a previous

tutorials you know I have explained you

know how to store the values in the

arrays and you know how to read through

that so we were using the array indexing

so you know we were writing the array

name first and then the square brackets

then we were specifying the index you

know the first element was stored in the

index 0 and we were assigning the value

for example let's say 22 any values and

similarly you know the second element

was at the index 1 and you know like

this you know the last element was at

index 1 less than the size of the array

so in this tutorial I'm going to teach

you some other methods which are used to

initialize the array so here what we're

going to do is we're going to initialize

this marks array in another way in Java

you can create and initialize RS you

know while declaring it so how to do

this so first you need to write the data

type and then the array name and then a

pair of square brackets then the equal

to symbol and then instead of writing

this new and data type and the RSI is

what you can do is you can use the curly

braces and here

you're going to write the elements that

you want to store in this array for

example you know I want to store

the marks of six subjects so I know the

marks right here so what I'm going to do

is I'm going to inter that so let's say

44 and then 5588 99 66 44 again alright

now we have the six elements so this is

another matter of initializing the

arrays and which is pretty easy and also

I know it reduces some coding effort so

here this 44 will be stored in the index

0 then this 55 will be present in the

index 1 and similarly this 44 will be

present at the index 5 because you know

we have six elements here all right we

can access the value stored in this

Mark's array just like the way we were

accessing in the previous tutorial so I

just connect system dot out dot print

line and you know I just cannot refer

the Mocs array and you know when you to

refer the index let's say 2 and I'm

going to save it and I'm going to run

this and you guys can see we get 88

that's because you know the index 2

refers to the third element all right

the next thing that we can do is you can

calculate the total marks here if you

guys want and I can teach you that very

quickly so what we're going to do is we

can create a variable and let's call it

as total marks and let's initialize this

one with a value of 0 and then what

we're going to do is we're going to use

a for loop so it's going to be 4 then we

need a loop counter variable and let us

call it as int counter equal to 0 that's

important because you know the indexing

starts from 0 and then we can have the

counter is less than length of this

array so we can get the length of this

array by using a property called a

length so we can use that by specifying

the order name first

you know the marks and then the dot

operator and then the length all right

then you know it is going to increment

our counter

variable so it's going to be counter

plus plus and here is going to be total

marks equal to let's say plus equal to

and it's going to be maths

you know the array name and the counter

alright what we are doing here is you

know we are looping through all these

elements and we are adding that value to

this total mark so variables and that's


so here I can print out that so it's

going to be total marks eyes and then

it's going to be total marks all right

now if I run this going to get 396 so

I'm not going to count it and it's going

to be the correct value all right so

this is it guys you know this is how you

guys can initialize the array in

different ways and also you know this is

how you guys can loop through the

contents or you know the values of an

array and you know it can now perform

some operations on them alright in the

next tutorial I'm going to eat you guys

how to receive the input for the array

from the user so thank you for watching

and don't forget to my channel and I'll

see you in the next tutorial