Publicidad
Publicidad

Más contenido relacionado

Publicidad
Publicidad

Introduction to java (revised)

  1. Hello Everyone! Welcome to TECHFORT’s Training
  2. Hello Everyone! JAVA Sujit Majety, Technical Head / Corporate Trainer
  3. Day 1 Agenda Introduction to Java
  4. • Introduction • Programming Paradigm • History • Features • Sample Program • Execution Steps • JVM • Data types • Objects and Classes • Variables • Flow Control Statements Java Introduction Day 1 : Agenda
  5. Introduction Lets start our exiting journey from now.
  6. Introduction •Java is one of the most powerful programming languages. •Java is cross-platform, object-oriented, network-based, reliable programming language. •The slogan of java is “write once and run anywhere”, this allowed java to gain enormous popularity. Its rapid ascension and wide acceptance can be traced to its design and programming features. •The name “JAVA” came from the name of the coffee seed. •JAVA released to market in different platforms.
  7. Java Platforms •A java platform or edition consists of a JRE with a specific set of libraries for a specific application environment. PLATFORM KEY CHARACTERISTICS J2SE Core Java platform designed for applications running on desktop PCs. J2EE Design, development, assembly, and deployment of business Applications. J2ME Design of small, embedded applications in consumer devices (such as mobile phones). Java Card Design of small Java applications that run on smart cards. JavaFX Design of Rich Internet Applications (RIAs).
  8. Programming Paradigm A way of conceptualizing what it means to perform computation
  9. Programming Paradigms Procedure Oriented • More emphasis on algorithms. • Programs divided into functions. • Most functions share data, less security to data. • Data move around the system between functions, any function can change the data. • Adding new code is very difficult. • Code cannot be re-used. • E.g. C, VB, Perl, Basic, Fortran Object Oriented •More emphasis on data. •Programs are divided into objects. •Functions operate on data are tied together in the data structure. •Data is secured; it is hidden to external functions, can be accessed by functions tied to it. •Adding new code is very easy. •Code can be re-used. •E.g. CPP, Java, VB.Net, C#.Net
  10. History Where did this start?
  11. History •Developed by a team led by James Gosling at Sun Microsystems. •Sun Microsystems was a company best known for its workstations. •Java was originally OAK, designed for use in embedded consumer electronic application in 1991. •OAK was redesigned for developing internet applications and renamed JAVA in 1995. •Java is inherently object-oriented.
  12. History Major Release Date Key Characteristics JDK 1.0 1996 First stable version of Java JDK 1.1 1997 Inner classes; Java beans; JDBC; RMI; Just in Time (JIT) compiler for Windows platforms JDK 1.2 1998 Swing classes; Java IDL; Collections JDK 1.3 2000 Java platform debugger architecture (JPDA); JavaSound; HotSpot JVM JDK 1.4 2002 Regular expressions; IPv6 support; image I/O API; non-blocking I/O (nio); XML parser and XSLT processor JDK 5.0 2004 Generics; annotations; autoboxing; enumerations; varargs; for each loop JAVA SE 6 2006 Improved GUI support; improved web service support JAVA SE 7 2011 New file I/O capabilities; support for new network protocols JAVA SE 8 2014 Lambda expressions; new date and time API JAVA SE 9 2016 (expected) Money and currency API
  13. Features
  14. Features There are eleven promising features in java • Simple (no pointers, interfaces, rich set of API, friendly syntaxes) • Secure (many number of security mechanisms to block stray programs) • Portable (can run on any operating system) • Robust (memory management, exceptional handling) • Distributed (runs on multiple servers and can be accessed by no of clients) • Interpreted (both compiled and interpreted) • Multi Threading (multiple flow of controls) • Dynamic (is always open for updating) • Architecture Neutral (can run on any processor) • High Performance (garbage collector, leaves resources wile waiting for inputs) • Object oriented (everything is written in class)
  15. Our First Java Program
  16. First Java Program public class Test { public static void main(String[] args) { System.out.println("HELLO WORLD"); } } javac Test.java Java Test HELLO WORLD
  17. Execution Steps
  18. Execution Steps Text editor Java Virtual Machine Java Compiler Source Code (.java) Byte code (.class) Output on Console Saves Java statements Produces Result in
  19. JVM
  20. JVM •JVM stands for Java Virtual Machine. •It is abstract machine. •It is a specification that provides runtime environment in which java byte code can be executed. •JVM’s are available for many hardware and software platforms (i.e. JVM is platform dependant). •JVM performs four main tasks, Loads code Verifies code Executes code Provides runtime environment.
  21. JVM Class file Class Loader Subsystem Runtime Data areas Method area Heap Stack PC Registers Native Method area Execution Engine JVM JIT
  22. Data Types
  23. Data Types • Java supports UNICODE character set. Hence, each character is represented in two bytes. • In java every variable has a type, every expression has a type and all assignments should be checked by the compiler for type compatibility. Hence, java is treated as strongly typed language. • We are having 8 primitive data types. • These data types fall under 3 categories. • Numeric Data types • Character Data types • Boolean Data types
  24. Data Types DATA TYPE SIZE RANGE DEFAULT VALUE byte 8 bit -128 to 127 0 short 16 bit -32768 to 32767 0 int 32 bit -2,147,483,648 to 2,147,483, 647 0 long 64 bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L float 32 bit approximately ±3.40282347E+38F 0.0f double 64 bit approximately ±1.79769313486231570E+308 0.0d char 16 bit 0 to 65,536 (unsigned) ‘u0000’ boolean not precisely defined true or false false
  25. Object Oriented Programming
  26. OOP • Object oriented programming models the real world. • Object oriented programming organizes a program around its data and set of well defined interfaces to that data. • An object-oriented program can be characterized as data controlling access to code. • By switching the controlling entity to data, you can achieve several organizational benefits. • Object oriented programming, the first letter indicates object. • What is an object?
  27. Object •An object is the basic unit of object orientation with behaviour and identity. •An object is a real time entity. E.g.. Car, bike, book, me and you. •It is a runtime entity that has a state and behaviour. •The state of the object represents the data. •The behaviour is represented by the methods. •E.g. An object is created to represent a rabbit. It would have data: how hungry it is, where it is. And methods : eat, hide, run and dig.
  28. Class • A class is a way of binding the data and associated methods into a single unit. • A class can also be said as a blue print to create an object. • If we want to develop a java program, then that should be developed with respective of class only. i.e. without class there is no java program. • Syntax: class <class_name> { Variable declaration; Methods definition; }; Class Name Data members/ properties/ Attributes Behaviours/ Methods Animal Name, Number of legs, Colour Eat(), Walk(), Sleep()
  29. Example class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }
  30. Variables
  31. Variables •A variable is a name given to a memory location. •Generally there are three types of variables. •Variable Types Local Variables, are the variables that are declared inside a method Instance Variables, are the variables that are declared outside the methods Static Variables, are the variables that are declared outside the methods with static keyword.
  32. Flow Control Statements
  33. Flow Control Statements •A statement is an instruction that executes at runtime. •Types of Statements 1. Sequential Statements 2. Conditional Statements 3. Iterative / Looping Statements 4. Branching Statements
  34. Sequential Statements
  35. Sequential Statements •These statements execute sequentially without interruption one after the other. •All the statements we have executed so far come under sequential statements.
  36. Conditional Statements
  37. Conditional Statements •These statements execute based on a condition’s success or failure. •Types : 1. If 2. If-Else 3. Nested If-Else 4. IF-Else Ladder
  38. If Condition
  39. If Condition •A simple if condition checks for the condition and will decide to process the statements or not. •Statement 1 and Statement 2 are conditional statements as they depend on the condition for executing. Statement 1; Statement 2;
  40. If Syntax if(condition){ statement 1; statement 2; statement 3; }
  41. If-Else Condition
  42. If-Else Condition •This construct consists of two statements. (if and else). •If the condition is success some statements will get executed, if failure other statements will get executed. Statement 1; Statement 2; Statement 1; Statement 2;
  43. If-Else Syntax if(condition){ statement 1; statement 2; } else{ statement 3; statement 4; }
  44. Nested If-Else
  45. Nested If-Else Condition •If else construct within either the body of if statement or the body of an else statement or both. This scenario is said to be Nested If-else
  46. Nested If-Else Syntax if(condition){ statement 1; } else(condition2){ if(condition){ statement 2; } else{ statement 3; } }
  47. If-Else Ladder
  48. If-Else Ladder •If-Else ladder is used to check multiple conditions. •Here, every else is associated with it’s previous if. •The last else will go to work only if all the conditions fail. •Even in else if ladder the last else is optional.
  49. If-Else Ladder Syntax if(condition){ statement 1; } else if(condition){ statement 3; } else{ statement 4; }
  50. Switch-Case Statement
  51. Switch-Case Statement •Java has a built-in multiple- branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants •When a match is found the statements associated with that constant are executed.
  52. Switch-Case Syntax switch(expression){ case constant1 : Statement1; break; case constant2 : Statement2; break; case constant3 : Statement3; break; default : Statement4; }
  53. Iterative Statements
  54. Iterative Statements •Some times there will be a need to repeat the execution of certain statements for certain number of times. •This can be achieved using Loops. 1. While 2. Do-While 3. For
  55. While Loop
  56. While loop •A while loop has a loop condition only that is tested before each iteration to decide whether to continue or terminate the loop. •Hence while loop is said to be entry controlled loop.
  57. While loop Syntax while(condition){ Statement1; Statement2; Statement3; --- --- --- }
  58. Do-While Loop
  59. Do-While loop •A do-while loop has a loop condition only that is tested after each iteration to decide whether to continue or terminate the loop. •Hence, do-while loop is said to be exit controlled loop. •Even the condition fails every time the contents will be executed at least once.
  60. Do-While Syntax do{ Statement1; Statement2; --- --- --- }while(condition);
  61. For Loop
  62. For loop • A For loop contains 3 parts. • Initializer i.e. executed once at the start of the loop. • Loop Condition i.e. tested before iteration to decide whether to continue or terminate the loop. • Increment/Decrement i.e. executed at the end of each loop iteration. • Hence, for loop is said to be entry controlled loop.
  63. For Loop Syntax for(initializer; condition; increment/decrement){ Statement1; Statement2; --- --- --- --- }
  64. Foreach Loop
  65. Foreach Loop •This is an advanced for loop, used to traverse array or collection elements. •The advantage of this for loop over traditional is it eliminates the possibility of bugs and makes the code more readable. •Syntax: for( data_type variable : array|collection){} •The variable will hold on to single value for each iteration and stores the next value for the next iteration.
  66. Jumping Control Statements
  67. Jumping Control Statements • Java provides support to some more control statements. • These statements are used to move the control from one place of a program to another place. • These instructions are implemented by using the following statements. 1. Break 2. Continue 3. Go To
  68. Break • This keyword is used to stop the loop and go to the statement following he statement. • Break can be written with or without condition. • Syntax: break;
  69. Continue • Continue is a keyword used to skip iterations. • When continue is encountered in a loop the control stops the current iteration i.e. it will not execute the statements following it. • It can also be used with or without condition. • Syntax: continue;
  70. Go TO • Goto is a specific control statement. • It is used to jump the control from one part of the program to other part of the program. • It can be used for either as forward or as backward. • When forward goto is used the statements between the goto statement and label statement will be skipped. • When backward goto is used the statements between the goto and label statements will be repeatedly executed infinitely if not stopped with a condition.
  71. Important Interview Questions • What is the most widely used protocol on internet • What is the difference between an executable file and .class file? • Why java is suitable for internet? • Why pointers are eliminated in java? • What is the difference between a function and a method? • Which part of the JVM will allocate the memory for java program? • Which algorithm is used by garbage collector to remove the unused variables or objects from memory?
  72. Assignments • Check the hello world program by removing each and every word. • Shuffle the order of public static void main • Try experimenting with print & println. • Try giving different names for args. • Try giving int args[] instead of String args[] in main. • Give Different name for the filename and the class name • Repeat the above problem with declaring class as public • Copy the class file into different folder name and execute it. • Write your own program, compile it and execute it. • Try giving different data types ( the one you know like int x, double d ) in the println and see if it prints all of them. • I have two Integers int x =1; and int x=2; I wish to print 12. find the ways to print. • Repeat the same if I want to print given int x=1; double d=2.5; and print 12 instead of 12.5
  73. Assignments • Create new classes for each real-world object that you observed at the beginning of this trail. Refer to the Bicycle class if you forget the required syntax. • Create a small program that defines some fields. Try creating some illegal field names and see what kind of error the compiler produces. Use the naming rules and conventions as a guide. • In the program you created in Exercise 1, try leaving the fields uninitialized and print out their values. Try the same with a local variable and see what kind of compiler errors you can produce. Becoming familiar with common compiler errors will make it easier to recognize bugs in your code.
  74. Thank You! Sujit Majety, Technical Head/Software Trainer
  75. SUJIT MAJETY Technical Head / Corporate Trainer Techfort Software Solutions PVT. LTD. Mail To : sujit.majety@gmail.com Call To : 040-40062299 View Link : http://tinyurl.com/techfort-java1 Feedback : http://tinyurl.com/mrcewfeedback
Publicidad