Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Java_Introduction

Java_Introduction

Published by sailaxmib4, 2017-02-28 20:34:41

Description: Java_Introduction

Search

Read the Text Version

Java IntroductionJava is a programming language originally developed by James Gosling at Sun-Microsystems (which is now a subsidiary of Oracle Corporation) and released in1995 as a core component of Sun Microsystems. The language derives much ofits syntax from C and C++ .Jdk 1.8 is the latest version.Url to download java->http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html Once java is installed you can determine which version of the JDK is the defaultby typing java -version in a Terminal window. java –version

Compiling a java program from Command Line-> javac xyz.javaRunning the java program from Command Line-> java xyzNotes:1. All Java Statements are saved as java file with extension '.java'.2. Java compiler generates a classfile based on java file.3. Classfile contains bytecode(1's and 0's) which is understood only by JVM.4. JVM is an interpreter means executes the code line-by-line.5. JVM converts the bytecode line by line into machine level language.6. Java Compiler and JRE together called as JDK.7. Java is platform indepent means you can compile the program once andexecute onany platform the only condition is it needs specific jvm. Ex: Linux - xyz.java --> javac xyz.java -> xyz.class Windows-> copy xyz.class and run the programs as ‘ java xyz’EclipseEclipse is an IDE-Integrated Development Environment. Different versions ofEclipse available in the market. Juno,Helios,Indigo

Steps to Download:1. Access the url www.eclipse.org/downloads.2. Click on Eclipse IDE for Java Developers.3. Download Zip file.4. Unzip file.5. Click on eclipse icon.Creating a ProjectFile Menunewproject Select Java Project(In Eclipse you are going to run the program using ctrl+F11 and Classfilesare generated automatically in bin folder)Eclipse ShortcutsCtrl+Shift+/ Add Block Comment around selection ( adds '/... */' )Ctrl+Shift+\ Remove Block CommentCtrl+shift+o For Organizing the importsCtrl+A & Ctrl+I IdentationCtrl+Space Opens Content Assist (e.g. show available methods or fieldnames) ExecutionCtrl+F11Ctrl+1 Open Quick Fix and Quick Assist

Data Types

byte 1 byte b=2 short 2 bytes s=5 int 4 bytes i=10 long 8 bytes l=20 Time 1 hour = 1 * 60*60*1000 1second = 1000 milliseconds In java time stored in long. float 4 bytes f=2.5f;//Valid f=2.5;//Not Valid Decimal numbers are by default double. double 8 bytes d=2.5;//Valid d=2.5d/2.5D//Valid char 2 byte character code c='a'// c=\"a\"(not correct)//c='ab'(notcorrect) boolean true/false b= true/false.

Contents of Java Program 1. Keywords-Predefined in java (int,final,void) 2. Variables in java int i; // i is a variabled of type integer int i;// Declaration i=5;//intializing int i=5;//Declaration and IntializationNotes1. Keywords should not be used for variables. int public =5;//Not Valid2. Use Alphanumeric Characters int a1=5; //Valid Combination of numbers and characters3. Always start the variables with alphabets should not start with numbers. int 1a=5;//Not valid int a1=5; // its valid4. Special Characters are not allowed in java for variables.MethodsMethods are mainly useful for reusability. Ex:Login();Syntax of a Method:

modifier returntype methodname(arg1,arg2,arg3) { //Body of the method return value; } ()-Paranthesis// Methods []-Square Bracket// Arrays {}-curly braces// Starting and end of the class/body Reference Variable: Syntax of reference Variable: ClassName refVar = new ClassName();Class MembersClass Members means variables and methodsThey are two types of class Members: 1. Non-static Members 2. Static Members //non static int i =5; public void sample(){

} //static static int i =10; public static void sample(){}OOPs concepts:1. Object2. Inheritance3. Abstract Class4. Interfaces5. PolymorphismObjectObject is an real world entity which has state and behavior

Class is a pro-type/ class is a blue print. Class is useful to create Objects. Class isalso a datatype which helps us to create Objects ClassName refVariable = new ClassName();Constructor Syntax of constructor ConstructorName() { //Constructor body }Notes1. A constructor is a block just like a method which is invoked at the time of objectCreation.2. The constructor name is same as class Name.3. The constructor should not have any return type.4. A constructor is developed to initialize the object variables at theObject creation.5. Every java class should have a constructor, if we don’t developa constructor compiler writes a constructor known as default constructor.6. Default Constructor doesn’t have any arguments.7. A Constructor which is defined with arguments is known as parameterizedConstructor.8. If you have a parameterized constructor, the compiler doesnt invoke defaultconstructor, user has to explicitly write a default constructor.

/*** When we create an obj/instance , the constructor will be invoked.**/Inheritance(Reusability of members)1. Inheriting a member of one class to another class is called as inheritance.2. The class from where memebers are inherited are known as Super class/Base Class/Parent class. Ex:Animal3. The class to which members are inherited are known as subClass/Derived Class/Child Class.Ex: Cat4. A subclass can inherit a super class by using 'extends' keyword. Syntax: Class SubClass extends SuperClass { //Class body }5. The inheritance always happens from super class to subclass.6. Class can extends only one class at a time. It cannot extends more than one class. Multiple Inheritance is not possible.Abstract Class:1. A method developed with signature & body is known as Concrete Methods.2. Any method developed without body is known as abstract method.

3. The abstract method should declare with a keyword called abstract.4. If a class contains atleast one abstract method then the class should bedeclared as abstract.5. Inside an abstract class we can develop both concrete and abstract methods.6. If a class is declared as abstract it’s not mandatory to develop abstract method.7. We cannot create an instance of abstract class hence we cannot access non-static members of abstract class.8. The static members of an abstract class can be referred using class name.9. We can develop empty abstract class.10. A subclass inheriting an abstract class should override all the abstract methodsof the abstract class.INTERFACES1. An interface is one of the definition block which is 100% abstract.2. All interface methods are by default abstract and public.3. An interface method need not be declared as abstract by default all methodsare abstract by default.4. An interface methods cannot be declared as final.5. interface variables are to be intialized at the time of declaration.6. Subclass inheriting an interface should implement all the abstract methods ofinterface.7. 'implements' is a keyword useful for impelementing the interfacees in thesubclass.8. Variables in interfaces are public static final by default.9. Right-click on package explorer and select new Interface to create an interface. WebDriver is an Interface-> Contains 100% Abstract methods

FirefoxDriver is a class which is implementing the WebDriver Interface. This we can written as WebDriver driver = new FirefoxDriver(); driver.get(\"\");PolymorphismPoly means many -> Polymorphism existence in many formsThey are two types of polymorphism1. Compile time is called as Method Overloading- If two or more methods whichare having the same name but different parameters which are available in thesame class. Then those methods i call as overloaded methods.The compile Timepolymorphism is called as static binding. And once it is binded it cannot bechanged throught the program. Thats the reason we can call this as staticpolymorphism.2. Run Time polymorphism is called as Method overriding-If two methods whichare having same arguments and they are available in super class and Subclassthen those methods are called as overridden methods. This is called dynamicBinding.Method Overloading: 1. Developing same methods() with different signature in a class is known as Method Overloading. 2. The signature should differ either in a. Argument type b. No of arguments c. Position of Arguments. 3. Both Static and Non-Static members of the class can be overloaded in the class. 4. The JVM executes the method based on argument value.

Method Overriding: 1. Inheriting the behavior of super class and changing the behavior in sub class is known as overriding. 2. The method signature should be same as SuperClass. 3. We cannot override the Static methods of the super class because it will not inherited to sub class.String Class1. String class is used to handle string values in a program and it is available in thepackage ‘java.lang’.2. String class is final and it cannot be inherited in any subclass.3. String objects can be created in two ways String str = new String(“abc”); String str=”abc”;4. Strings are immutable/constants.5. if(s2.equals(s3))//checking the contents of the string ex:sudheer { System.out.println(\"Two Object contents are equal\"); }6. if(s2==s3)//If the two objects are stored in different address locations the // two objects are not equal. { System.out.println(\"Two Objects are equal\"); }

‘final’ Variables, Methods and Classfinal variables are constants//final int i=10;final class cannot be inherited-> String,StringBuilder,StringBuffer,Systemfinal methods cannot be overriden.Differences between String,StingBuffer & StringBuilder1. String,StringBuffer and StringBuilder are final classes available in the packagejava.lang2. String is immutable StringBuffer & StringBuilder are mutable.3. String & StringBuilder are not thread safe. StringBuffer is threadSafe(means safely access between multiple threads)Exceptions:1. Compile Time Exception (checked Exception)IO Operations- Read and write the fileDatabase Operations-read and write the databaseSocket Programming-read and write to a socketIf you keep the code in try/catch block if you get an exception the executioncontinuestry{ //try block}catch(Exception Name e){

//catch block}If you use ‘throws IOException’ if you come across an issue the execution of theprogram is aborted.2. Run Time Exception(unchecked Exception)These are all programmatically flaws means the programmer is doing mistakes.java.lang.ArithmeticException: / by zerojava.lang.ArrayIndexOutOfBoundsException: 12They are only run time exceptions in seleniumEx: NoSuchElementException driver.findElement(By.id(\"xyz\"));Exception Flow Charthttp://3.bp.blogspot.com/-P8rJT34jDrY/UXAhPsohmtI/AAAAAAAADEU/nTtOrPMh7bs/s1600/Exception.pngObject ClassObject is the super class for all the classesThrowable is the super class for all the Errors and Exceptions.For all the runtime exceptions RunTimeException is the super class.For the compile Time Exception Exception is the super class.Errors are thrown at JVM level example:OutOfMemoryError.System Class:1. System is a class available in the package java.lang.2. java.lang package is iported to every java class by default.Hence

no need to import explicitly.3. System.out.println(); System is a class,out is a static reference variable of type PrintStrea Classand println is a method available in the PrintStream Class. public class System { static int i ; static PrintStream out = new PrintStream(); } System.out.println();java.util.List 1. List – is type of collection. 2. In List Objects are stored in index based. 3. List allows duplicate Objects.java.util.Set 1. Set is a type of Collection. 2. In Set Objects are stored without index. 3. Set doesn’t allow duplicate elements.


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook