Java Tutorial for Beginners - Made Easy - Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to the Java programming step-by-step video tutorial the very first question that is asked is why did Java become famous Java became famous because it is a platform independent programming language so the next question that is asked is what is platform independence now if you take a language like C or C++ now you write them on top of the Windows operating system and you compile them once compiled the C and C++ languages makes use of the Windows operating system resources to run so you cannot take this compiled c and c++ code and try to run it on the linux so this c and c++ are not platform independent because they depend on their operating systems so wherever whichever platform you compiled in you have to run them only on that particular platform or that operating system so how did they make java platform independent what they did is they brought in another layer between the java language and the operating system the layer is called the Java Virtual Machine or JVM for short now there are different virtual machines for different operating systems now this virtual machine is dependent on the operating system but Java is will run on top of the Java Virtual Machine so like this so that way there is a layer so you can see that they have brought in a layer between the language and the operating system that by making Java a platform independent language Java was originally created for creating programs for your mobile phones your refrigerator and your TV and your cars and all those things since all these various devices had different types of operating system they decided that they want a language which is platform independent and they hit upon this idea called the Java Virtual Mission so this is the reason Java is called a platform independent language now in this particular course we going to do these following chapters in Chapter one we are going to learn about the installation of the Java development kit the java virtual machine etc and the eclipse ID that is the integrated development environment this tool is used to write Java programs in Chapter two we will learn the basic syntax and statements that are available in Java including like you know datatypes and operators' then we will do like a few exercises and then a small review of all the topics we have done in Chapter one in Chapter three we are going to learn about the class and object we are going to learn like the various like how to write a method we are going to learn how to write a method the method signature and constructors in Chapter four we are going to be introduced to the object-oriented programming concepts especially what makes a language object-oriented programming language we are also going to learn about encapsulation and data hiding and polymorphism and overloading now with all the concepts we had learnt till chapter four we are going to do a small sample project using the java programming language then we will do a quick review of all the topics in Chapter six we are going to learn about inheritance that is like we are going to learn about abstract classes interfaces and like like the super keyword and the concept of overriding in Chapter seven we will be introduced to collections that is list ArrayList hash map and generics and then we will have like a quick review of all the topics we did till then in Chapter eight we will look at like various types of exceptions that is try catch finally and throw and throws chapter 9 we will look at the final keyword the static keyword and variable arguments and then we will finish off this entire course with a final review chapter 1 we will learn how to install Java and Eclipse IDE to install Java you need to install something called the Java development kit JDK it will install the compiler virtual machine and related files now you can write Java programs in notepad but it will be very difficult to program you need to remember syntax etc and if there are any issues you need to know like how to go catch those issues so the industry uses a free tool like Eclipse and NetBeans etc we will use Eclipse and we will take a look at it very soon so we need to install the Java development kit and Eclipse IDE so to install Java go to google and type in Java JDK download so it will take you to like a site called Java SE downloads or something click on it now here you can see that you can go to the latest version that is Java standard edition 7 you can see that this is the JDK and this is the Java Runtime environment if you install JDK it will install JRE 2 so you don't have to worry about it so just click on download here for the JDK once you click on download it will tell you like it will ask you what kind of operating system do you have you accept the License Agreement and then you decide whether you have a 64-bit or a 32-bit if you have a 32-bit operating system then you select this if you have a 62 if you have a 64-bit select this to find out what kind of operating system you have you can right click on my computer or if you have Windows 7 you can right click here on this computer click on properties and here you can see that the system type is 64-bit if you have XP or Vista just right-click on my computer and click on properties you would be able to see what how many bits is your operating system now I have like a 64-bit operating system windows 64-bit so here I'm going to click on this and then save this particular file now after saving your file double click on the file and open it this is likely to take some time the wizard is pretty simple and straightforward you can just click Next go through everything it will install it in Program Files java jdk 1.7 if you want to change it you can change it but I suggest you leave it as it is click Next so here you can see that the development kit has been successfully installed now once you have installed the Java development kit you can install the Eclipse ID that is the integrated development environment which will make writing your Java programs easy so in google you can search for Eclipse IDE and then it will take you to this particular website you can go to downloads and over here you can download either like Eclipse IDE for Java developers or you can download Eclipse IDE for Java Enterprise Edition developers the difference between these two are like basically if you are going to learn about hibernate if you are going to learn about JSP servlets and spring and all those things then it is better you go for this Eclipse IDE for Java Enterprise Edition developers but if you want to pretty much stick with Java basic Java then Eclipse IDE for Java developers is more than enough or you can download the classic Eclipse also so you can either download the 32-bit or the 64-bit so once you download the 64-bit or 32-bit Eclipse Java Enterprise Edition all you have to do is right click extract files you can install WinRAR to extract files and then just click on OK or you can extract it any that you can extract it anywhere it's just a direct extraction so in my case I have extracted to this Eclipse folder in C Drive all you have to do is come here create a shortcut for this Eclipse create a shortcut and paste it to your desktop right click create shortcut once you have the shortcut here you can pull this to your desktop you can pull this to your desktop or you can cut and paste it to your desktop or if you want you can leave it here itself and then you can double click on this Eclipse to start eclipse you in Chapter two we are going to learn the Java syntax by writing some small Java programs go to your desktop or wherever you installed Eclipse and double click on eclipse now you can click on the Browse button and select any folder this will be your workspace I created a folder in the C Drive called a learn Java I am selecting that particular folder and I will click OK so this C colon learned Java folder will be my workspace that is all the sample programs will be stored here and I'm going to click OK you can close on the welcome screen the very first thing that you have to do is we have to create a project so let us click on file new and then here select project now we want to create a Java project so select Java project and click Next here we can give any project name I am just going to name this project as first project now you can keep everything as it is like use default location and the environment is Java 1.7 click Next and then you can click finish it will ask you something like do you want to open the Java perspective say yes so now we have a project and if you click here right next to the project you will see that it has a source folder the SRC means is a source folder where you can store all your Java programs now before writing any java program let us create something called as a package a package is nothing but folders in Java we can right click on the source click new package and create a package like de 1 dot examples and click finish now these packages are nothing but folders in Java if you want to see the folder structure you can go to the C Drive that learn Java workspace you can go to the C Drive learn Java workspace and you can see that project name first project and under which we see that source folder and under which you will see this day one folder and under which you will see the example folder so you can see that the packages that we created they're basically created folders for us in the C Drive so once you have created a package let us create a small Java program so let us right click on a package click on new class now don't worry about water classes we will learn that very soon but for now remember if you want to write any java program you need to create something called as a class and let us call this class my first class now remember while creating the class name you need to use a capital letter the class name starts with a capital letter and then the subsequent words will also have to be capital letters for example my M is capital and then first class F is capital and C is Captain there should be no space between these particular words it should be one word also click on this public static void main of course you will learn very soon about the use of all these things but for now just remember to create a class and then select public static void main click finish now I want to increase the font size in Eclipse so that everyone can see clearly to increase the font size click on windows preferences type in font select colors and font basic text font click Edit and here you can select a size and click OK now before writing any program we have to learn how to write comments comments are useful in describing a particular program for example I want to say that I am planning to learn in Java so I want to write a comment so let me delete these things now to write a multi-line comment all you have to do is put this slash sign asterik press Enter this will be a multi-line comment like this now I can write some kind of a description above this particular program so here you can see that I have written some kind of a comment these know this is an example of a multi-line comment for a single line comment you need to have two slashes so here you can see that I have like two slashes and my first statement it is a comment you can write a comment anywhere in a class it is not going to affect your program now if you want to print something to the console the syntax is system dot now remember the system keyword should start with the capital S and when you put this particular dot you should get some kind of a pop-up this pop-up shows that this particular word you have typed it correctly system dot out dot println then you open the parentheses and double quotes and within the double quotes you can type anything you want like say hello world now this is the syntax to print something to the console in Java system dot out dot print line and the system starts with a capital S and within double quotes you can type in anything you want now you can save this particular program by clicking on the Save button here and to run this program that is compile and run this program you can right click here anywhere in the screen click on run as and then click on java application now you can see that the helloworld has been printed here to the console now suppose you want to add two numbers so to add two numbers we first need to declare two numbers that is something like this so here we have declared two numbers like say integer X equal to 10 and integer y equal to 20 this x and y are called variables variables can hold some data now this integer is called the data type that is this integer X can hold any whole number so your next question may be like what if I want to hold a decimal number what if I want to hold like a character or a string or something like that like a text we will see all different types of data types later on but for now remember integer data types can hold any whole number and then we can say something like integer C equal to X plus y let's say integer result equal to X plus y and then we can print result now to print result once again at a system dot out dot print line now there is a shortcut to write system dot out dot print line you can say Type S why gets oh and keep the control key pressed and press the spacebar like this and then you can say result now you can save this program and run it right click run as java application and you will see that the hello world is printed and the result of 30 is printed you can also say result equal to within double quotes and then plus result so what this will do is it will print out save it and run it so here you can see that it has printed out result equal to and then it has concatenated this plus sign does not add you cannot add some text and some number so it has it's called a concatenated it concatenates this text and this particular number and then you see the print out result equal to 30 you saw how to add two numbers and these are the signs for subtraction multiplication and division this is the modulus sign the modulus sign is used to find the remainder for example if you have like integer X equal to 10 and integer y equal to 3 into silt equal to X mod y what happens is 10 mod 3 the remainder is 1 that is 3 goes into 10 three times three threes are nine the remainder is 1 so the result will be 1 you can right click run as java application and you can see that the result is equal to 1 next let us take a look at the if statement we can always expand and collapse this package Explorer by clicking here so to learn the if statement let us do another example right click on the package new class and then over here let us type example if select public static void main click finish we don't need these statements let us delete them so imagine I have two numbers two variables integer X equal to 20 and integer y equal to 20 and if statement goes something like this you read this as if X is less than Y this symbol is for less than to see if the value of X is less than Y then I want to print out sys Oh control space print X is less than Y let us write another if statement if X is equal to y the way you say equal is you have to say equal to equal to if X is equal to equal to Y that means if the values of x and y are the same X is equal to Y let's check if X is greater than Y now let us save this you can either click here or you can press on control key and keep the control key pressed and then click on the S key control S will save it or you can click on this particular Save button now let us run this right click run as java application you will see that X is equal to Y has been printed the reason is X is 20 and Y is 20 it will come and check if X is less than Y well X is not less than Y because 20 and 20 are equal to each other then it will come and check if X is equal to equal to Y if X is equal to equal to Y yes both of them are equal then it will print X is equal to Y it will also go and check if is greater than Y well X is not greater than Y so it will not print this particular statement so you just learn the if statement but of course if you notice this is an inefficient way of writing a program the reason is it is checking if X is less than Y then it is checking if it is equal to Y then it is checking if it is greater than Y so it will run all these three statements you can use an else statement to make it more efficient like this you can say if X is less than Y system Road dot print line X is less than Y else if X is equal to equal to Y session dot dot print line X is equal to Y yells if X is greater than Y then system dot print line X is greater than Y now the advantage of this is of writing a statement like this it is like it will come and check if X is less than Y if it is less than Y well it will print it out but in this case it is not less than Y it will come here else if X is equal to equal to Y so since it has matched the condition it will print this and it will come out of it it will not go check yell statement because only if this fails it will go to the next el statement so you are saving that fraction of a second for the compiler so let us save and run this so you can see that you get the same answer but then the compiler did not check this particular condition and for the last condition you really don't have to use this if statement you can simply say like else system Road dot print line X must be greater than Y the reason is so here you can see if X is less than Y else if X is equal to equal to Y print this else well I am sure if it is not less than Y and if it is not equal to Y it should be greater than Y save and run this you will get the same answer as a new Java programmer one of the most confusing things is the small issues like when to put the semicolon then not to put the semicolon when to use this curly braces and when not to use the curly braces etc remember these things you will know by experience but for a general rule any complete statement will Bend with the semicolon for example you see that int X equal to xx is a complete statement whereas the if statement is not a complete statement I am checking there is going to be some kind of a code if X is less than Y I want to do something else so I will not end this with a semicolon this is like some kind of a main method so I cannot end this with a semicolon same way whenever you open a curly brace you must make sure that you close the particular curly brace these are called curly braces you can always check if a curly brace is closed by clicking on a curly brace and you will see a small square box here on top of the one which is opened same way if I click on this I will see a square box here so that you can make sure that every open curly brace is closed same thing goes for if I click on a curly brace over here the closing curly brace is shown and these are called parentheses you will get more used to these syntaxes as and when we go on this system dot out dot print line is a statement on its own this less than symbol here is called a relational operator these are relational operators you may be wondering what are the other relational operators well there are a few other relational operators to equal to Sciences equal to an exclamation mark and equal to sign is not equal to this is the greater than less than sign this is greater than or equal to and less than or equal to these are used to check if X is greater than or equal to Y now learn how to do a four loop let us create a class called example for the for statement syntax go something like this for int I equal to 0 semicolon I less than or equal to ten semicolon I plus plus or these in parentheses then open curly braces press enter it will automatically do the closing curly braces and here let us print out I a for statement you can see that has three parts the initial value that is int I equal to 0 the condition that is loop through this as long as I is less than 10 and then the incremented or decremented of Y so the very first time the value of I has been declared as 0 so it will come here and will check this I less than 10 or less than or equal to 10 yes 0 is less than or equal to 10 so it will come here and print whatever it will execute whatever is there within these curly braces so it will print out system route 99 I equal to 0 then once again it will go into this particular false statement now when it goes here for the next time you can see that it it reads this I plus plus that means the value of I has been incremented by 1 I plus plus is nothing but I equal to I plus 1 writing I plus plus is a short form of writing something like this I equal to I plus 1 if you want you can say I equal to I plus 1 right here so it will start printing out like 0 1 2 3 it will keep incrementing I till I so value reaches 10 when it reaches 10 what will happen is yes eyes value is 10 so 10 is less than or equal to 10 years it is equal to 10 for the print 10 but next time it comes is value will be 11 but 11 is not less than or equal to 10 so what will happen is it will come out of the loop so in this case it will print from 0 to 10 so let us save this and run this right click run as java application you can see that i can increment i'll expand the screen you can see that is value has been preached has been printed from 0 to 10 I equal to 0 to 10 if you want to print from 0 to 9 all you have to do this remove the equal to sign and then run this you can see that I is value has been printed from 0 to 9 you may be wondering like is there any other symbol like this plus plus sign is there anything else I should know you can either use a plus plus sign which is like we increment the value or you can use a minus minus sign to decrement the value let us try using the minus minus sign let us put the initial value of is 10 and we will check we will print till I is greater than 0 and then you will say I minus minus let us save this and run this so here you can see that it started out printing I equal to 10 and then it kept reducing the value of I by 1 till I became 1 since 1 is greater than 0 it printed it but 1 minus minus is 0 once so once I reached 0 it came out of this loop next let us learn up with the while loop let us create a class so we have a class called example why let us have some integer X equal to minus ten the while syntax goes something like this while X is less than or equal to zero curly brace curly braces print X so here you have integer X equal to minus ten while X is less than or equal to 0 where minus ten is less than or equal to zero print X and then increment the value of x we can increment the value of x by saying X plus plus which is nothing but X equal to X plus 1 now let us run this save so if you look at the result X started at minus ten and then like this minus ten less than or equal to zero yes it is less than or equal to zero so print out X equal to minus ten then it will come and increment the value of x by 1 so minus ten plus 1 is equal to minus 9 so is minus 9 it will go back here is minus 9 less than 0 yes it is it will print out X equal to minus 9 so it keeps printing out till X reaches the value of 0 is 0 less than or equal to 0 yes it is equal to 0 so it will print out 0 but 0 plus 1 is equal to 1 1 is not less than 0 so it will come out of the loop so this is what we call a while loop now there are different ways to increment the value of x you can either say X plus plus as you have written here or let me come in this and type X equal to X plus 1 this is another way of incrementing the value of x this will also work the same let us save and run this you can see that there is no difference and there is another day of writing this too when you have something like X equal to X plus 1 and you can also write it like this X plus equal to one so this means X equal to X plus one let us run this so you can see it's the same answer so different styles of writing it so just remember plus equal to means X equal to X plus let us change this number to two this means X equal to X plus two so here you are incrementing the value of x by 2 so you get printouts like minus 10 minus 8 and minus 6 you may be wondering is there any other symbol like this so here you can see that you can either use plus equal to which says X equal to X plus whatever number it is called an addition assignment you can also do minus equal to or multiplication equal to our division equal to n mod equal to next let us learn about the do-while statement let us create a class called example do-while let us declare some integer integer X equal to 10 the do-while syntax goes something like this do curly brace curly brace and then your while statement comes here wire let's say X greater than 0 and then a semicolon here and inside these curly braces I want to print out X and I will reduce the value of X so let us run this and see how this works so here you can notice when I run this it starts printing out X equal to 10 that is X equal to 10 do and then it will come in here and it will print out X equal to 10 X minus minus will reduce the value of 10 by 1 that is 9 it will check is 9 greater than 0 yes 9 is greater than 0 so it will come back to the loop and it will print out X equal to 9 and then it will come here reduce the value of 9 by 1 it will come and check if 8 is greater than 0 yes it will go back here and print out 8 so on and so forth so it will print till X equal to 1 and then X minus minus of 1 0 is 0 greater than 0 no it will come out of the loop so this do-while is very similar to the while loop do not forget to put the semicolon right here so what is the difference between a while loop and do-while loop well for a while loop you can see that it first checks for the condition it checks if X is less than or equal to 0 and then it goes and prints it whereas for do-while it will first print the statement it will do all the work and then it will check for the condition so the main difference is the do-while statement will run at least once for example if X is equal to minus 10 here even though minus 10 is not greater than 0 this day Eggman will still run it will still print out X and two minus X by one so so when I run this you can see that I have X equal to minus 10 printed out so you love these while condition is false whereas for a while statement if I make this 10 it will not print this even once you can see that well 10 is not less than 0 so it will not print here so do-while statement executes the block of code at least once next let us learn about and and/or statements let us create a class called example and and our imagine I have two numbers integer X integer Y I can declare two variables like this and then I can assign numbers to X equal to 10 now imagine I want to check if both these numbers are positive numbers that is x and y are foster numbers the way to do it is if X greater than 0 and y greater than 0 curly brace and close curly brace so the way you read it is X greater than 0 if X greater than 0 and these two ampersand sign is referred to as and if X is greater than 0 and Y is greater than 0 then print both numbers are positive yells if check if at least one of them is positive number that is if X is greater than 0 or Y is greater than 0 the way to write it is the or symbol is the two pipe symbol you can find this pipe symbol right above your Enter key you have to press the shift and the key above your Enter key so you read this as else if X is greater than 0 or Y is greater than 0 at least one number is positive yes where I don't have to check if both of them are not greater than 0 and at least one of them is not greater than 0 that means both must be less than 0 that is both numbers are negative so let's save it ctrl s and run it right click run as java application so here you can see that at least one num is positive is printed because x is 10 and is a positive number so this is the and and or statement now this will work even if you have like a single ampersand and a single pipe symbol this will work the same so what is the difference in this single am president and double am percent is this double ampersand is called a short and now what is a shorthand this imagine I have X as minus 10 and Y is positive 10 now it will come here and will check if X is greater than 0 where is X greater than 0 no it is not greater than 0 so what happens is this if this condition fails a shorthand will not even bother to look at the other condition because both the condition must be true for an to work and this this X must be greater than 0 and y must be greater than 0 but if you have like 2 ampersand sign it will know that it is useless to go look here at this particular condition here the reason is well X fails so it does not matter if Y is positive or not if you have like one ampersand symbol what happens is it will check if X greater than 0 well no it is not greater than 0 but even then it can go and check if Y is greater than 0 you may be wondering then why use the single ampersand at all well the single ampersand is used for bit operations which I think you can read up on your own at a later stage next let us take which case statement so let us create in your class so imagine I have a variable like J equal to two the switch case statement is written like this switch within parenthesis J curly braces closed curly braces then you create various cases like this k 0 : system dot print line value is 0 and then say break create another case so here you can see that I have created various cases like I have a int J equal to 2 and then I have a switch J and then I have like a 0 which will print out value is 0 and then there is a break case 1 value is 1 and so on and then at the end I have something called as default and it's prints out no value and then break and then I close the particular curly braces over here this curly braces I close it here now let us save and run this so when you run this you get the value as 2 value is 2 is printed so you can see how the switch case works when the compiler reaches this particular point like switch J then it will look for a case which matches the value 2 so since this particular case matches the value 2 it will execute all the code in this particular block of code before the break is hit and then it reach this break and it will come out of this particular switch statement if you do not have this break statement then what will happen is it will fall down to case 3 and in print out values 3 also so it will print out values 2 and value is 3 I can save and run this right they ran out so here you can see that if I run this now values 2 is printed and then values 3 is printed because I had removed the break statement now suppose there is some value like say 5 and we don't have any of the matching cases in this case I have no case 5 so in those cases the default case would be executed that is no value would be printed since Java 7 you can also use drinks in switch case statement before Java 7 you cannot use string string this used to hold any kind of a text so let us modify this to you strings string J holds the texts too so now let us say K is 0 1 2 and you can see that I have also changed all the cases to like strings now if I run this you can see that values 2 is printed so you can also use string in these switch case statements next let us take a look at arrays so here I have a new class called example arrays now what are arrays imagine I want to store a bunch of data I can store data like this so here I am storing some data that is integer a equal to 10 integer B equal to 20 I am also declaring that variables like C D and E and I am storing cs30 D as 40s 50 like I can have them in one line I can have them in multiple lines it doesn't matter so I can store a bunch of numbers like this but this doesn't look like an efficient way of storing numbers I want to store five numbers this doesn't look like an efficient way and I am likely to run out of variable names too so another way to do it maybe I can do the same thing like this so let me comment out this block of code to come and throw this block of code you can just select it click on source and then say add block comment so this whole thing will be like blocked so I here I have like in a 1 equal to 10 a 2 equal to 20 like this I can declare all the numbers but this still does not look efficient so what is an efficient way of storing large amounts of data uniform data so to do that let me block this code an efficient way of storing large amounts of uniform data is called arrays so if you want to store a large amount of data the best way to store the data is so here I have declared an array of a so how you declare an integer array is you say integer square brackets a equal to all the numbers that you want to store in an array once you declare something like this what happens is internally Java will create a boxes like this since we name the array a it will label the boxes like this so here you can see that we create an a array and it has file numbers so Java internally will create 5 boxes 1 2 3 4 5 and it will label the boxes as a of 0 a of 1 a of 2 a of 3 a of for this is read as a of 0 a of 1 and it will put those fine numbers in these boxes so this is an array an efficient way of storing a bunch of homogeneous data so if you want to access some data in this array all you have to do is try to print out like say a of 2 and a of 4 it will print out 30 and 50 so let us print out something from the array so here you can see that I am trying to print out a of 2 and then there is a space and then I am trying to print out a off for room where arrays always start at 0 so a of 2 will print out this is a of 0 a of 1 a of 230 and then a or 4 is 50 so let us save this and run this so you can see that 30 and 50 are being printed out now you may ask like what if I want to loop through all the numbers and print out all the numbers in the array to loop through all the numbers you can use something called as a for each statement or an enhanced for statement the way to write a for each statement or enhanced for statement is for now the data type of the array whatever the data type of the array in this case the data type of that is integer integer some temporary variable colon and then the name of the array a curly braces curly braces and inside this you can print out the temperature so what happens in this case is let us save and run this you can see that all the values in the array that is 10 20 30 40 50 I've been printed so how this enhanced for workers the very first time it comes and sees this for int temp cotton a it takes the first value of a the first value of the array a and put certain temp and then it is printing out temp the second time it loops through it takes a second value of a and puts it in this temp and then it prints out temp so this is an for statement of course it is not required that whenever you declare an array you should immediately give all the numbers in that particular array you can first declare an array and then you can assign numbers to it like this so in this case I have declared an array called X and I am saying like new interface that means this X array will have five numbers so immediately Java behind-the-scene will create five boxes and label them as X of zero X of 1 to X of 4 remember arrays always start at 0 now since we have just declared the array like here and since that array X does not have any values by default it will put the value 0 so here you can see that 0 is the value available of course you can always assign data later like this so here you can see that I am assigning X of 3 is 25 and X of 0 is 12 so X of 0 will be 12 and X of 3 would be 25 let us loop through this particular array I can have some temporary variable so here I have some temporary variable temp 2 I am going to loop through this X array I am going to print it out let's save and run this to differentiate between this print out and this print out I am just going to have this dash dash dash printed in between save and run so here you can see that it is printing out this other array with 1200 25 and 0 1200 25 and 0 the zeros are default if you don't give any values for an integer array of course you can create any type of arrays you can also create strings arrays if you want so here I have a string array called St which has 1 2 and 3 and I can loop through string arrays so you can see that it is printing out 1 2 & 3 same way I can create I'm creating an empty string array and inside which so here I am creating an empty string array inside which I am placing the text 25 and text 12 you can see that these are not numbers I put them within double quotes so let's save and run this so same way Here I am declaring a string array of 5 and I am putting the text 25 and text 12 in the string array of 3 and string array of 4 and I am looping through this particular string array now the one big difference between us declaring a string array and declaring an integer array is I told you if you declare an integer array by default all the values would be 0 and then you can assign numbers but for string array the default is null values the default is null values so here so here instead of zeros you are going to have null values and null here and when you save and run this you can see that 12 null another 25 null are being printed next let us take a look at a two-dimensional array so here I have a class called example to dim array now arrays need not be one dimension like this that is you have like one array of numbers it can be two-dimensional what does it mean by two-dimensional two-dimensional arrays are a kind of a matrix so here you have like columns column 0 column 1 column 2 and then rose that is column row 0 Row 1 Row 2 and Row 3 two-dimensional arrays you can label them that is here you have like a 2d Mario of 0 comma 0 that is the zero row and 0 column this box is 0 row first column and 0 row second column here it is Row 1 and 0 column Row 1 and 1 column and in these boxes you can assign numbers and this would become a two-dimensional array so how do you go about declaring a two-dimensional array and assign values you can declare a two-dimensional array like this in two square brackets like an array name too dim and then you int of how many rows do you want and how many columns you want here you can see that I want four rows and three columns so what I did is I named the array has two dime and I said four rows and three columns for rows three columns now to assign data to this array I can assign it like this so here you can see that I am assigning the second row first column as the number 10 so the second row first column will be assign the value 10 so like this I can assign data to any of these boxes I can also use two for loops to assign data automatically like this so here you can see that I have a loop within a loop I have a for int I equal to 0 I less than 4 and then I plus plus and inside this I have like 4 in J equal to 0 J less than 3 J plus plus and now I am going to say so here I am saying like to dimension off I comma J is equal to temp and the temp value initial value is 10 and then I am incrementing the temporary that is temp plus equal to 10 that is 10 P equal to 10 plus 10 so how this works is the very first time I will be 0 and then J will be 0 so 2 dimensional of 0 comma 0 will be 10 then the temp value will increase increment by 10 that is temple be 20 it will go here and first it will go into the internal group that is I will still be 0 but J will increment by 1 so it will be 1 so 0 comma 1 will be 20 so you will have data like this so 0 comma 0 will be 10 0 comma 1 will be 20 this is the I and this is the J value and here it will be like ie value will be 0 next loop J value will be 2 and then the template I've incremented to 30 so once Jay reaches 2 and then it increments the 3 it will come out of this loop it will go back to this for loop and I value will now be 1 so now it will once again come back it will come back to this J and J will start at 0 because no I is 1 and there will be a fresh J loop so here it will be like 2 dimensional of 1 comma 0 and that would be like the temp value so it will take the latest tempered you so once that entire loop is done it will fill up these boxes with these numbers suppose you want to see the print out you can always redo this and here instead of assigning data let us print it out so I am going to print out the two-dimensional of I comma J the tudm of I and J what we declared here so if you run this you can see that all the numbers are printed from 10 to 120 ten two hundred and twenty of course you may feel like I don't want everything to be printed in one line can be printed like a matrix yes you can remember the print line will always print in a new light but you can use the print statement the print statement will print in the same line so what I'm going to do is I'm going to say system dot out dot print print the number but then the print statement will keep printing without creating a new line so every time J loop ends I want to print a new line so I will print an empty line like the system route dot print line and then I just leave it so now let's run this so here you can see that it has printed like a matrix just like what we have drawn ten 20 30 40 50 60 and so on so this did not print in a new line it printed the two-dimensional of 0 comma 0 then the space that is 10 and then the next time 0 and 1 and then space but once the J got over before going back to I it printed a new line so it came here and then it printed started printing 40 50 and 60 next let us take a look at string manipulation string is used to hold any text so for example I have a string X equal to James team if I want to concatenate string I can do it like this so this statement will print hello space and then it will concatenate it with James Dean to print out hello James Dean if I want to convert the string into uppercase I can say X dot to uppercase now like this there are so many different methods you can see here when I say X dot you have all these methods that you can do with that is trim uppercase like you know substring and all those things we will take a look at few of these string methods X dot to uppercase will convert that particular string into uppercase so X dot substring of 2 will print everything from the second character now what is the second character remember when you have string X equal to James Dean will internally create an array of characters like this so you have like James Dean each in their own boxes and to label it as X of 0 to X of 9 so when you see X of substring of 2 it will take from the second character that is X of 2 till the rest of the word so mes space Dean will be printed when you say X of substring of 2 comma 7 then what happens is it will print from the second character up to but not including 7 so it will print mes space and then D up to but not including 7 so here you can see that mes space D has been printed X dot caret 3 will print out the character at the third position that is e will be printed now sometimes what happens is you make it a number as a string for example you have a string H equal to 30 which is basically a string and then string salary equal to seventy eight thousand nine sixty five point eight three now if you have a number as a string you cannot manipulate it that is if I want to divide each by two or if I want to like in a multiply H by two I cannot do that so how do I convert a string into a number where we have a concept called wrapper classes if you want to convert some kind of a string into an integer this particular agent to integer you can do something like this you can say integer dot parse int and then you can pass this particular string so what this will do is this particular class called integer will take the string of age 37 and it will convert it into an integer and class in it to a now this you can divide by two if you want to so here you can see that I am taking this particular salary which has a decimal number and I am converting it into a double number double dot parse double and then I'm passing the salary and then whatever the double value I get I can multiply with 0.15 I cannot do like divide by 2 here it will give me an error it will say that you cannot divide string by 2 but I can convert that particular string age into an integer and then divided by 2 and they can assign it to a or I can like say add 2 for this so now if I run this you can see that I got 39 as the answer because I have added 2 and I am getting 15% of the particular Saturday that is 11,000 so this is called wrapper classes that is a wrapper class for every primitive datatype of course the next question will be like okay we saw integer and you just introduce double to us to hold decimal places and you also showed us a string which holds text what are the other data types that Java has Java has eight primitive data types byte short int law float double character boolean now out of these you will be using mainly integer for holding any kind of a whole number an integer can hold an whole number of this range if you want to hold any decimal values you can use a double and for holding true or false you can use a boolean now string is not a primitive datatype but then use string to hold text values that is string x equal to hello world or any text values you will not be using byte short and long you can use long if you have like a really long whole number and for all decimal places you can use double instead of a 32-bit float so once again you will mostly use integer to hold a whole number a number without any decimals or you can use long and for using decimal use double for text your string for true or false value use boolean do a quick review of all the concepts we have learned till now our very first example we learned how to do a print statement to print something to the console you use system dot out dot print line and then you can put anything within double quotes internal printer to the console same way we learned how to declare numbers like integer X equal to 10 and then we learnt how to add numbers or like multiply or the mod mod is basically how many times does 3 go into 10 3 goes into 10 three times so the remainder is 1 and the X mod Y it is print out the result as 1 next we learnt about the if statement we had two numbers X and Y if X is less than Y print out like X is less than Y else if X is equal to equal to Y print out X is equal to Y else system dot out dot print line X is greater than Y then we learnt the for statement a for statement has three parts first is the initial value then the condition the condition must always be true for this for to be executed and then the incremented or decremented so in this case I am going to print the initial value of I equal to 10 so it will it will check is 10 greater than 0 yes it is greater than 0 then it will go and print out I equal to 10 then the next time it comes over it will reduce the value of 10 by 1 I minus minus will decrease the value of 10 by 1 so this one will print from 10 up to 1 but not 0 because it will stop when it comes to 0 then we saw the concept of while loop a while loop will first check for the condition and then execute the block of code so in this case X equal to 10 as long as 10 is less than 0 it will it will execute this in this case it is not 10 is not less than 0 so it will not execute so you can see here this would not be executed but if you say as long as 10 is greater than or equal to 0 it will execute and then here you are incrementing the value of X or you can decrement the value of x if you want so you can see that it has printed minus ten it is zero less than zero and then you're incrementing by two then we saw the concept of do-while do-while will first execute the block of code and then it will check for condition so in this case so here in this case I have X equal to ten it will execute that X equal to 10 and then it will reduce the value and it will keep looping till the value of X is greater than zero so the advantage of do wireless even if the condition is false it will execute this block of code at least once so that is a difference between while and do y next we saw the concept of switch case we can use like integer double or string from Java seven we can use string for the switch case switch case is like you have some kind of a data next string J equal to 2 and then switch J it will look for a case that matches this particular string so case 2 it matches this particular word too so this particular block of code value is to be printed and it will break out of this particular switch case if you don't have the break it will fall down to the next case and will start printing here if none of the cases match then the default case will be printed then we saw the concept of and and or statement that is if you have like two conditions and both the conditions have to be true that means you can use the end that is if X is greater than 0 and Y is greater than 0 then print both numbers are positive if either one have to be true then use the or statement that is X is greater than 0 or Y is greater than 0 then print out at least one number is positive we also saw the concept of arrays so basically if you have like some kind of a data same data and you want to store them you can use an array so this is an integer array of a so which will be used to store an array of integers it will create boxes and it will put them as a of 0 to a of 4 and you can access those data by saying a of 2 and a for if you want to look through an array you can use an enhanced 4 or a for each loop you can say like the data type integer put some temporary variable callin the name of the array a and then you can print out temp you can also declare arrays and then assign values to those particular array boxes if you do not assign some value by default it will take 0 and samely you can look through this particular array to you can also create strings arrays if you want here you are creating a string array and of course by looping through a string array the data type of this array a string you put a temporary variable call in the name of the array and then it will print out temp and same thing happens for you can declare a string array and you can look through those string array of course if you don't assign any value to a particular box by default for string array it will be null value you can also create two-dimensional arrays two-dimensional array is a matrix that is it will have 4 rows and 3 columns so you have like two dimensional array and you can assign data to time two dimensional arrays like this you can say like a row and column and you can assign some data you can use a loop within a loop to assign data or to print a two-dimensional array then we also saw the concept of strings that is how to manipulate strings how to concatenate strings use some of the string methods get the substring and find what are the characters and we saw the concept of wrapper classes that that is if you have some data some number that is as a string that comes to you as a string then you can always convert that into a number so that you can manipulate that number now let us try a few practice exercises let me create another package right click on source new package and then the create a package called de 1 dot X err sizes and here I can create a new class called exercise a right click on the package new class exercise a public static void main click finish you can view these packages two ways click on this click on package presentation you can either view them in a flat manner that is it'll show like day one dot examples day one dot exercises etc or you can view them in a hierarchical manner click on package presentation and say hierarchical so what happens is it will say like day one and within which it will show you all the packages and the methods or classes inside those particular packages so for this first exercise let us take a look at this data imagine I have this data that is salary and tax rate I want to write a program to calculate the tax now up to fifteen thousand dollars the tax rate is ten percent from fifteen thousand to forty thousand two tax rate is twenty percent and above forty thousand the tax rate is thirty percent so write a program to calculate the tax imagine we have a salary a double salary equal to seventy eight thousand six seventy eight point six five we want to see if this particular salary falls within those particular range so let us have another variable called tax and let us use a series of each statement to calculate the tax so here you can see how we have solved the problem we declared some kind of a variable called tax equal to zero point zero and then we are checking if the salary is less than fifteen thousand or less than or equal to fifteen thousand then calculate the tax as salary times point one zero 10% if the salary is less than or equal to 40,000 then calculate the salary at 20% else calculate the salary it must be if it is not less than 15,000 and if it is not less than 40,000 it should be more than 40,000 so so are above 40,000 calculate the salary at 30% and print out the tax so let us save and run this so here in this case it will calculate the salary at 30% of 78,000 suppose I have like 10,000 at the salary into the calculator - 10 % you can see the 10% off thousand 10,000 is thousand if I say like 20,000 salary it will calculate it at 20% that is 4000 which is 20% of 20,000 so we used a simple earful statement to calculate the tax for our next exercise let us try to solve this problem imagine I have a string array called numbers which basically has numbers but they are present as strings that is you have like 10 but it is within course basically it is a string imagine I want to find the sum of all these numbers then how do I go about solving it let me first declare a variable in total equal to 0 and let me try to loop through these numbers loop through the string data so we can use an enhanced for loop no I cannot say something like this this will not be acceptable I cannot say total equal to total plus temp because temp is a string so how do I convert this particular 10 into an integer I can use a wrapper class I can say something like this I can say total equal to total plus integer dot parseint of them that means I take the very first 10 from this array which is in temp converted to an integer and then I add it to the total so the next time it comes over the 20 will be taken into temp and then you convert the 20 and the 20 plus 10 total B 30 so on we can add all the numbers of course a shorter way of writing this is you can simply say total plus equal to integer dot parseint and once we are out of the loop we can print out total equal to total of course don't try to print it within this particular loop because every time it loops through it will print it again and again let us save and run this so you can see that the total is 100 has been printed for our last exercise imagine I have a string a equal to hello world I want to print out the word world in lower case now how do I go about doing that I can use a print statement and I can say a dot substring off now if I want to print out word I can say like substring of six the reason I have to use six is H is 0 1 2 3 4 the space is 5 so everything from 6 I want to print out everything from 6 so I will say aid or substring of 6 so when I save this and run it you'll notice that the word will be printed but you can see that the W is in capital case I want everything in lowercase so if I want to convert this whole thing into lowercase all I have to do this put a dot here and then say to lowercase now you can save it ctrl s and then run it now you got the world printed all in lowercase so with this we end our day 1 exercises you next let us take a look at the concept of class and object now if you have done any kind of programming like Fortran or COBOL and you are given a problem like calculate the total pay of an employee by adding the salary and bonus how would you go about doing it so we will have something like this so given a situation where we have the employee salary and bonus we will like add up the salary and bonus and then print the total pay or if you have done some COBOL or Fortran we may call a method or a subroutine here so here if you notice we are taking the input of salary and bonus and then we are calling some kind of a function or subroutine to calculate total pay and then this function will calculate that what will be and then we will print the total pay so this used to be the old way of writing programs a kind of a top-down approach the program starts the beginning here and then it ends here and then you run this program again and again for various employees like for an employee like Alex we will take a salary and bonus we will call this function which will calculate the total pay and then we will print the total pay for Alex for Linda will run this program again we will get the salary bonus and then we will call a function to calculate the sum of salary and bonus and will print the total pay but in object-oriented programming the same program of calculating total pay is written in a different manner in object-oriented programming that is for something called as a class now don't worry about water classes now this class will have those two data that a salary and bonus and then it will have this function or what they call a method which will calculate the total pay by adding the salary and bonus so here you can see that what happens is there is a class called employee which has like two data that is like salary and bonus and then it has like a kind of a meth which will add up the sardian bonus and print the total pay now in some other place we will have some kind of a main method where we will create employees like this like employee Alex Linda John now if you remember this syntax is very similar to this like integer X comma Y comma Z like we have integer X comma Y comma Z we will have employee Alex comma Linda Comicon now if you remember when you say integer X comma Y comma Z that means X can hold an integer Y can hold a integer and Z can hold an integer that is you can assign some integer to X you can assign some integer to Y same way when you look at this when you say employee Alex comma Linda come at John that means Alex can hold an employee Linda can hold an employee and John can hold an employee just like X can hold an integer and Y convert an integer so what does it mean by holding an employee now when you see int X that means X can hold an integer and when you say employee Alex that means Alex can hold an employee that means Alex will have his own salary will have his own bonus and will have his own calculator total pay that means you can say Alex dot since employee has a salary alex will also have a salary dot salary equal to say 90,000 alex dot since employee has a bonus I will Alex would also have a bonus 20,000 and since employee has a method called calculate total pay Alex will have Alex will have a calculate total pay method so what happens now this then alex calls this calculate total pay method it will call this method called calculate total pay and it can add up the salary and bonus but whose salary and bonus will it add since alex is the person who calls this particular method it will add up Alexis salary and bonus that is now 90 and $20,000 and it will print out over here 110,000 same way for Linda Linda will have her own employee that is Linda as a type of employee so Linda will have access to her own salary her own bonus and she can call her own calculate total pay that is Linda dot salary and Linda can call her own calculate total pay so over here when Linda calls calculate total pay what happens is it will come here and it will add up salary and bonus whose salary and bonus since Linda is the person who called this method it will add up Linda salary and bonus and it will print out one hundred twenty thousand dollars this Alex Linda and John are what we call objects this Alex Linda and John are what you call objects so what is an object so an object can be defined as a copy of a class so if you notice Alex is nothing but it is a it has a copy of the class employee because the employee has salary and employee has born X Alex will also have a salary also have a bonus and Alex will have its own copy of calculate total pay method since Linda is a type of employee that means Linda has a copy of employee that means Linda will have her own salary her own bonus and their own calculate total pay method so an object can be defined as copy of a class and some people even call it instance of a class so if that is the case what is a class a class is a template for an object a template the meaning of template if you remember like if you use ms word document you can create new documents from like templates existing templates and then you can type in your own data so same way this is nothing but a template you create copies of this class employee and then you are saying your own data for each and every object so glass is nothing but a template for an object now what is a class usually contained this is called data this double salary and double bonus is called data and this is called a method so a class usually contains like data and methods it will have multiple data and multiple methods so a class contains data and methods now let us look at another example this time I will have a class called box which will have like two data length and width and which will have a method so you should look at this class box it has two data length and width and it has one method called calculate area which basically multiplies the length and width and it prints out that particular area so to make use of this class I will create objects of this class so here I have created two objects of this box class that is UPS and FedEx and since UPS is a type of box UPS will have its own length and width and FedEx will have its own length and width and UPS will have its own calculate area and FedEx will have its own calculate area so here you can see that UPS since it is a type of box UPS will have a length and I am assigning 10 and UPS would have its width assigned as 5 and when UPS calls calculate area it will multiply the length and width and it will print the area but whose length and width will it card since UPS is the one which called this method it will use UPS is length and upss width and it'll print out 50 the same thing when FedEx calls calculate area it will use FedEx is length and width and we'll print out the area so this UPS and FedEx are called objects so what are objects objects are nothing but copies of this class each and every object has a fresh copy of this class it has a fresh length fresh width and a fresh carrot area method and a class has data and methods and it is a template for objects so let us try the two examples that we saw that is the employee class and the Box class now let us create a package called day two dot class and object and in this let us create a new class called employee you need not check this public static void method so in this class employee we are going to have like a salary bonus and a calculate total pay method so now if you see I have this class called employee with salary and bonus and then I have this method called calculate total pay which adds up the salary and bonus and prints out total pay equal to the total pay it is give me some kind of an error now to remove this particular error you have to put this void in front of this method now you may be asking me what is this void we will learn more about this void and return type later on but for now remember this particular method is printing out something right here it is not returning this any value this method is not returning any value so why it means that this method is not sending any value anywhere else it is just printing it out right here of course you will see more uses of void later on let us save this class and let us create another class in the same package and we will call this like test employee and here let us select public static void main in this main method we are going to create objects of employee now to create objects of employee you can either do something like this so to create objects of employee you can either do something like this employee Alex Linda John and then we can initialize each and every object this is a requirement you have to do this or you can say employee Alex equal to new employee so this new employee is required you are initializing the object so once you have created this employee objects Alex Linda and John you can assign the salary and bonus for each and every object and called the calculate total pay you can say Alex dot and here you will immediately see salary bonus so that means this alex object as a copy of salary a fresh copy of salary that you can assign Alex Dot bonus equal to Alex dot calculate total pay so when you say Alex dot these things must come no when you run this save it and run this particular main method right click run as java application you will see that the total pay of 110,000 is printed same thing goes for Linda so Linda has her own salary her own bonus and Linda can call her own calculate total pay and when you save and run it you will get this Linda's total pay printed right here so let us try out the other example the Box class and create box objects I'm going to right click on this package new class so in this box class I have like a length width and a calculate area method which basically multiplies the length and width and then prints out area equal to area and to test this box class I'm going to create a test box class with the public static void main method I am creating two objects of box UPS and FedEx and I'm going to assign the length width and I'm going to call the calculate area method so when you run this box class this main method in the box class right click run as java application you will see that the area is 50 for this UPS areas 42 for this FedEx now a few conventions while creating classes class names should always start with a capital letter now these are conventions not a rule it will work even if you have a lowercase e here but then follow the convention so that your code looks neat for data data always starts with a lowercase now if you have two words like salary of employee that means the next word we'll start with the capital letter so you can have data like the salary employee or something like that then the next word will start with a capital letter but if you have only one word it's all lowercase same thing goes with a method name you start with the lowercase but the subsequent words you will have capital letters like calculate total pay T and P are capital so here also if you notice the double total pay the T is small letter but the P P is capital letter also if you have like lots of classes opened here and you feel like you know I want to close the others you can just right click on the class and say close others so you'll have only one class open at a time
Info
Channel: undefined
Views: 6,225,807
Rating: 4.886312 out of 5
Keywords: Java Programming Tutorial, AP Computer Science A exam, Java in 4hrs, Java (programming Language), Java (software Platform), Computer Science (Game), Software Tutorial, Java tutorial for beginners, java step by step, java patrick, java patrickvideos, best java tutorial, java training
Id: 3u1fu6f8Hto
Channel Id: undefined
Length: 87min 25sec (5245 seconds)
Published: Fri Oct 12 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.