**Understanding Loops in Java**
A loop is a fundamental concept in programming that allows you to execute a block of code repeatedly. In Java, there are several types of loops, each with its own purpose and usage.
The for Loop has three parts: the initializer, the test expression, and the iterator. The first part, the initializer, declares a variable and sets its value. This is only executed once when the loop starts. The second part, the test expression, checks if the condition is true. If it is, the loop continues. In our example, we use this to see if the variable I is still less than or equal to 10 after each iteration. The third section, the iterator, increments the value of the variable by 1. This can be achieved using a single statement like `I = I + 1`, which is equivalent to `I++`.
The while Loop also has two main parts: initialization and iteration. In the initialization phase, we need to declare variables that will be used in the loop and assign them values outside of the loop. In our example, we initialize a variable J to 11 before the loop starts. The iterator section is where we write code that changes the value of the variable on each iteration. In our case, it's `j++`. This means that as long as the condition inside the while loop is true, the loop will continue.
Another type of Loop in Java is called do-while Loop. It works similarly to a while loop but with one key difference - the test expression is evaluated at the end of the loop, not at the beginning. This means that a do-while loop is guaranteed to execute at least once, whereas a while loop may skip iterations if the condition isn't met.
**Java as an Object-Oriented Programming Language**
Java is an object-oriented programming language, and understanding its core concepts is essential for mastering Android programming. One fundamental concept in Java is what makes an "object." An object is essentially a set of methods or functions along with a set of data. The data and the methods belong to the object and work together as part of it.
Let's take a look at an example program that creates a simple counter object. This object has one piece of data, the integer variable `count`, and three methods besides the main method: `increment` and `getCount`. We notice that all the methods we've declared so far start with `public void`, but the difference between `void` and `int` is crucial. `void` declares a method that doesn't return anything, meaning there will be no result coming back out of it. `int`, on the other hand, tells us that the method will return an integer.
**Constructors in Java**
In our example program, we have a constructor, which is called only once when the object is created. It's used to initialize the object with default values and perform any necessary initialization tasks. In this case, our constructor simply sets `count` to zero. Constructors are special methods that don't have a return type, not even `void`. They're used to create new objects from a class.
**Inheritance in Java**
Another fundamental concept in Java is inheritance, which allows us to create classes that inherit behavior and data from other classes. We can create an abstract class that represents an abstract idea and then derive classes from it, known as subclasses or child classes. In our example program, we have an abstract class called `Animal` and a derived class called `Elk`. When we derive a class from another class using the keyword `extends`, some of the methods and data in the superclass automatically become part of the subclass.
By deriving the `Elk` class from the `Animal` class, we can inherit its behavior and data. We can see this by checking out the source code for our example program. The `Elk` class has access to all the methods and variables in the `Animal` class without needing to explicitly define them again.
Overall, understanding loops, constructors, and inheritance is essential for mastering Java programming and Android development. These concepts form the foundation of object-oriented programming and are used extensively throughout Android programming.
"WEBVTTKind: captionsLanguage: enhello there my name is Gary Sims from Android authority if you've ever wanted to write an Android app you're probably going to have to do some programming now Java is the official language of the Android operating system not only is it used to create apps it's also used to write actually parts of the OS itself now when you want to write an app there are two aspects the first part is the Java language and the second part is the Android software development kit and today we're going to be looking at the first part Java to start writing Java programs you need a way to compile source code and turn it into an executable for the Java runtime the normal way to do this is to install the Java development kit which you can download from oracle's website however if you aren't ready to install the Java development kit then you want a quick route to trying your first Java program then I recommend coding.com coding gives you access to a free virtual machine with all the compilers and tools that you need to run a Java program as well as developing other languages like go python node and C to sign up just visit coding.com type in your email address and a password of your choice and then click sign up the coding virtual machine comes with an IDE and command line access the web view of the virtual machine is divided into four parts on the left is the coding control panel with access to your account details the virtual machines you have created and so on next is is the file manager which shows all the files and folders you have on your virtual machine next to that the screen is spit into two the top half is an editor for writing your code and the bottom half gives you access to the command line now all the code for today's video is available on the Android Authority website in the written companion which will be linked in the video description below so our first program is the hello world program cut and paste the hello world code into the editor and and save it as helloworld.java now in Java the file name of the source code and the class name must be the same the first line of the code declares a class called hello world so the source code must be called helloworld.java in coding to save the file hover the mouse over the tab for the code probably called untitle do text and click on the little arrow then click save as enter hello world. jaava as a file name and click save at the bottom of the screen is the command line the virtual machine is running Linux and the terminal is running bash to compile the program type in Java c space helloworld.java the compile should only take a second or two you can then run the program by typing Java hello world the reward is the text hello world being displayed in the terminal congratulations so let's just take a moment to look at what happened first the source code the file does three things it declares a class called hello hello world it defines a method a function in the hello world class called Main and that main method calls system.out.print line to Output some text each Java program must Define a method called Main in at least one of its classes it is the entry point where the program starts executing once the program is written you then have to compile it and to compile the program you first call Java C and then to run it you call Java the first is to compile and hence the letter c at the end and the second is the Java virtual machine when writing computer programs you will often need to store data for temporary use for example in an Android game you might want to store the score for the current player these bits of data are stored in variables a variable is a box in which you can put some data and then come back to it later to retrieve it since data comes in different forms a variable needs to define a type which tells Java what you want to store in it some of Java's primitive data types include int for integer double for double Precision floating Point numbers and booleans for true or false let's look at the uh variable test program it's a simple program that sets the value of a variable prints out the variable to the console changes the variable and then prints it out again cut and paste the code into the editor and then save it as variable test. Java to compile it type Java C see variable test. Java and to run it use Java variable test as you can see the program defines a variable called I and then gives it an initial value of one the value of I is printed to the console then I is set to a new value of I + 24 now since I is already 1 that really means 1 + 24 which is 25 and so the new value is then printed out onto the console try modifying the program yourself to use a double rather than an INT set I to something like 1.3 and increase its value by a decimal number like 24.9 and see what happens if you take a look at the print line method you will see that an integer is being added to a string the string being the value of I is colon and then of course it does plus I what actually is happening here is that Java knows that the first part of the expression is a string so it generates a string value for I in other words it converts the one or the 25 into the characters 1 or 25 and then concatenates them to the string giving you the final string output the value of I is one or the value of I is 25 strings are an important part of any programming language including Java unlike int or Boolean a string isn't a primitive type it's actually a class when you create a string variable you are creating a string object and notice that uses the capital S rather than the small S as an object it has certain properties like a value the string itself and its length strings can be manipulated in lots of different ways including being dissected concatenated compared and searched so let's take a look at the playing with strings source code cut and paste it into the web editor save it as playingwith strings. jaava and compile it and run it using Java C and Java as we have done previously the first part of the program creates a string called hello and gives it a value of hello world although this might look similar to how you declare and assign an integer or a different primitive type actually there's a lot more going on here Java allows simple operators like equals and plus to be assigned to objects so really although it might look like you're just assigning the value hello world to the string what's actually happening is a new object of type string is being created and the value hello world is being passed to its Constructor but we'll talk more about that in the object section the next part shows how you can concatenate strings in this case an exclamation point is added to the end of the string now since string is an object it can have methods so string do substring is a method which Returns part of a string in this case the first five characters string. trim is another method that removes leading and trailing spaces the last part of the program demonstrates string do to Upper C case and string do2 lowercase methods computers are good at doing repetitive tasks as do a repetitive task in a programming language you use a construct called a loop now Java has three types of loop the while loop the for Loop and the Doh Loop and they're all basically the same in that they keep on looping round until a certain condition is met so let's take a look at a Loops example this particular one will show you how to print out the numbers 1 to 10 11 to 20 and 21 to 30 using three different types of Loops as before copy of the source code from the written companion and put it into a file called loops. Java you'll then be able to compile it and run it as you have done the previous programs the for Loop has three parts the first part is the initializer that's the in I is equal to one and it's only executed once what it does is declare an integer called I and sets its value to one then comes the test expression I is less than or equal to 10 this expression will be tested every time the loop goes round if the result of the test is true it will go round again and in our example it's used to T whether I is still less than or equal to 10 after each iteration the third section the iterator will be executed in our example it increases the value of I by 1 note that I is equal to I + 1 is actually the same as I ++ the W Loop is very similar to the four Loop except it doesn't contain the initialization phase nor the iterator phase that means the initial Iz ation needs to be done separately in our example it is the int J is equal to 11 which appears before the loop starts the iterator also needs to be coded separately in our example it is the j++ line which can be found inside the loop just after the call to print line a do wild Loop is very similar to a wild loop with one big difference the test to see if a loop should continue is at the end of the loop and not at the start this means that a do while is guaranteed to execute at least once but a while loop doesn't even need to execute a all if the conditions aren't met at the entrance into the loop like the while loop the initialization needs to happen outside the loop and in our example this is the int X is equal to 21 line and the iterator needs to occur inside the loop x++ when X goes over 30 the loop will stop as I mentioned before Java is an objectoriented programming language and to really succeed in Android programming you're going to need to understand some important oo Concepts the first one is what is an object an object is basically a set of methods or functions along with a set of data the data and the methods belong to the object and work for the object let's have a look at an example here is a program that creates a very simple counter object the counter object has one piece of data the integer variable count and three methods besides the main method counter increment and get count leaving the first method out for the moment you can see that increment and get count are very simple the first one adds to the internal variable count and the second one just returns its value until now all the methods we have declared started with public void but if you notice the get count method starts with public int but the difference between void and int is this void declares that the method doesn't return anything there will be no result coming back out of the method but int tells us that the method will return a number specifically an integer you can actually create methods that return all kinds of data including objects notice that the first method has the same name as the class itself I.E counter and it doesn't have a return type not even void this is a special method called a Constructor The Constructor is called only once at the moment that the object is created it is used to initialize the object with default values and perform other necessary initialization tasks in this example all it does is set count to zero now there's one last concept we need to look at before we come to a close and that's the idea of inheritance now in Java you can create an abstract class that represents an abstract idea and derive classes from it which are called subclasses in the written companion I show you how to create a class called animal it could be any animal and then I derive from it a class called elk now when you do that what happens is some of the methods and some of the data that are in the superclass animal automatically get inherited now belong to the subass elk go and check out the source code in the written companion have a read about the explanations I give you there and see what you think well my name is Gary Sims from Android authority I hope you've enjoyed this video if you did please do give it a thumbs up I really do recommend that you try out some of these examples I've given try out what's written in the written example go to coding.com get yourself an account install the Java uh compiler if you need to on your PC or on your Mac just try it out can't do any harm it's good fun if You' got any questions why not leave them in the comments below we'll try our best to help you out if you've got any experiences about learning Java you want to share with us leave them in the comments below if you've got maybe some books that you recommend also leave them in the comments below and as for me I'll see you in my next video hhello there my name is Gary Sims from Android authority if you've ever wanted to write an Android app you're probably going to have to do some programming now Java is the official language of the Android operating system not only is it used to create apps it's also used to write actually parts of the OS itself now when you want to write an app there are two aspects the first part is the Java language and the second part is the Android software development kit and today we're going to be looking at the first part Java to start writing Java programs you need a way to compile source code and turn it into an executable for the Java runtime the normal way to do this is to install the Java development kit which you can download from oracle's website however if you aren't ready to install the Java development kit then you want a quick route to trying your first Java program then I recommend coding.com coding gives you access to a free virtual machine with all the compilers and tools that you need to run a Java program as well as developing other languages like go python node and C to sign up just visit coding.com type in your email address and a password of your choice and then click sign up the coding virtual machine comes with an IDE and command line access the web view of the virtual machine is divided into four parts on the left is the coding control panel with access to your account details the virtual machines you have created and so on next is is the file manager which shows all the files and folders you have on your virtual machine next to that the screen is spit into two the top half is an editor for writing your code and the bottom half gives you access to the command line now all the code for today's video is available on the Android Authority website in the written companion which will be linked in the video description below so our first program is the hello world program cut and paste the hello world code into the editor and and save it as helloworld.java now in Java the file name of the source code and the class name must be the same the first line of the code declares a class called hello world so the source code must be called helloworld.java in coding to save the file hover the mouse over the tab for the code probably called untitle do text and click on the little arrow then click save as enter hello world. jaava as a file name and click save at the bottom of the screen is the command line the virtual machine is running Linux and the terminal is running bash to compile the program type in Java c space helloworld.java the compile should only take a second or two you can then run the program by typing Java hello world the reward is the text hello world being displayed in the terminal congratulations so let's just take a moment to look at what happened first the source code the file does three things it declares a class called hello hello world it defines a method a function in the hello world class called Main and that main method calls system.out.print line to Output some text each Java program must Define a method called Main in at least one of its classes it is the entry point where the program starts executing once the program is written you then have to compile it and to compile the program you first call Java C and then to run it you call Java the first is to compile and hence the letter c at the end and the second is the Java virtual machine when writing computer programs you will often need to store data for temporary use for example in an Android game you might want to store the score for the current player these bits of data are stored in variables a variable is a box in which you can put some data and then come back to it later to retrieve it since data comes in different forms a variable needs to define a type which tells Java what you want to store in it some of Java's primitive data types include int for integer double for double Precision floating Point numbers and booleans for true or false let's look at the uh variable test program it's a simple program that sets the value of a variable prints out the variable to the console changes the variable and then prints it out again cut and paste the code into the editor and then save it as variable test. Java to compile it type Java C see variable test. Java and to run it use Java variable test as you can see the program defines a variable called I and then gives it an initial value of one the value of I is printed to the console then I is set to a new value of I + 24 now since I is already 1 that really means 1 + 24 which is 25 and so the new value is then printed out onto the console try modifying the program yourself to use a double rather than an INT set I to something like 1.3 and increase its value by a decimal number like 24.9 and see what happens if you take a look at the print line method you will see that an integer is being added to a string the string being the value of I is colon and then of course it does plus I what actually is happening here is that Java knows that the first part of the expression is a string so it generates a string value for I in other words it converts the one or the 25 into the characters 1 or 25 and then concatenates them to the string giving you the final string output the value of I is one or the value of I is 25 strings are an important part of any programming language including Java unlike int or Boolean a string isn't a primitive type it's actually a class when you create a string variable you are creating a string object and notice that uses the capital S rather than the small S as an object it has certain properties like a value the string itself and its length strings can be manipulated in lots of different ways including being dissected concatenated compared and searched so let's take a look at the playing with strings source code cut and paste it into the web editor save it as playingwith strings. jaava and compile it and run it using Java C and Java as we have done previously the first part of the program creates a string called hello and gives it a value of hello world although this might look similar to how you declare and assign an integer or a different primitive type actually there's a lot more going on here Java allows simple operators like equals and plus to be assigned to objects so really although it might look like you're just assigning the value hello world to the string what's actually happening is a new object of type string is being created and the value hello world is being passed to its Constructor but we'll talk more about that in the object section the next part shows how you can concatenate strings in this case an exclamation point is added to the end of the string now since string is an object it can have methods so string do substring is a method which Returns part of a string in this case the first five characters string. trim is another method that removes leading and trailing spaces the last part of the program demonstrates string do to Upper C case and string do2 lowercase methods computers are good at doing repetitive tasks as do a repetitive task in a programming language you use a construct called a loop now Java has three types of loop the while loop the for Loop and the Doh Loop and they're all basically the same in that they keep on looping round until a certain condition is met so let's take a look at a Loops example this particular one will show you how to print out the numbers 1 to 10 11 to 20 and 21 to 30 using three different types of Loops as before copy of the source code from the written companion and put it into a file called loops. Java you'll then be able to compile it and run it as you have done the previous programs the for Loop has three parts the first part is the initializer that's the in I is equal to one and it's only executed once what it does is declare an integer called I and sets its value to one then comes the test expression I is less than or equal to 10 this expression will be tested every time the loop goes round if the result of the test is true it will go round again and in our example it's used to T whether I is still less than or equal to 10 after each iteration the third section the iterator will be executed in our example it increases the value of I by 1 note that I is equal to I + 1 is actually the same as I ++ the W Loop is very similar to the four Loop except it doesn't contain the initialization phase nor the iterator phase that means the initial Iz ation needs to be done separately in our example it is the int J is equal to 11 which appears before the loop starts the iterator also needs to be coded separately in our example it is the j++ line which can be found inside the loop just after the call to print line a do wild Loop is very similar to a wild loop with one big difference the test to see if a loop should continue is at the end of the loop and not at the start this means that a do while is guaranteed to execute at least once but a while loop doesn't even need to execute a all if the conditions aren't met at the entrance into the loop like the while loop the initialization needs to happen outside the loop and in our example this is the int X is equal to 21 line and the iterator needs to occur inside the loop x++ when X goes over 30 the loop will stop as I mentioned before Java is an objectoriented programming language and to really succeed in Android programming you're going to need to understand some important oo Concepts the first one is what is an object an object is basically a set of methods or functions along with a set of data the data and the methods belong to the object and work for the object let's have a look at an example here is a program that creates a very simple counter object the counter object has one piece of data the integer variable count and three methods besides the main method counter increment and get count leaving the first method out for the moment you can see that increment and get count are very simple the first one adds to the internal variable count and the second one just returns its value until now all the methods we have declared started with public void but if you notice the get count method starts with public int but the difference between void and int is this void declares that the method doesn't return anything there will be no result coming back out of the method but int tells us that the method will return a number specifically an integer you can actually create methods that return all kinds of data including objects notice that the first method has the same name as the class itself I.E counter and it doesn't have a return type not even void this is a special method called a Constructor The Constructor is called only once at the moment that the object is created it is used to initialize the object with default values and perform other necessary initialization tasks in this example all it does is set count to zero now there's one last concept we need to look at before we come to a close and that's the idea of inheritance now in Java you can create an abstract class that represents an abstract idea and derive classes from it which are called subclasses in the written companion I show you how to create a class called animal it could be any animal and then I derive from it a class called elk now when you do that what happens is some of the methods and some of the data that are in the superclass animal automatically get inherited now belong to the subass elk go and check out the source code in the written companion have a read about the explanations I give you there and see what you think well my name is Gary Sims from Android authority I hope you've enjoyed this video if you did please do give it a thumbs up I really do recommend that you try out some of these examples I've given try out what's written in the written example go to coding.com get yourself an account install the Java uh compiler if you need to on your PC or on your Mac just try it out can't do any harm it's good fun if You' got any questions why not leave them in the comments below we'll try our best to help you out if you've got any experiences about learning Java you want to share with us leave them in the comments below if you've got maybe some books that you recommend also leave them in the comments below and as for me I'll see you in my next video h\n"