‫Instructor: Hi within this section
‫we're going to deep dive into Java
‫and we're going to learn about Java fundamentals.
‫So that we can understand what's going on
‫when we write some code, what's variables, what's sets,
‫what's arrays and stuff.
‫So let me create an empty activity.
‫In fact, we're not going to create an empty activity,
‫we're going to create a project.
‫But this time we're not going to even have an activity.
‫So as I said before,
‫in every app we have activities, right,
‫because that's how users interact with apps.
‫But this time we're not gonna build any app,
‫we're just going to learn about Java fundamentals.
‫So we don't need an activity in order to do that
‫because we're not going to be doing any designs,
‫we're not going to create any user interfaces,
‫we're just going to write some Java code.
‫So I can just as well say add no activity at all, okay?
‫So this will create an empty project for me
‫and we're going to work on Java code
‫rather than layouts or examas and stuff.
‫We're just going to focus on Java code.
‫So this may create some kind of issue for your computers
‫and we're going to deal with it as well.
‫So this time we're going for add no activity
‫but, again, most of the time like 99%
‫when you're dealing with real android development,
‫you need an activity so that your user can se some screen
‫where they choose to interact with the app.
‫So I'm gonna call this Java learning or Java basics.
‫Okay?
‫And package name will be my name com.exam.javabasics
‫So you can change the save location by clicking over here
‫and choose any folder like this, okay?
‫And then you can name whatever you want.
‫Like this, you can just choose whatever
‫you may want to choose from here and add some slash
‫to write your own folder name like this Java basics.
‫If you want you can just go along with the default.
‫Choose Java from language
‫and minimum API level doesn't even matter at this point.
‫Just go for something like API 28, 23.
‫This is an important choice
‫when it comes to Android development,
‫but this time we're just going to test some Java code.
‫So make sure your androidX artifacts is chosen as well.
‫If it's not choosable it's good
‫because it will be on by default, right.
‫Now, so after you create your project
‫this time you won't find anything on your left hand side
‫because there will be some folders
‫but there will be no mainactivity.java or mainactivity.xml
‫We're going to add them locally,
‫we're going to add them manually
‫so that we can start writing some Java code.
‫So this is what's gonna happen.
‫We're going to wait until it's finished building
‫and you will see this folder empty as it is.
‫So all you have to do is
‫just right click on that folder under your java
‫with inside your package name.
‫And as you can see there isn't even a layout folder
‫on your resources folder, okay?
‫Because we don't have any activity right now.
‫So in order to make this work
‫I'm going to add a new Java file over here, Java class.
‫So in order to do that, again, choose the folder
‫that you want to create your Java file in.
‫And this is the folder, and say New, and say Java Class.
‫Once you do that you have to choose a name
‫and we're gonna go for Main with a capital M.
‫So, as I said before, with the Pascal syntax
‫we only use it with classes like this, okay?
‫When you deal with a class just make sure
‫that the first letter of that class
‫is capital name, capitalized letter like this Main.
‫And we're going to see what a class is later on
‫but just right now we need it in order to write some code.
‫As you can see, we have this class which is public class
‫and this will let us run some Java code
‫inside of our Android Studio application.
‫But before we write some code
‫we need to have an entry method.
‫Maybe that you remember that when we add an activity before
‫we had it's own create methods.
‫So this was the first method to be called
‫when an activity is created.
‫Now we don't have any activity
‫so we have something to spark this application.
‫We need and entry point.
‫We call this Main method public static void main in Java.
‫This is the first method to be executed
‫when a class of Java is run.
‫So that what's we're going to do, actually.
‫You don't even know what a method is, I know.
‫Just follow along with me and write
‫public static void main with lowercase m this time,
‫not uppercase, okay, main.
‫So this is what we are looking for.
‫This is the class with an uppercase M,
‫and this is the method with a lowercase m.
‫And then you're gonna have to open and close
‫some parenthesis and say String
‫like this with an uppercase S
‫and add this array annotation at the end of the string.
‫Okay?
‫So you can do this
‫by holding Alt and 8 or 9 in your keyboard.
‫And write args.
‫So what this basically does is
‫this is a method that it takes a parameter,
‫an arguments parameter as in string,
‫as in an array of strings, okay?
‫And you don't know what a parameter is,
‫you don't know what an array is,
‫you don't know what a string is,
‫you're going to learn it.
‫All you have to know right now
‫is that this is the main method.
‫This is the entry point for Java code.
‫So this is what will get called
‫when we execute this code.
‫So whatever we right under this method over here
‫is going to be executed when we run this application.
‫Okay?
‫So this, let's answer on some Java code.
‫So in order to write any code, of course,
‫we're going to use this main method.
‫And first thing that we are going to do
‫is to make sure that we are writing
‫inside of these curly braces, okay?
‫I'm not writing anything outside of this main method.
‫So with inside of curly braces
‫means that I'm writing with inside of this main method.
‫So make sure you're in the curly braces section,
‫this is a coding section, and you're writing over here.
‫And the first thing that we are going to write is,
‫of course, will be
‫the hello world or hello Java application.
‫So this is a cliche that every programmer uses
‫when they start a new programming language
‫they have to write hello world
‫so that they can print out something, they can make it work,
‫and this is the first application
‫for most of the programmers.
‫If you write S-O-U-T you can see this brings us
‫something called System.out.print line.
‫Of course, you can write it this way as well
‫System.out.println.
‫So this basically prints something to the log.
‫So open your quotation marks like this.
‫If you hit a quotation mark
‫it will also complete the second one.
‫And between this quotation marks
‫I'm going to write hello Java.
‫So this is our first piece of code.
‫This will be written in the log
‫so that we can keep track of what's goin on
‫inside of our application.
‫So in order to run this
‫I'm going to right click on this main,
‫but the small main, not the public class one.
‫I'm going to right click onto the main like this
‫and I'm gonna say run.
‫So this will run the main method
‫and you will see the result in the logcat like this.
‫But as you can see, I'm getting an error.
‫So maybe you didn't get an error,
‫maybe you have seen hello java and it's perfectly fine.
‫It's actually better than my condition.
‫But if you get an error like this
‫I'm going to show you how to overcome this problem as well.
‫Because many of the students face this problem
‫when they first start writing Java code.
‫If you open your build out gradle you have to make sure
‫that this android test implementation is implied.
‫So if we don't see this line
‫at the bottom of your build out gradle,
‫at the last line of your dependencies,
‫you should add it yourself.
‫You're more than welcome to pause the video
‫and just write it exactly like this into your dependencies.
‫So that's what I have copied and pasted.
‫Once you have copied and pasted this line,
‫or once you have written it down like that
‫all you have to do
‫is just come over to the right upper side
‫of the Android Studio and say sync now.
‫I'm gonna do it with you now.
‫So say sync now from here
‫and it will just add this dependency as well.
‫It will try to fix the problem.
‫So most of the time problem will be fixed at this point.
‫Okay?
‫But again, there might be some other blocks as well.
‫So make sure this is written exactly like this
‫androidx.test.ext:junit:1.1.1
‫and close the quotation mark
‫with only single quotation this time
‫rather than double quotation marks.
‫Okay?
‫And I'm writing this inside of my dependencies curly braces.
‫And then I said sync now.
‫So let's see if it solved the problem.
‫I'm gonna right click over here and say run.
‫And here you go, I'm still getting the error.
‫It's good for me because I get to show you
‫what you can further do in order to solve this problem.
‫But most of the time your problem will be solved
‫at this point.
‫So if you're still getting this error
‫all you gotta do is just close this down,
‫close the Android Studio down, okay?
‫And open your Android Studio folder,
‫Java learning folder
‫that you have created for this app, okay?
‫And you're gonna have to delete something
‫so let me show you.
‫You're going to find the gradle, okay, gradle folder.
‫And inside of you gradle folder
‫there is something called wrapper.
‫And just delete all the gradle folder, okay?
‫This will reset the application.
‫Once you start the application from Android Studio
‫one more time like this
‫it will download all the dependencies one more time.
‫So right now it looks like it's ready,
‫but once I do that, once I hit the run from here
‫it will build it again and it will try to create
‫that gradle folder one more time.
‫So it's gonna take some time
‫but most of time it will just solve your problem.
‫But again, as you can see, it didn't solve my problem
‫so I'm gonna try one more time.
‫And as you can see, we get a different error
‫but we're still getting error.
‫We're still not seeing hello Java.
‫Great because I will continue showing you
‫what you can do further to solve this problem.
‫So I'm gonna go over to this build out gradle
‫and, as you can see, it says that
‫there's some newer dependencies over here.
‫For example I see this highlighted dependencies
‫like this, okay, in my dependencies coding block.
‫And, as you can see, it's giving me some kind of message
‫saying that you're using 1.0.2 but there is 1.1.0 available.
‫So most probably you are getting the same message as well.
‫So I'm going to change the versions as it instructs me.
‫So if you're seeing different versions
‫like if you see 1.5.0 rather than 1.2.0 just do it.
‫I'm trying to do the latest version of these dependencies
‫and it is the same thing with my junit.1.1.1 as well.
‫So if it gives you some kind of upgrade message
‫just take everything to the latest version.
‫So let me try to run this.
‫And here we go, it says hello java and it worked.
‫If it wouldn't work then what I would do next
‫is to shut down it again,
‫delete the gradle folder one more time from here, okay,
‫from my Android Studio folder.
‫And then come back over here and run this one more time.
‫Because sometimes it doesn't actually refresh the gradle
‫when you change the dependencies.
‫But you will have to delete it and just do it one more time.
‫So let me try to print line one more thing.
‫And let's say 5 x 2 right now.
‫I'm not writing this between any quotation marks
‫as you can see.
‫And I'm going to say run.
‫And we're gonna see hello java and we're gonna see 10.
‫So when you put something between quotation marks
‫it means that it's a text, it's a string.
‫So it will be recognized as a text.
‫So if you don't put anything between some quotation marks
‫then it will be kind of code.
‫For example, at this example at this instance,
‫we have a code multiplying five by two.
‫So it's running five by two and it's printing out the result
‫over here in the logcat.
‫And if it doesn't work out for you anyhow,
‫just create a new project, okay,
‫from here or from the menu as well.
‫But this time go for empty activity
‫rather than no activity.
‫So if it doesn't work for you...
‫I'm gonna call this Java learning
‫in order to avoid any confusion.
‫And I'm going to open this with activity
‫rather than none activity.
‫And we can do the same thing over here as well, right?
‫All we have to to is write everything under onCreate
‫rather than our main function.
‫So why don't we do that?
‫Because when we do that we have to use emulator, right?
‫In order to just take the emulator just out of the picture
‫you can work with no activity at all, it will be faster.
‫But if it doesn't work out no matter what you do,
‫then it means that you're gonna have to open this
‫with a main activity so that you can write everything
‫under onCreate like this, okay?
‫And you can run it with an emulator
‫and do the same thing again.
‫So let me write exactly the same thing, hello Java
‫and five multiplied by two and run it on an emulator.
‫So when we run it we're gonna see an empty screen
‫or something called hello world like a text view,
‫the default text view.
‫But at the same time we can actually follow along
‫with inside of the logcat as well.
‫It will be a little bit slower
‫since we're going to deal with emulators,
‫but it's also manageable.
‫So if you can not make it work without an activity
‫than you can just have it with activity like this.
‫There will be some kind of disadvantages as well.
‫For example, I'm going to create
‫some additional Java classes in the left hand pane
‫and when I do that
‫maybe you will have to create new projects
‫rather than new Java files, but it will be fine eventually.
‫You can do whatever I'm trying to do
‫inside of Java files from here.
‫And if you open the logcat section
‫on your bottom side of your Android Studio
‫you can see all the logs.
‫And if you write something like
‫system.out in the search bar,
‫it's actually a filtering mechanism.
‫So that you can filter only the system out print lines
‫so that you can see what's going on over here.
‫For example, let me do some other example.
‫Like hello and run emulator from scratch.
‫It will compile and it will run
‫and it will show me the logcat eventually.
‫And as you can see, it's not very slow
‫but it's not as convenient as Java file.
‫Okay?
‫So you right now a lot of ways to run Java codes.
‫So we're gonna continue with this file in the next lecture.