- Podcasts
- The Java Developers Hub
- Java Arrays Explained | Declaration, Initialization, Traversal & Introduction to Strings
Java Arrays Explained | Declaration, Initialization, Traversal & Introduction to Strings
The Java Developers Hub
- December 3, 2026
- 1 h 47 min
- Episode 14
About this episodeArrays are essential data structures in Java that allow developers to store multiple values of the same data type in a single variable…
- Understanding arrays and why they are important in Java.
- How to declare Java arrays.
- Different ways to initialise arrays.
- Accessing array elements using indexes.
- Understanding zero-based array indexing.
- Traversing arrays using for and for-each loops.
- Updating and modifying array elements.
- Finding the length of an array using the length property.
- Understanding common array errors and ArrayIndexOutOfBoundsException.
- Introduction to Java Strings.
- Declaring and initialising String variables.
- Understanding the difference between arrays and Strings.
- Practical examples of working with arrays and text.
- Beginners learning Java programming.
- Computer science and software engineering students.
- Developers strengthening their Java fundamentals.
- Students preparing for Java programming examinations.
- Anyone interested in learning data structures and managing collections of information.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Arrays Explained | Declaration, Initialization, Traversal & Introduction to Strings
Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to start with a new topic, Arrays and Strings. Now this topic is very simple and if you come from any other programming language, then you will know what arrays and strings are. And if you are new to this topic, then it's okay. We will understand it well and basically and everything will be crystal clear to you.
Yes, so we are going to see what arrays are. Now any programming language has an interesting topic of arrays. So let's see it from the starting. You will enjoy it a lot. Let's suppose you want to write a program in Java and you want to store the role numbers of students in it. So I will make an integer variable And I can store a student's roll number in it And we will give a value to it Let's say a student has a roll number of 1 I want to store a roll number of 4 students We will need 4 variables I will make one more variable Let's say roll number 2 Let's call it roll number 1 equal to, let's say whatever new roll number will be, let's say its roll number will be 2. Similarly, I will make one more end roll number. Let's say this time the roll number will be 3.
Why are you allocating different memory? What if I allocate a big memory like this and divide it into sections? First section is to store roll number 1, second roll number 2, third roll number 3, fourth roll number 4 and so on. I allocate a big chunk of memory which I call contiguous memory. Contiguous means 7th memory. One after the other. So if this memory location is 0, then it will be 1, 2, 3, 4, 5, and so on. I will allocate such a memory, and you can add the first child's row number, second child's, third child's, fourth child's, and so on.
To declare any type of array, you first write the type of array. Now you are telling us that the array we are going to declare in a memory, which we call contiguous memory location, will have every element of an integer type. Basically, this array is an integer array. It will store integers. For example, roll number 1, 2, 3. These are integers. So, I have to make.
Consider an array as a collection. I have to make a collection of these three. So, the first one is detribe. After this, we will make a curly bracket. Sorry, we will make square brackets. This square bracket denotes that we are going to make an array. Because if you would have written normal int, it would have been of int variable. If we put square brackets in it, then we will understand or the compiler will understand that we are going to declare an array.
Numbers and every element in this collection all these elements will be of integer type meaning we will store an integer now see why do we have to tell the compiler that we have to make an integer array and then its name meaning why can't we make an array normally why do we have to tell that it will be of integer type now simply understand this you did here that you told the compiler that we have to Collection, we are going to make an array in which all these elements will be of integer type. The way you told us, why did you tell us? It's a simple thing, we have discussed about it. Imagine that this is your one memory, this is your computer's memory. What will happen in this memory? Whenever you declare a variable, let's say you declare this variable as rule number 1. So what will happen? First of all, a container will be created. Now here is an integer type variable, so it will be a 32-bit container.
So 32 bits, it has allocated the memory for you. And it will say, you can put any value in this memory. So you put here, let's say 1. Something like this. Now, When you are going to make an array, roll numbers, then it should also know how much memory I have to allocate. Although we have just declared it, not defined it. Now we will define it too. But let me tell you a little bit that the computer should already know how much memory I have to allocate, how much memory I have to store or how much memory I have to occupy to store your roll numbers. So suppose you have to put 3 elements in this array. You have to put row number 1, row number 2, row number 3. Like we put it above. I will tell you the syntax of how we put it. But suppose you have to put 3 elements in this array. And I said that every element is an integer. So you have to put 3 elements and every element is an integer. So you know that first of all you will need 3 cells of 30 to 32 bits. Like here you took 1 of 32 bits. Here you will need 3 of 30 to 32 bits. So this way our compiler knows how much memory we have to allocate to store our values. So here I can store the row numbers. 1, 2, 3. Something like this. And this is the whole concept of arrays. So if I say in the normal language, what is arrays?
If I write it a little, arrays is nothing. Collections are of a particular data type of a data type like I have made an integer collection here I have made a collection of a row number and every row number is an integer so there are collections of a particular data type now in that collection we can store multiple values like I have stored row number 1,2,3 separately I can store them together here 1,2,3 like I have shown you here so now let's see here I have declared this variable this is the variable declaration.
Absolutely correct. Now how do we define it? So its syntax is a little different. Now you might have to understand one or two things in this definition. But it's okay, we'll study it in depth later. But now you just have to understand how do we declare. So you write int, bracket, roll numbers, whatever is the name of your array.
Now look at this carefully. You write a special keyword whose name is new. You wrote the keyword as new. After that you write int again. And make square brackets. And close it. And there will be one more thing in the square brackets. Let's say I write 3 for now. And what does this 3 tell? This 3 tells size. Size of your array.
Now, obviously, many people don't know what heap memory is, right? So, we will study all that later. When we have studied objects and classes, then we will understand about memories and understand what a new keyword is. But for now, let's assume, again, like a black box, that we have to write a new keyword. Now, finally, we will tell int3. Here, we will tell size of array. Okay, so I hope you have understood everything that how we did the declaration. Okay, by combining all these, your one array is defined.
Like you define a variable, means if you want to declare a variable, then you write int x, right? If you want to define it, then you write x equal to 4, right? In the same way, if you want to define an array, just declare it, then you just write like this, roll number, And if we want to define it, then the role number is equal to new and int and its size.
I hope you understood this Now whenever we write this line So I told you what is happening in the backend through the compiler It will see the size of the array, it is the size of 3 arrays It means I have to allocate 3 memory Like this. 1, 2, 3. So, I have allocated 3 memories. Now, how many bits will each memory have?
Similarly, when we write int let's say array equal to new int of size let's say 3. So I want to say here that I have to allocate a memory of 3 sizes and each memory will be of 30-32 bits and its name will be ARR So this is exactly the same, what I did here, I did that here. Here I just allocated one container of 32 bits, here I allocated three containers together, 32, 32, 32, okay? And named it ARR. Now remember, this memory will be allocated, it will be contiguous. Okay, what does contiguous mean? First is this container, after it, the second, after it, the third. It won't happen that one container is here, the second container is here, the third container is here in the memory.
These three will be one after the other, their memory address will be one after the other. So, let's say its memory address is 1001, then it will have 1002 and 1003. Something like this. Obviously, after 1001, 2 will not come directly. Because after this, you will wait for 32 bits and then 1033 will come. Something like this. But you understood, the basic concept is that memory will always be contiguous. This is the concept of array. Now, how will we put value in it? We will see that later, how to allocate value in it. But I hope you understood the basics.
What are arrays? They are collections of data types. We have written collections of data types here. After that, how do you declare an array? We have said that you declare and define it in this way. And what is its meaning? Its meaning is this, like the meaning of a variable. Now, let's go inside the arrays. Okay, very good. So, first of all, let's take this example with the same rule number. Let's say I have three students. I have one roll number, let's say 101. I have one roll number, I have 102. I have one roll number, I have 103. I don't want to represent this in different variables, I want to represent it in an array.
Let's write it a little correctly. new hint of size 3. This is how I write the first line. Do you know what will happen? I will have an array of 3 sizes. This is how it will be. Its name will be rollNumbers. Like this. Very good. Now let's see, we have declared an array. Now how do we add values to it? Suppose I want to add 101 in its first cell.
I will write the syntax here. First of all, I will write the syntax. Role numbers, whatever the name of our array is. Our array's name is role numbers. I have to put the value on its zeroth index, 101. I will write something like this. Now, what did I say here? On zeroth index. First of all, I wrote the name of my array. Then, I opened my square brackets.
Means it has 3 containers of 32 bits and I also said this is a contiguous memory one after the other memory is allocated now how will I know which cell or which container I am talking about For that, whenever you declare an array, there are indexes in it. For example, its index is 0, its index is 1, its index is 2. Now, index is nothing. Index means you have to go to a particular cell. So, you will give a number of that cell. So, I gave its number 0, its number 1, its number 2. Now, Internally, in Java, the indexing of the array always starts with 0, not with 1. So, you call the first cell as 0, the second cell as 1, and the third cell as 2. So, if you ever want to put a value in the first cell, then you will write it like this. Roll numbers of 0. Roll number is the name of the array. In which index do you want to put the value?
You must be able to understand how we have filled the values. Now we have filled the values. But now I have to retrieve the value. So how will I do that? Like it is the same thing that I have put x equal to 4. But I want to know what is stored in x. So what I used to do is system.out.println. I used to print x and I used to get the output on the fore screen. So I used to get to know from this. But now how can I print this?
And suppose I want to print the first element So I will write the name of my array which is rollNumbers And I will write 0 in the bracket So my screen will be printed as 101 In the same way I can do rollNumbers 1 And my screen will be printed as 102 And in the same way I can do rollNumber 2 And my screen will be printed as 103 Something like this What can I do? Absolutely, I can do it. So, first of all, I declared the array. I also defined it like this. I also added the values in it and retrieved those values.
But still, you are understanding a problem. Why did I make an array? So that I don't have to make a lot of variables one by one. I can make a container in one and store all the variables in it. But let me tell you one thing, I made a container of three lines from this line. So I didn't have to write it separately. But when I had to put a value in it, see how I was putting it. I was putting it one by one. Now let's assume that it is of only three sizes. But it would have been an array of 100 sizes. I could have put 100 elements in it. So, if I would have put them one by one, then I would have put row number 3, then 4, then.
By doing this, I would have gone to 100. So, my manual work still increased. Same when I had to retrieve, I would have retrieved one by one. Manual work increased again. Now, remember here, what we can use, that we don't have to put the value one by one, but we start storing all these values together. Means, we use some block, and write some code in it, and fill all the values one by one. Remember, we read in the last lectures, loops.
So I wrote this code. Look at this code carefully. First of all, you must have understood that I have run a loop from 0 to 2, basically 0, 1 and 2. Every time I put the value of x in the row number i and kept doing x plus plus. So the first time the value came was 101, the value of x was 101. After that, the value of x became 102, the next time it came 102, the value of x became 103, the next time it came 103. Now the value of x will be 104, but as it will go again, now the value of i was 0, 1, 2, now i will also be 3, i equal to 3 will be done, now i equal to 3 will fail here, so we will come out of the loop, right? That means I filled 101, 102, 103 inside the array, right?
So I hope you understood how we can use loops to fill any values. Now think, if I want to print the entire array one by one, then also can't I use loops? Of course I can. I can make the same loop in the same way. For int i 0, i less than 3, i plus plus. I can open the loop in the same way. But this time I will do system.out.println. And what should I write? Whatever our roll number was, roll numbers of i. Keep printing roll numbers of i. So all three will be printed one by one. In the first line, it will print 101, 102, 103.
So I wrote here I less than 3 If I made an array of 4 sizes So its index would be 0, 1, 2, 3 So I write here I less than 4 If it was of 100 Then I less than 100 I write like this Okay, but tell me one thing, should I make an array of any size? Do I need to hard code it here? Hard coding means that I should know the length of my array here. I will write the same number here. What if, where I am writing a loop in my code, I don't know the length of the array of this roll number? This can happen, right? You have a very big code base. There are multiple arrays in it. You don't know what its length is. What is the length of your row numbers array? And even if you know it, you don't want to hard code it here because its length can change tomorrow.
If I write like this, Do you know what dot length will give us? Dot length will give us its size. It will print 3. Because there are 3 elements here. If there were 5 elements here, then it would give us 5. So dot length is an amazing thing. Whenever you write dot length in front of an array, first the name of the array and then the length, then we get the full size of the array.
I don't want to loop in the entire array, I just want to print a particular element. So, you will say, no problem. Now, we can write this, system.out. println, okay which element do I have to print? Let's say I have to print 103, so I will write role numbers and which index do I want? 2, I will write 2 here, second index, so its output will be 103, because here 103 is stored, okay what happens, I will write system.out.println and I will write role numbers And write index 3 here which doesn't exist Or index 4 which doesn't exist If I write an index which is outside the limit of this array Then I will have an exception throw whose name will be IndexOutOfBoundException Now the first question that will come to your mind is what are exceptions? There are no exceptions. Java has a way of telling you that something is wrong. Now what is wrong here? The error is that the array's length was only of 3 sizes. In that, the indexing was 0, 1, and 2. You tried to fetch an element which is outside the limit of this array. So it said index out of bound. And if you put word with exception, then the total is index out of bound exception. So, exception means that Java compiler wants to tell you that there is something wrong in running this program. Now, exceptions will be made in depth in a different video. Later, it will be made that what are exceptions and what types are there. But now, if you see any exception on the screen, Any kind of exception, like index out of bound exceptions, there can be many more types of exceptions. But if you ever see an exception on the screen, you should understand that the compiler wants to give you a message that something is wrong. Something is wrong, you fix it. So whenever we try to fetch an element which is outside its index, we will get an exception, index out of bound exception.
Quickly go to the screen, code it, and then come back and see what multi-dimensional arrays are. So again, we have made our project empty by doing demo.java and we have written our boilerplate code here. So first of all, we have to declare an array. So to declare an array, we are making this syntax. So role numbers equal to new int of size 3.
Now, what did I tell you? If I want to put values in it, then one way is to put rollNumbers. First, I'll comment it and show you how our array is going to look. There are going to be three variables in our array. 101, 102, and let's say 103, something like this. So I can do one thing that I will put the roll numbers of 0th index in 101 and similarly I will put the roll numbers of 1st index in 102 and roll numbers of 2nd index in 103. 103, something like this. Okay, I have added the value. Now I can print it like this. What if I write system.out.println. println and how do I have to print it? roll numbers of, let's say 0th index. Sorry, 0th index. Something like this. And by doing this, I can print first as well as second. So first and second. Here I just have to change their indexing. First and our second. Now, if we run this once, then you can see 101, 102, 103.
And we talked about another method that we can use for loops. So, let's do one thing. First, let's do int x equal to 101. Which we saw earlier. So, for int i equal to 0. Because indexing starts from 0. How far will i less than go? What did we talk about? Roll numbers dot length. Because its value is the size of the array. So now it will be 3. And every time we will keep adding the array. And what we have to do is roll numbers of i-th index. So i-th index means first it will be 0. If the value of i is 0, then it will be 1. If the value of i is 1, then it will be 2. 0, 1 and 2 will run only 3 times. Because as soon as 3 happens, it will be equal to the roll number dot length. And here only less than operator is there. So it will come out of this loop. Okay, what value do we have to put? We have to put the value of x and every time we have to make x plus plus. Something like this. So I hope you have put the value by now. And then using the loop in the same way, we try to print the array. What does print mean?
Let's assume that Java always prefers this way. But this way is also supported because it was supported in the old language. This is how you make an array in C, C + +. So to support a legacy, it has supported this method as well. So you can put this array sign in front of the roll number, which is square bracket. Or you can put it in front of the integer, which is the data type. And again, the rest of the things will remain the same. If you run it again, the output will be the same.
This type of array is called in the language of computer science, one-dimensional array. One-dimensional array. Okay, what are the dimensions in this? The dimensions are that we are telling the computer that I want a contiguous memory. But in that contiguous memory, every element should be an integer. Right? We have written this here. We want a contiguous memory and every array's element should be an integer. Okay, now this is 1D array. So, what is 2D array?
I am saying that I can call this array of arrays. Like, what did I call 1D array? Let me write it here. I call 1D array array of any data type. Let's say int in this case. Here we made array of int. It was an array in which every value was an integer. Now think what I am saying here. I have an array in which every element is an array itself.
So, first of all, I want to say that I will have an array. Let's say, it will be of three sizes for now. And in that, this element will not be an integer, but it will be an array in itself. This is what I call array of arrays. Now, this array itself, its element can be an integer. Same, its element can be an integer, its element can be an integer. In fact, all its elements will be integers. All of its and all of its. Basically, the one-dimensional.
And this represents our two-dimensional array. Because every element of it is an array itself. So, if you didn't understand the definition, it's okay. Now we will declare this array and show you. You will be able to understand more things. If you are wondering why we need a two-dimensional array, So the answer is for a lot of use cases. In fact, sometimes we need a 3D array, we'll see that later. But let's assume that you need multidimensional arrays. Okay, now let's go back to the starting and see how we declare this 2D array, how we define it, how we define it and how we store the value in it. But before going there Let's take an example of why we need a two dimensional array. For example, we have to store the student's roll number. We have stored roll number 1, roll number 2, roll number 3. And you know what are all the roll numbers?
This student is S1, this student is S2, this student is S3. Now, let's say there are some marks in these three subjects. So, let's say this is your subject. I will write it in different colors. This is your subject, Hindi. This is, let's say, English. And this is, let's say, your computer science, CSC. So, I am saying, how many marks does student 1 have in Hindi? So, let's say his marks are, let's assume 50 for now. Let's say student 2 has 60 and student 3 has 70. Here, their marks will be 30, 40, 50 and 90, 80, 70. Their marks are like this. And I have to store this data in my memory.
Column, represents a mark So I will have to put 3 marks here 1 mark won't work with 50 marks So if I am representing this for student 1 Then I will have to put 50, 30 and 90 marks Same, if I am representing student 2 in this I will have to put 60, 40 and 80 marks And so on for this one as well I will have to put 3 marks for this one as well So now think, how can I put 3 elements in one cell Which has every element as an integer.
Let me say that my cell is an array itself. It is an array of three sizes. And in that array, I am putting 50, 30 and 90. Sorry, here is 70, right? Yes, 90, sorry, it is 90. I am putting 50, 30 and 90. If I say this, then my work will be done. And from here, the concept of two-dimensional array arises. That such an array, if you look carefully, a lot of things. So, I say such an array, okay? where every element is an array and I have stored the marks or any integer in that array. So, in this case, 50, 30, 90. Similarly, this cell is an array and it has three values stored. What are those three values?
Stores 70, 50 and your 70 so here 70 will come here 50 will come here 70 will come so okay I hope you understood what we want to say every cell is an array every cell is an array see I did one array here one array here one array here And in every array, I have added integer values. Something like this. So this is the concept of your two-dimensional array. And I have also told you how we use it. That if our data is multi-dimensional. Because we have two variables. One is student's marks. And marks are also in three subjects. Three students, three subjects. So I said let's make an array of array.
Now let's see how to declare and define this Let's say we want to store this information, remember this table Remember this table, we will use this again and again That I want to store these marks in this array So let's see how to declare a multidimensional array I will write integer of Now if I make a one dimensional array, then I make one square bracket. Now because I have a 2D array, then I will make two square brackets. Something like this.
Now we will see later how we can change this. Can I make this and this of different sizes? We will see everything. So I made an array of 3 x 3. Now think conceptually what I would have done. conceptually if we think about this diagram then how will our array look? I am thinking how will it look in memory so in memory our array will look like a matrix something like this I have made a matrix of 3x3 so let's say here I am representing the student and here I am representing their marks in the subject So my student S1, let's say this subject is Hindi, this subject is English and this subject is CSE. So student S1, S2, S3, this is Hindi, English, CSE. So S1' s marks here are 50, 30, 90. Let me write here 50, 30, 90.
One student will have marks of 1, 2, 3. Similarly, student 2 will have marks of Hindi, English and CSE. Then student 3 will have marks of Hindi, English and CSE. In this way, some marking will be done in the computer. Now I will tell you how it works in reality. Conceptually, when we try to visualize a two-dimensional array on our screen, we will do it in the form of a matrix. And we can write here that the size of our array is 3x3. 3x3 means it has 3 rows. See, row 1, row 2, row 3. We call them rows.
This is my R1, Rho1, this is my Rho2, this is my Rho3. This is my column 1, this is my column 2, this is my column 3. This cell of mine, what is it representing? Rho1, column 1. This cell represents row1, column2. So if I do indexing of this. So let's say I do indexing like this. Column1 is 0, 1, 2. Similarly here also 0, 1 and 2. Let's write it a little clearly. So for now I say this is my 0, this is my 1th index, this is my 2nd index, same, this is my 0 index, this is my 1 index, this is my 2nd index, so I can do its indexing in this way, like I did my 1 dimensional array indexing, how did I do it? I declared an array like this, I said this is 0, this is 1, this is 2, now here I have 2, 2 dimensions, so I did indexing.
There are 3 marks here. So, you can see this too. And you can see this too. This is its conceptual view. That how my rows and columns are arranged. If you understand these two things, how I defined it and how conceptually I am imagining that our array will be stored, then I can store the values in this array and I can also retrieve.
Because it has 0th row and 0th column. Let's erase it from here. Similarly, what will be the column of this cell? 0,1 because it has 0th row and 1st column. And 0th, 2 because it has 0th row and 2nd column. You must have understood that after writing this line, what did I imagine? I imagined that in the computer, this would have been done. This much work would have been done. Now I have to fill values in it. I have to fill these values. So how will I fill? Okay, let's think. First let's remove this. And let's see this again.
Now, how will we fill a value in a two-dimensional array using loops? If we run a single loop, you know, if we run a single loop, for i equal to 0 to i less than 3, i plus plus, if we run a loop like this, then it would have filled a row, right? Then we would have to run another loop to fill a second row, we would have to run another loop to fill a third row, right?
First one is 101, second one is 102, third one is 103. So what did I do for that? I declared a variable x equal to 101 and kept doing x + + every time. But here we can't do anything because all the numbers are different. So obviously, we will have to fill them individually. We will have to fill them individually. So let's see how to fill them individually. Let's say for now, I will write the value again.
So, my array name is marks, I have written, in my marks array, which value I have to fill first? First, I have to fill this value, which means row 1, column 1. So, I will write, in 0th row, because you know, our first row tells, in 0th column, second column tells, here I have written, first row tells, The first row tells, the second column tells. So I wrote in my 0th row, in the 0th column, I have to fill a value which is 50. So we must have understood. 0th row, 50 in the 0th column. So I can fill the value in this way. Now suppose I have to fill the 0th row in the first column. That is, 0th row, this row and first column. I have to put 30 in this. So how can I write again? I can write again marks 0th row 1st column equal to 30. Here 30 came. Similarly marks 0th row 2nd column equal to 90.
So I will write marks First row, means I am talking about this row. 0th column, means I am talking about this column. I have to put value of 60 in it. So, I can write like this. Now, you must have understood how I will keep writing the rest of the values. I just need to remember row number and column number.
First of all, let's copy this matrix down. So, what I did was, I simply printed it down again, the matrix that we had made. Now I have to print this matrix and I need 2 loops which we call as nested loops. So what I can do is that I will go like this that first I will print row 1 basically 50, 30 and 90 then I will print row 2 60, 40, 80 and then I will print row 3 70, 50, 70.
If you have practiced patterns in loops, then you must have understood how to do this. So, let's say, first of all, I write outer for loop for int, let's say, row. int row equal to 0. How far will row go? Less than 3. row plus plus. Something like this. Our row will start from 0 and go till 3. So this is our outer loop. Now what can I do after this?
And sorry, row with row and col, column with column we are trying to print this ok, so what will happen in this first of all, our flow of control It will come in this loop, it will define row as 0, then it will come in the inner loop and define column as 0 And it will print marks as 0,0 which means it will print as 50 Then this loop will run again, then it will say that row is still 0 and I have done the value of column as 1 So it will print 0,1, now 0,1 is 30, so it has printed 30 Then this loop will run again, row is still 0 and column value is 2. So it will print 0,2 which is 90. Once one row is finished, this loop is finished. Now outer loop will run again and row value will be 1. Now this will work for one row. So 1, 0, 60, then 40, then 80. It will print like this. Then again this inner loop will end. Then again outer loop will run. This time the value of row will be 2. Now we will print the values of second row. We will print 70, 50, and 70. And this way our work will continue. Again, as we used to do in pattern printing. Once this loop ends, we will have to put a new line. So we can write here system.out.print. So that we can add a new line. To print each row, like we printed one row 50, 30, 90. After that, we change our line and come here. So that we print 60, 40, 80. Then we change the line and come here. We can do something like this.
But logically, when you look at it in memory, it won't be represented like this. Let me tell you how it will be represented. Think about what memory will do. Whenever you write, marks, or let's start with this. Let's say I write int, I made a 2D array, marks, and write it equal to new int, Rows and columns.
As soon as I make it, my compiler will go to the memory and tell me the size of the array to be made. Remember, for compilers, no row and column concept exists. This is for our visualization. Because humans like to represent everything in a pictorial form. Because our brain works like this. Our mind will look at this. And like we read matrix in Maths, we look at matrix and say that.
It has some rows, it has some columns. Our mind thinks like this. So whenever we represent a data in the form of a matrix, we do it this way, by making tables. So remember, when we read SQL, query languages, we imagine data in the form of tables and rows. But actually data is not stored. In the same way, data will not be stored in the computer. Computer has neither a concept of rows nor a concept of columns. It has a concept of contiguous memory location. Remember, I am explaining this a little internally. Memory location So how will you store it from the computer? The computer will directly think in its memory That I have to make an array of marks And its size is 3 So basically what I have to do is I have to make 3 containers Very good Okay, I will make this But what type of container will my every container be? Will it be of integer type? No My every container Is itself an array Do you remember what I said in the beginning? My every container is also an array This one and this one. So what I have to do is, first of all, I have made an array of three sizes and every element of it is storing another array. So let's say this one is also storing an array. Its size is also 3. 1, 2, 3. This one is also storing an array. Its size is also 3. 1, 2, 3. And this one is also storing an array. Its size is also 3. 1, 2, 3. The memory will be allocated in this way in the computer.
Let's say, the computer will first make an array of three sizes. Because it knows that it has to make three containers. And what should be the size of each container? I will tell you that when we have read references and objects. Because these arrays will make an array of objects. But don't go so deep into what objects are. Just understand that as soon as the computer makes an array of three sizes.
Because we have told it that we need an array of three sizes. Now, when I write the next three, I will tell it that every array, sorry, every container, this container, it is storing an array itself. So, let's say it will say, oh, so I basically have to put one more new array in this container. And similarly, I have to put one more new array in this container. And similarly, I have to put one more new array in this container. Something like this it will think.
This first container of marks is called marks. So the first container of marks, I have to go to its 0th index. This is 0, this is 1, this is 2. Let's write indexing here. This is 0, this is 1, this is 2. I have to go to 0th index. Now, as soon as I reached this index, it is representing an array itself. And in this new array, the indexing will be.
Then, when you write marks 0, 1 equal to, let's say, what have you written? 0, 1 equal to 30. So, you write 30. So, what will it do? First, this one, It will go to the 0th index of this array. It will say that I am standing here. And it is representing an array itself. And it will go to the 1st index. Meaning here. And it will put a value here.
Then you will understand the depth of the array. How things are stored. Things are not stored in rows and columns. Like this. Computer doesn't work in tables. Computer works like this. So we have to do contiguous memory allocation. And we have to tell it that every element. itself is an array, so it has pointed it towards a new array. Now this array has been pointed towards this array. This array has been pointed towards this array.
But to explain that, first I had to explain to you that how exactly things are stored in the computer. Because if you understand this, then you will understand how I will write those loops with dot length. So now imagine this data again. And suppose I have to write our loop in this way. So I will do one thing, I will copy this again. So what I did is, I copied this loop again, that how I printed row less than 3, column less than 3.
This was the data. Let's copy this once. So I copied this too. We ran this loop and we tried to print this data. 50, 30, 90, this way and this way. Now I am saying that instead of row less than 3 and column less than 3, I have to write something else. I have to write in the same form that dot length is dot length. So how can I write? Now I have definitely explained this to you. If you understand this, then you will understand what will come here and what will come here. According to the computer, it has an array whose name is marks. I will write it here. The name of this array is marks according to the computer. And the length of the marks is 3. 0, 1, and 2.
So what I mean to say is, if I write marks of 0 dot length on the screen, then our 3 should be printed. Similarly, if I write marks of 1.length then also our 3 should be printed because marks of 1 is this itself is an array and if I will put dot length operator on it then it will also give me the size of that array and in fact I can simply put dot length on marks too marks dot sorry I can simply write marks dot length I can write it like this also. Because marks is also an array. So I can write dot length on the name of this marks. I can write dot length on the name of marks 0. I can write dot length on the name of marks 1. And dot length on the name of marks 2.
So I hope you understood this. So if you understood this. Then it is exactly the same. What I will write here. First of all. This is our outer loop. This is our outer loop. Instead of this I will write 4. int row equal to 0 and row less than marks dot length marks dot length row plus plus so I will write this in outer loop ok marks dot length means I am saying that I have to run this loop till its length till its length till the length of this big array first thing ok very good then in the inner loop in the next loop I will go for int Column equal to 0.
On its row, now marks of row, means basically, first time I am talking about marks of 0, next time marks of 1 and next time marks of 2. So, first time this row's value will be 0, so I will talk about marks of 0. Next time this row's value will be 1, so I will talk about marks of 1. And next time row's value will be 2, next time means when this outer loop will run again.
I am saying that every time I have to go to marks of row dot length and column plus plus. Something like this. And I will print the same inside here. But I just told you that instead of row less than 3 and column less than 3, I wrote row less than marks dot length, column less than marks of row dot length.
Because our computer is storing this data from inside. Something like this. It is saying that I have only one array of marks. Every element has its own array. So I can add dot length on this too. And I can add dot length on this as well. I can add dot length on the first array as well. And I can add dot length on the second array as well. I can use dot length operator to loop in my entire matrix. So remember that this is just a conceptual overview which we imagine how it will be stored. And this is a logical overview. So actually the way data is stored in the computer.
If you understood this conceptually then you understood everything. But still you need to understand this. That why we are putting marks dot length here. And why we are putting marks of row dot length in the inner loop. It is important to understand this. Otherwise, you will imagine that this is also going to 3 and this is also going to 3. So, I can write something like this. Marks.length and inside also marks.length. You will do like this. Yes, it will work for this because marks.length's value is 3. And here if you write marks.length then also it will work but it will be wrong when every row is of a different length and yes you can do this too we will see how we can do this but I hope you understood how you represent a two dimensional array if you understood this whole story then we will move a little further and see one more concept Let's say this was our two-dimensional array. Which array?
We said int two-dimensional array marks equal to new int and we gave here rows and columns. I am saying that giving both these values together is optional. If you want, then you can give only one value which is row. Giving row is compulsory. Meaning, this number of rows, what is this? This is compulsory. Meaning, while defining any array, you will have to tell that how many rows are there in it.
Of zero marks of zero means you are telling the compiler that you must have made a big array of three size whose every value is an array itself this is index 0, index 1 and index 2 so I am talking about this zero one that what is the size of this array itself so you can tell its size separately marks of zero equal to new again new operator Int of size, let's say 2.
So, you can say new int of size, let's say 3. So, its size is 3. So, you are seeing, I am declaring different sizes here. Let's make it a little lower. So now comes the next value, so I can write marks of 2. Means the second index which is an array itself, what is its length? So let's say I make it length 3 or 4, new int 4.
How I had imagined it earlier, I had imagined it 3 by 3, 3 rows, 3 columns, so row 1, row 2, row 3, similarly here column 1, column 2, column 3, earlier I had imagined it like this, but now the data is not like this, so see, I have rows here still 3, see, marks 0, marks 1, marks 2, because you know the earlier one tells rows, this 3 tells me rows, So I have to represent 3 rows in this way.
I hope you can understand why I have done different representations like this Because in the first row, I have C1, C2 In the second row, I have C1, C2, C3 In the third row, I have C1, C2, C3 and C4 If I write their indexing again This is my 0, this is my 1, this is my 2 This is my 0, this is my 1, this is my 2, this is my 3 So I will write 0, 0, 0, 1 0, 0, sorry 1, 0, 1, 1, 1, 2. Similarly, I will write 2, 0, 2, 1, 2, 2, and 2, 3. Something like this.
How can you believe that I have multiple students and each student has different subjects. Some have 3 subjects, some have 4 subjects. So, the one who has 3 subjects, he will have 3 marks. The one who has 4 subjects, he will have 4 marks. The one who has 2 subjects, he will have 2 marks. So, I have different students, but each student's marks... The number of subjects are different. So, the number of marks are also different. Right? So, for that case, I need a matrix whose length of every column is different.
This is my physical representation. or logical representation that how the computer is storing it from inside. It is not storing in this form. So it is storing in such a way that it makes a big array which has three containers. And every container itself is an array. And the sizes of every container are also different.
And every index itself has an array. Whose size is different. The size of the first one is 2. The size of the second one is 3. And the size of the third one is 4. So I make it in 4. Something like this. So I will call this as marks0, marks1, and marks2. These are the names of the arrays. This is its actual representation which the computer does in memory. So if I call this as marks0, then I will have to call this index as marks0. So I will call this index as marks0 array. its zero index, means this one. Okay, if I have to say this, then I will say, marks two is the name of the array, and its first index. So, marks two and its first index. So, this one is there, because this is zero and this is one. Okay, so if you have understood all this, then you have understood the concept of all the arrays very well. After this, one last thing remains, that, okay, you imagine, like this, if I say, what a 3rd dimensional array is. Multidimensional means, it can be of any dimension. Now, what does third dimensional mean? I will have an array declaration of this kind. int, 1 bracket, 2 bracket, 3 bracket. And let's say, I can say anything. For now, I will just call it array. Equal to new int. Now, three things will come in this.
So, it made an array of three sizes. Its name is ARR. Now, the computer understands that That the array of 3 sizes that I have made Each element of this is an array itself Because I have put it in the second square bracket So it will understand that this itself is an array This itself is also an array And this itself is also an array How many size arrays are these? These are also 3 size arrays because I have already defined it as 3 So it also made an array of 3 sizes.
Let's write the indexing of this as 0, 1, 2. 0, 1, 2. 1 and 2 and it also has 0, this is 1 and this is 2 I have written indexing in this way Then it will again go to this element And it will say that the list of my second arrays A, R, R0, A, R, R1 and A, R, R2 Each of its elements is an array itself So this is also an array, so it means again you must have understood This is also an array And again it is of 3 sizes, in that this is also an array Again, it is of 3 sizes. And this is also an array. Again, it is of 3 sizes.
Now, your compiler is saying that every element of this All these elements have an integer value which means that it will store an integer value. So I can store values like 3, 4, 7, 2, 5, 8 in this way. So first I came down one level, then I came down the second level and stored the integers inside them. Similarly, I will do the same here. All these will also have an array. This is called a three-dimensional array. So, one array whose every element is a single array. Now, again, this is the logical representation of how a computer thinks. If you want to make it conceptual, then assume this is a 3D world. So, what will you do? You will make a matrix. Something like this. This is a row. Row 1, row 2, row 3. This is column 1, column 2, column 3.
And you started stacking this multiple 2D matrix on top of each other. So, one layer became this. Similarly, you have made another layer. This is your second layer. Similarly, you have made a third layer. This is your third layer. Why three layers? Because see what we have. What is written here? 3, 3, 3.
Three rows. Three columns. And you assume that this is on top of this and this is on top of this. So it becomes a cube. So you can represent a 3D matrix conceptually. Although 3D matrix is very uncommon. No one uses it that much. But if you understand 3D matrix, then you will understand 4D, 5D and so on. You can declare any ND matrix. So we say in Java that you can declare a multidimensional matrix. You can make a multidimensional array. But generally, we mostly go to a two-dimensional array. Sometimes, to solve a problem, we need a three-dimensional array. But maybe we go beyond that. Because it becomes very complicated to understand things. But yes, you should know that you can do this.
Understanding all the arrays, like first of all let's talk about 1D array which was our 1D array So we saw only one way to display it, let's say we wrote roll numbers roll numbers and we wrote equal to new int and gave a size, let's say 3 You can write something like this instead of this int roll numbers And you want that you don't declare new int 3 first, that you make 3 size array and then add value to it one by one. Because look, let me show you in this first. What did you have to do to add value? You had to do like this to add value that roll numbers of 0 equal to let's say 4.
Numbers of 1 equal to let's say 5. You do something like this. So on the other hand, you can write like this too. How can you write? Something like this that int sorry int bracket role numbers equal to and give a curly bracket and don't put its values here 4,5,6 and close this curly bracket and put a semicolon you can also declare your array in this way So here we didn't write the size, like we wrote 3 size here, we didn't write the size here, we just wrote 3 values here. So your compiler will determine by itself that what to do is to make an array of 3 sizes whose values are 4, 5, and 6.
Remember, if you already know the size of the array, I mean, the size will obviously be known before, but if you already know the value, then you can declare it in this way too. And you can do the same thing in a two-dimensional array too. What you have to do in that, you can write int, you had marks, marks equal to now make a two dimensional array, how? you can write like this, marks 4, 5, 6 then curly bracket 7, 8, 9 Let's say 23, 34, 93, whatever random values I have added. So you can fill values in a two-dimensional array like this. Open a curly bracket and close it. Define each row one by one. So this is row 1, row 2 and row 3. Something like this.
And this is so that we can see our multidimensional array in a different class. Okay, so let's write our boilerplate code in it. And now let's start. So here we are going to start with our multidimensional arrays. Okay, very good. Now let's see multidimensional arrays. First of all, what did I say? I make a multidimensional array, marks.
And again, here comes the row. And again, in inner loop, I can write int col equal to 0. col less than 3 because size is 3. And col plus plus. Something like this. And I can print all of these. System dot out dot. First, let's print so that there is only one print in one line. Sorry, all of them get printed in one row. So, my array name is marks.
Print all the columns and give some space And after this loop, I will put one println so that it comes in the next line after every row This is what we talked about in our program So our output should be like this that it will come in the first line 23, 25, 45 Then these three will come in the second line and these three will come in the third line So let's run it once Look at this.
And what can I write here? Here I can write marks of length. The array is of the dot length of the roweth array. We talked about this, that the array of the marks is different conceptually, but how will it be stored physically and logically? So what we will do about it, the array of the first marks, means we will call this one marks of 0, this one marks of 1 and this one marks of 2. So, marks of 0.length, marks of 1.length and marks of 2.length. So, what I have written in short is marks of row.length. If we do this, then also our array will be easily printed.
We will comment it so that we can keep all things together for you. So I commented it for now. Now let's go down slowly. And let's start again from here. Now see there is nothing above. We are starting everything again because I want to make a multidimensional array but with each with different length. So first of all I made an array of marks.
To This is a two dimensional array so I have to declare it twice. Marks equal to new int Something like this. And let's say I tell 3 rows. That number of rows. That number of rows is 3. Number of rows is 3. But I haven't told columns yet. Now see how I will make columns. I will make marks of 0 array. Its length is 2. Or let's say 1.
Marks of 1 array. Its length is 2. and the length of marks of 2 is new int of 3. So basically, how did I tell you? If I visually imagine it, then I would have told it like this, that my array would look something like this, that here will be an element, sorry, here will be an element, and here, sorry, here, and here will be an element, similarly here, and here, And there will be an element here.
I will print only one element, 23. Then again, I will come out of this loop on the new line. Then again, the value of the row is 1. Again, I will come to the inner loop. I will say, this time, how far should I run this column? marks of row.length, i.e. marks of 1.length. And what is the length of marks of 1.length? It is 2. So again it will go till 2, so it will print 24 and 90, which was written here. Okay, then again it will go for 3rd. And this time marks of 3 dot lengths will go. How many 3 dot lengths are there?
So, I hope you understood that how we can print different size of the array, which is a multi-dimensional array, how we can play with it. You can apply the same concept if you want to make a 3-dimensional array or a 4-dimensional array. It's the same concept. And let me show you once that I can declare the array like this.
Something like this. So I can do this too. In fact, I can print it like this too. i equal to 0, i less than roleNumbers.length roleNumbers.length length i + + And every time what I do is system.out. println to our roll numbers of i something like this so our 101,102,103 will be printed in a new line see this is perfect and same concept I can do for a two dimensional array I can also do like this that the int of our marks array equal to new int sorry I will put it like this in my curly brackets and in every curly brackets let's say I will make a new row like 12, 14, 56. This is a list of 1 marks. Similarly, 24, 45, 67. This is a list of 2 marks. Here, comma will keep coming obviously. Comma and my 1st row is left. Let's say 3rd row. So here, 45, 67, 78. Something like this. I can define the marks in any way. This is my two-dimensional matrix. And I can print them in the same way. In the same way that I was doing till now. The way I was printing my two-dimensional matrix.
So now we are going to talk about Strings in Java Strings itself is a very big topic And you will understand Strings better when we will talk about Object Oriented Programming in Java Because Strings are also a non primitive data type In fact, before that let me tell you that what we were reading till now Remember we had discussed about data type And we had said in the beginning that we have primitive data types and non-primitive data types. Okay, you must remember. And in primitive, we talked about different data types. First, we talked about integers, in which your byte, short, int, long, all these come. Then we talked about floating point numbers, like your float and double come. We talked about boolean, we talked about char, and etc., Apart from this, we call all the data types as non-primitives. You will understand non-primitives when you have object-oriented programming knowledge. But you can assume that non-primitives include your arrays, strings, and many other things like objects. But you don't have to focus on all these because we haven't read all of them yet. But now I will tell you about strings because strings are a data type that.
First of all, if I write string, let's say str or give it a proper name. First of all, I have to write data type and my data type's name is string. Here comes the data type. And let's say I write its name name equal to. I write any name here and end it with a semicolon. As soon as I wrote string, it means that I am telling our compiler that whatever value I am going to put in this container, it is a string type. Now string can be any line. For example, I have written my name here, so let's say I have written the name Aditya. And again, you always put any string in a double quotation.
Let's say I call it fname instead of name And I can also make another string lname meaning last name And I can write another string in it Let's say tendon Something like this So, I can declare any string in this way. Now, we will not go into the operations of the string. Because we will understand all that better when we will study the strings in-depth after oops. Now, we just want to understand that we can declare the string. So, now see I have made two variables fname and lastname. In fact, whenever I write system.out.println Whenever I write something in it, it is always of string type.
That's why you see that I sometimes write it in double quotes. If I want to print a hello world, let's say I want to print a hello. So what do I do? I put it in a double quote. Because double quote means a string. So I am telling the compiler that I want to print a string whose name is hello.
Let's say I want to print Aditya Tandon together. So either I write Aditya Tandon here. But because I have made two variables for it. So what can I do? I can write system.out.println and I will write fname first. So my first print will be aditya. After that, I have to print tendon. Now, to print aditya and tendon together, I have to concatenate this first name and last name variable. Concatenate means to join them together. For that, I have operator plus. So, I will write fname plus lname. And here plus doesn't mean to add them. Whenever you add plus in strings, then plus means to concatenate the first name with the last name. That means to add them together. So first you write Aditya and then add the tendon together. That's all it means. If you understood this well, then you can use strings well. But yes, We won't tell you the internals of strings, we will learn that when we will learn the strings in depth. So let's quickly see this in code and end this video. So let's quickly see our strings. Let's say I declared a string first name. I gave it full available first name.
It comes in double quotes. So I can write aditya here. Similarly, I can declare a string. Remember, the last name of the string will be S. And I can add my surname, which is Tandon. Now, I can print this. So, I can write system.out.println And here, I can combine my first name and last name.
Neither there is a gap in front of aditya nor there is a gap before tendan. So it will print it like this. So if we see it, See, it printed without a gap. If I want a gap, what can I do? I can add a gap and a lastname. Because remember, this is also a valid string, lestne mainne lala htr equal to this.
We will read about this in depth later. But now see what I did. First name, space, last name. Now here first name, then space will be printed and then last name will be printed. And now see how our output will come. Proper Aditya Tandon came. Right. So this is what we had to read about strings. In fact, by concatenating this, we can add our own variable. Like I wrote a variable full name. And in that I wrote first name. plus space plus last name. I have also put this in a variable whose name is fullName. Now here I can print fullName directly. Something like this. So what I did is, I took a variable, like you add integers, if you add 2 and 3, then you write 2 plus 3. Here I am not adding integers, I am concatenating strings. So this is also a variable operation. Sorry, it is an expression, so I wrote firstName plus space plus lastName, put it in fullName. And fullName will also become a string. And print this string. So still my Aditya Tandon will be printed. So that's all you need to know about strings. After that, when we will study in depth, you will be able to understand a lot of things.
