Learn Java Programming with Beginners Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you before we introduce [the] Java Platform how about answering a very basic question What is a PC? Take your time The dictionary definition of computer is that it is an electronic [device] capable of performing computations [it] Has many peripherals like a monitor and keyboard to accept or display data to users and memory to [store] information But the heart of the computer is its processor which does all the thinking for the computer? Coming back to our definition of computer you may have noticed the key word electronic Thinking of it if a computer is an electronic device. How can it understand [it] display text? images videos and other Rich media information Well a computer understands only Electronic signals where a five volt current represents a binary one and a 0 volt current represents a binary 0 Your PC is continually bombarded with these electronic pulses which it interprets 8 bits of such signals are grouped together and text Numerical and symbols are identified by unique patterns of these 8 electric pulses for example the pound symbol is represented by the pattern 1 0 1 0 [1] 0 1 0 instruction to add two numbers is represented by pattern 1 0 0 0 0 0 1 1 This is 8-bit computing current day processors are capable of understanding and decoding 64 bits at a time Suppose you are asked to write a program to add two numbers say add one with [two] Assembly language is the most elementary form of software development languages? your code to add two numbers in this [language] would look like store number one at Memory location say [a] store number [two] at Memory locations say be Add contents of location a and B store results The way you feed this code to the processor would depend on the PC. You are using Back in the 50s when computers were huge and consumed a great deal of power you would convert your assembly language code into corresponding machine code of ones and zeros using mapping sheets and Punch the machine code into punch cards and then feed those punch cards to the computer With advancement in technology I/o devices were embedded you could directly type the program into the PC using a program called Assembler Converted into corresponding machine code and feed that [to] your processor That apart you will also have to make calls to operating systems providing functions to display the output of code With advancement in software development languages this entire assembly code could be shrinked into just one line print F1 Plus 2 A software called compiler is used to convert your C language code into assembly code and the Assembler converts it into corresponding machine code Though Present-day compilers come bundled with assembler they can directly convert your higher language code into machine codes Now suppose windows operating system is running on this intel processor Combination of operating system plus the processor is called the platform The most common platform in the world is the windows and intel called the wintel platform the other popular platforms are amd and Linux powerPC and Mac os x Now with change in processors the Assembly instructions will change For [example] the add instruction in intel may be called [edition] for amd or mass ad for PowerPC and Obviously with change in operating system the level and nature os level call. We'll also change as A developer I want my software program to work on all platforms available to maximize my revenues So I would have to buy separate compilers which convert my print f command into the native machine code But compilers come expensive and there's a chance of compatibility issues enter Java language The code to display addition of two numbers is system. Dot out [dot] print in one Plus two saved as a dot Java file Using the Java compiler the code is converted into an intermediate code called the bytecode The outcome is a dot class file This code is not understood by any platform, but only a virtual platform called the Java virtual machine This virtual machine resides in the ram of your operating system When the virtual machine is Fed with this bytecode it Identifies the platform it is working on and converts the bytecode into the native machine code in fact while working on your PC Or browsing a web whenever you see either of these icons be assured the Java virtual machine is loaded in your ram But what makes Java lucrative is that code once compiled can run not only on all PC platforms? But also Mobile's or other electronic gadgets supporting Java Has Java is a language as well as a platform To understand the compiling process in Java let's first. Take a quick look at the linking process in C suppose in the main you have called two functions f 1 and f 2 The main function is stored in a file a 1 dot C function f 1 is stored in a file a 2 Dot C Function f 2 is stored in a file a 3 dot C all these files are fed to the compiler Whose output is the corresponding object files which is the machine code? the Linker will club these three files together and produce an exe file During program run a loader program will load an exe into the ram for execution Let's look at the process [for] Java in your main You have two methods f 1 and f 2 Main method is stored in file a [one] dot [Java] f one is stored in file a to Java F 2 is stored in file a 3 dot Java the compiler will compile the three files and Produce Corresponding dot class files which consists of the [bytecode] Unlike C. No linking is done the JVM Java Virtual machine resides on the Ram during execution use in class loader the class files are brought on the ram the [bytecode] is verified for any security breaches using the bytecode verifies of the JVM Next the execution engine will convert the bytecode into native machine code This is just in time compiling. It is one of the main reasons. Why Java is comparatively slow There is more to the JVM architecture. Which will be discussed in later tutorials Open notePad declare a class with name a Declare the Main method Public status void main string a [r] [g] [s] Type the system dot out dot Print Ln Which displays the text first Java program? You save the file as first program dot Java Make sure to select all files while saving the file in Our working folder a Java file is created in A command prompt make sure your current directory path is the same as where your Java file is stored Enter Java C first program dot [Java] this will compile the code The [code] is showing error the string is not declared correctly Let's correct the error and save the file compile the code again After making the changes in the string the code is compiled successfully if you look in your working folder you can see that a file named a dot class has been created to execute the code enter the command Java followed by the class name as Expected output first Java program is displayed now if you copy and paste the same code in Ide like Netbeans the compiling and execution is [done] on the click of a button Using Ide [is] more convenient and can improve your efficiency, but since you are learning Java. We will stick to notepad Suppose you want to create banking software which will perform deposit and withdraw operations and show the balance Let's examine. How the software would be created using different programming languages? the earliest programming language were unstructured programming language a very elementary code of the banking applications in unstructured programming language will have two variables for one account number and another for account balance Suppose a deposit for $100 is made next you will obviously show the latest account balance Now the amount of $50 is withdrawn Again, you show the latest account balance [in] Fact for any further deposit or withdrawal operations you would program the same steps [again] and again Here the code for display of account number and account balance are exactly the same and it is repeated throughout the program Such repetition is a waste of programming efforts and increases coding costs and possibility of coding errors with the advent of structured programming common lines of your code were put in a structure called functions or methods and wherever required a simple call to their function was made Now if you can examine closely throughout the program we [are] dealing with Data or performing certain actions on the data in Fact having data and performing operations on the data is the very basic characteristics of any software program? Thought leaders in software development came up with an idea to combine Data and actions together Hence the birth of object-oriented programming commonly called [oops] The same code in oops [will] have some data and the actions to be performed on that data by this combining of Data and action We have several Advantages over structured programming namely abstraction and capsulation inheritance and polymorphism We will learn them [in] detail in succeeding tutorials To understand the principle of abstraction let's do an exercise List the top ten information like name address ETc of your customer to create a banking application Pause the tutorial and complete the exercise I'm sure you did a good job in identifying the information Some of the information [that] will be useful would be full name address contact number tax information favorite food favorite movie favorite actor Favorite band and I am sure you're wondering what the heck. I'm talking about Well the amount of information available for you as an individual is infinite We do not need all the available information for a customer to create a banking application but [only] the relevant ones in Essence we are selecting data from a larger pool hence the name abstraction but the same information once abstracted could be used in a wide [range] of Applications like this data set can not only be used in banks, but will also be useful in employee databases Hospitals government databases the Advantages the same set of [Data] can [be] reused Coming back to the banking example that we used earlier suppose a hacker is able to gain access to the code and Tries to deposit an invalid amount into your account Usually variables in a class are set as private and they can only be accessed Via corresponding methods So such access is not allowed Next he tries to use [the] deposit method to carry out his operation But the method implementation has checked for negative values and throws an error Thus you never expose your data to an external party which makes your application secure the entire code can be thought of as a capsule and you Can only communicate to it via messages? hence the name encapsulation consider the same banking application from Previous examples We're supposed to open two different account types One for saving and another for checking also known as current Let's compare and study how we can approach coding from a structured and object-oriented programming perspective? Instruct your programming I will create two functions One to withdraw and another to deposit since the working of these functions remain same across the accounts While using the object-oriented approach we will create two classes each having an implementation of deposits and withdrawals This is redundant extra work Now there's a change in requirement specifications for something that is so common in software industry You're supposed to add functionality privilege banking account with overdraft facility For a background overdraft. Is that you can withdraw and amount more than the available balance in your account Using functional approach, I would have to modify my withdraw function Which is already tested and perhaps code a method like this one which will take care of new requirements? Using oops approach. [I] just need to write a new class with unique implementation of [withdrawal] functions I never touched the tested piece of code What if the requirement changes further like to add Credit-card account with its own unique requirement of deposits? Using structural approach you have to change the tested piece of deposit code as well But using object-oriented approach you will just create a new class with its own unique implementation of deposit methods So even though structural programming seems like an easy approach initially oop wins in the long term But one may argue that across all classes you have repeated pieces of code to overcome this you create a parent class say a count and implement the same function of deposit and Withdrawn and Make child classes inherit account so that they will have access to withdraw and deposit functions in account class The functions are not required individually This is inheritance but wait there is [a] problem with the method of Implementation for withdraw for privilege and deposit for credit card is different To overcome this [you] can override the method implementation in your base class Such that when withdrawn method for the savings account is called this method is executed But when withdraw method for the privilege account is called the custom method is executed This is polymorphism Suppose you want to build software for a pet management system, especially meant for dogs Obviously you need to store information about various breeds of dogs in your system you need to model real-life entities that is dogs into software objects and The million-dollar question is how do you design such a software? First let's do an exercise You see the picture of three different kinds of dogs Pause the tutorial and list down the differences between them [I] Hope you did a good job determining the differences Some of those differences would be breed size age Color ETC if you think for a minute these differences also in essence are common characteristics shared by these dogs They can form data members for your dog object Next identify common behavior among the dogs. Here's a hint eat sleep sit and run So we have identified actions for our software objects for dogs Next I will define the class dogs with the data members such as size age ETC and with methods such [as] eat fit sleep Etc And for different values of Data members in my class, I will get different dog objects You can design any boot program using this approach Let's create a class account With instance variables a and B And a method set data to set the values for a and B And the method show Data to display the values for a and B a main method Wherein we create an object of account class And call methods set Data and show data Let's compile and run the code our Expected output a and b. Should be initialized to the values two and three respectively But the value is zero why? Let's investigate in The methods set data the arguments are declared with names a and B While the instance variables are also named a and B During [execution] the compiler is confused whether a on the left side of the assigned operator is The instance variable or the local variable hence it does not set the value of a when methods set Data is called the problem can be overcome By this keyword append both a and B with the this keyword followed by a dot operator during code execution when your object calls method set data the keyword this is replaced by the object Handler [O] B J now the compiler knows the a on the left hand side is [the] instance variable Whereas a on the right hand side is the local variable the variables are initialized correctly [an] Expected output is shown Suppose you are smart enough to choose different names for your instance variables and method arguments But this time around [you] create two objects of the class [each] calling the set data method how will the compiler determine whether it is supposed to work on the instance variable of object [1] or? Object 2 well the compiler Implicitly appends the instance variables with the this keyword such that when object 1 is calling a set data method Instance variables are appended by its reference Variable while object 2 is calling the set data method instance variables of object two are modified This process is taken care of by the compiler itself And you do not need to append this keyword explicitly unless there is some exceptional situation as in our earlier examples Consider that some line of your code is calling method M1 in The Stack a frame will be created from Method M1 the variable x in m 1 Will also be created in a frame for M1 in the stack? M1 is Calling Method M2 in The Stack a new frame will be created for M2 on top of the frame M1 Variable B. And C will also be created in the frame M2 in the stack Say Method M2 is calling Method M3 Again a frame M3 is created on the top of the stack M3 is creating an object for a class account Which has two instance [variables] p and Q? The new Operator will create an object in a heap the reference variable ref Will be created in a stack The assignment operator will make a reference variable To point to the object in the heap Once the method has completed its execution the flow of control will go back to the calling method which in this case is M2 The stack from Method M3 will be flushed out Since the reference variable will no longer be pointing to the object in the heap it will be eligible for garbage collection once M2 has completed its execution [it] will be popped out of the stack and all its variables will be flushed and Will no longer be available for use likewise for Method M1 Eventually the flow of control will return to the start point of the program which usually is the main method you Consider the Following Scenario the class media player Media player has two subclasses CD player and DVD player each having its own implementation of play method Another class combo drive is inheriting both CD and DVD which play method should it inherit? This can cause a serious design issue and hence Java does not allow multiple inheritance But suppose you have requirements where your class dog is inheriting class animal and Class pet but you cannot extend to classes in Java. So what would you do? the Solution is interface and interface is 100% abstract class and has only abstract methods a Class can implement any number of interfaces? class dog can extend to class animal and implement interface pet Let's create Class C [one] With Method M1 and a main method Create an object of C1 And call Method M1 Now let's say. I want to put this class into the package at the first line of code [right] the statement package p one Save file as Demo dot Java Let's compile the file Compilation is done in a class file created, but we have to create a package for this Use Command Java C - D demo [Java] this command Forces the compiler to create a package here dot operator means the current working directory [if] you check out the folder a package P1 is created which has class C1 if You compile the same file with double dots The package will be created in the parent directory. Which is the C drive? Let's say you want to create a sub package modify the statement as P1 dot p to compile the file In your P1 folder another folder P2 is created with Class C1 To execute the code mention the fully qualified [name] of the class that is package name and sub package name followed by the class name There are two types of errors compile-time and run-time Compile time errors can be again classified into two types syntax errors for example instead of declaring a variable integer int a You mistakenly declare it as in a for which the compiler will throw an error Or you can have a semantic error where you have declared a variable int a and after some lines of code you? again declare an integer as int a all these errors are highlighted when you compile your code a runtime error is called an exception error and is any event that interrupts normal flow of program [execution] Suppose you have coded a program to access a server and things work fine while you are developing the code during the actual production run the server is down and When your program tries to access it an exception is raised if you observe These are not any syntax or semantics errors, but arise due to situation out of the developers control for example The programmer would never know a server's downtime beforehand But blaming your code failure on environmental issues is not [a] solution You need robust programming which takes care of exceptional situations Such code is known as exception handlers in our example. Good exception handling would be when the server is down Connect to the backup Server or mirror server To its lemat this enter your code to connect to the server you will check if the server is down if Yes, write the code to connect to the backup server Such organization of code using if and else loops is not effective when your code has multiple exceptions to handle Hence Java provides for inbuilt exception handling where normal code goes into the try [block] and Exception handling code goes into the catch block try block will contain code to connect to the server and Catch block will contain the code to connect to the backup server in case the server is up the code in the catch block will be [ignored] in case the server is down an Exception is raised and the code in the catch block will be executed
Info
Channel: Guru99
Views: 643,837
Rating: 4.7526264 out of 5
Keywords: java tutorial for beginners, guru99 java, guru99 java tutorial, guru99 java videos, jvm, jvm architecture in java, what is java
Id: uWYPVz_i7W4
Channel Id: undefined
Length: 35min 5sec (2105 seconds)
Published: Mon Mar 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.