1 / 105

Java programming

Java programming. Prof.Rohini S Hanchate Pimpri Chinchawad polytechnic (PCP). Visit for more Learning Resources. Contents. History of java. Why java???? Java Introduction Java Features How Java Differs from other Object Oriented languages. Why java????. It’s entirely object-oriented

calebb
Download Presentation

Java programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java programming Prof.Rohini S Hanchate Pimpri Chinchawad polytechnic (PCP) Visit for more Learning Resources

  2. Contents • History of java. • Why java???? • Java Introduction • Java Features • How Java Differs from other Object Oriented languages

  3. Why java???? • It’s entirely object-oriented • It has a vast library of predefined objects and operations • It’s more platform independent • this makes it great for Web programming • It’s more secure • It isn’t C++

  4. Why java???? • C : structured language, complexity of program is reduced but fails to achieve bug-free, easy maintain & re-usability • OOP: • Object and classes • Data abstraction & encapsulation • Inheritance • Polymorphism • Dynamic binding • message communication

  5. Java - An Introduction • Java - The new programming language developed by Sun Microsystems in 1991. • Originally called Oak by James Gosling, one of the inventors of the Java Language. • Java Authors: James , Arthur Van , and others • Java is purely object oriented.

  6. Java Introduction • Originally created for consumer electronics (TV, VCR, Freeze, Washing Machine, Mobile Phone). • Java - Machine Independent language • Java is language of Internet Programming. • It allows you to publish a webpage with Java code in it.

  7. Java Milestones

  8. Java Milestones

  9. Java Creation • Trouble with C & C++ they are designed to compile specific target or code. • The compiler are to expensive & time consuming. • To create an easier & more cost effective solution to achieve platform independent language that could be used to create software which can be embedded in various electronic devices eg: oven remote controls etc. • Dynamic and Extensible

  10. Java features • Familiar, Simple, Small • Compiled and Interpreted • Platform-Independent and Portable • Object-Oriented • Robust and Secure • Distributed • Multi-threaded and Interactive • High Performance • Dynamic and Extensible • Easy of development • Scalability and performance

  11. Familiar,Simple,Small • Familiar, Simple, Small: uses concept of c & C++. • Very simple to understand & to write programs. • Small : uses numerous methods to solve the delinquent.

  12. Compiled Languages (Eg C,C++) Programmer Object Code Executable Code Source Code Text Editor Compiler linker .c file .o file a.out file Notepad,emacs,vi gcc

  13. Java is Compiled and Interpreted Programmer Hardware and Operating System Source Code Byte Code Text Editor Compiler Interpreter .java file .class file java appletviewer netscape Notepad,emacs,vi javac

  14. Architecture Neutral & Portable • Java Compiler - Java source code(.java file) to byte code (file with .class) • Bytecode- an intermediate form, closer to machine representation contains set of instructions which is executed by Java runtime System(JVM) • A interpreter interprets the bytecode. • Porting the java system to any new platform any where any time we need only JVM needs to be implemented for each platform. • The interpreter will figure out what the equivalent machine dependent code to run.

  15. Total Platform Independence JAVA COMPILER (translator) JAVA BYTE CODE (same for all platforms) JAVA INTERPRETER (one for each different system) Windows 95 Macintosh Solaris Windows NT

  16. Security & Robust • Using Java compatible web browser we can safely download Java programs as it provides firewall between network based application and System. • Java is strictly typed language it checks code at compile time & run time. • All the run time errors can be handled by programs. • Robust: all memory management through garbage collection.

  17. High performance & Distributed • Byte code interpreted on any system that provide JVM • Perform well on low powered CPU's .byte code is designed to translate directly into native m/c code. • To achieve high performance using JIT compiler. • Distributed: it support TCP/IP protocol ,accessing resource using URL ,support RMI. • Allow multiple programmers at multiple remote locations

  18. Rich Class Environment • Core Classes • language • Utilities • Input/Output • Low-Level Networking • Graphical User Interface • Internet Classes • TCP/IP Networking • WWW and HTML • Distributed Programs

  19. Overlap of C, C++, and Java C++ C Java

  20. Java better than C++ ? • No Typedefs, Defines, or Preprocessor • No Global Variables • No Goto statements • No Pointers • No Unsafe Structures • No Multiple Inheritance • No Operator Overloading

  21. Object Oriented Languages -A Comparison

  22. Simple java program Class first_program { Public static void main(String args[]) { System.out.println(“hellow world”); } }

  23. Simple java program cntd... Public: The keyword public is an access specifiers that declares the main method as unprotected. Static: Which declares main method as one that belongs to the entire class ,The main must always declare as static since the interpreter uses this method before any objects are created. Void: The type modifier void states that the main method does not return any value.

  24. JAVA Tokens • Smallest individual units in a program are known as Tokens. • Five different types of tokens: • Reserved Keywords. (60 Keywords, false, null, true, package etc.,). • Identifiers (alphabets, digits and underscore and dollar sign characters.) • Literals. (Integer, Floating Point. Character, String, Boolean) • Operators. • Separators.

  25. Tokens cntd.... (Keywords) • Java language is having reserved 50 word as keywords.

  26. Tokens cntd.... (identifiers) These are programer designed tokens. • Classes. • Methods • Variables. • Objects • Labels • Packages • Interfaces

  27. Identfiers cntd... Rules for identifiers: • Alphabets,digits,underscore(_)and $ are allowed. • They must not begin with digit. • Uppercase and lowercase letters are distinct. • Identifier can be of any length. • Identfiers must be meaningful. • Short, easy, quick. • Easily readable and discriptive. Eg: average, dayTemerature,first_name,TOTAL.

  28. Literals Literals are sequence of characters. 5 types of literals • Integer literals • Floating point literals • Charester literals • String literals • Boolean literals

  29. Operators An operator is symbol that takes one or more arguments and operates on them to produce results.

  30. Separators Symbols used to indicate where groups of code are divided and arranged.

  31. Constants Constant in java refered to fixed values that do not change during execution of a program. • Numeric constants • Integer constants • Real constants • Charecter constants • Charechter constants • String constants

  32. Backslash character constants

  33. Constants cntd.... Integer constants: it refer as sequence of digits • Decimal integer constant consist of set of digit from 0 to 9 eg. 123 • Octal integer constants consist of from 0 to 7 eg. 037,0435 Real constants: numbers contain fractional parts refered to real constants eg 0.67878 Character constants: single character enclosed within a pair of single quotes eg. '4','a'. String constants: a sequence of charater encloused with in double qoutes eg. “hello”,”1998”.

  34. Symbolic constants • Symbolic names take the same form as variable names. • SC's are written in CAPITALS to distinguish them from normal variable names. • they should not be assigned any other value within the program after declaration of symbolic constants by using an assignment statement. eg. final int PEOPLE = 100; //we can not change the value. • This is not done in C and C++ where it is defined using the #define statement. • Can not be declared inside a method.

  35. Data types • Every variable is having Data type • Data type specify size and type of values that can be stored. • Depend on need of application select appropriate data type Category of Data Types • Primitive type • Derived type

  36. DT cntd…

  37. Integer types Java support 4 types of integers.

  38. Floating point Data types

  39. Data Type cntd.. Character type • To store character constants in memory. • Size is 2 bytes Boolean type • To test particular condition we use Boolean type. • Only two values it takes True & False. • All comparison operators return Boolean type.

  40. Standard default values • Every variable has default values.

  41. Variables Variable is an identifier that denotes a storage location used to store data value. Rules for defining variable • Variable may take different value at different times during execution of the program • Variable name should be Meaningful . • Variable must not begin with a digit. • Uppercase and lower case letters are distinct. • It should not be keyword. • White spaces is not allowed • Variable name can be anything.

  42. Declaration of variables • Variable are names of storage locations. • After designing suitable variable names we must declare them to the compiler Why declaration????? • It tells the variable name to compiler • Type of data that variable holding • Place of variable decides scope of the variables • Variable must be declared before it used in program • Variable used to store value of any data type.

  43. Declaration of variable cntd… Typevariable1,variable2,variable3…….; • Variables are separated by comma’s (,) • Declaration statement should end with ; . Eg , int count; float area; double p1; char c1,c2,c3;

  44. Assigning values to variables • Using assignment statement Variable_Name = value; Eg intialValue=0; finalValue=100; • String assignment eg x=y=z=0; • Assign values at declaration time Eg int finalValue=100; Char yes = ‘x’;

  45. Scope of variable • Instance or object variable • Class variable • Local variable • Instance and class variables are declared inside a class. • Instance variables are created when the object are initiated and those variables are associated with the object. • Instance variable take different values for each object • Class variables are global to class. • Belongs to objects that class creates • Only one memory location is created for class variables.

  46. Local variable: Variables used and defined inside the method are local variables • Not available to outside the method definition.

  47. Example for variables Eg : class myprg { Int z=10; class variable method1; { int x=0; \\ local variable int n=5; \\ only with in braces } Public static void main(String args[]) { First f=new First();\\ instance variable } }

  48. Type casting • Want store variable of one type in into a variable of other type. Type variable1 = (type) variable2; • It is converting one data type to another is known as casting. Eg int m= 12; byte n =(byte) m; Long count = (long) m;

  49. Casting with no loss of information

  50. Reading data from Keyboard Import java.lang.*; Import java.io.DataInputStream; Class read { public static void main (String args []) { DataInputStream in = new DataInputStream (System. in); int num = 0; System.out.println (“Enter a number :”); num = Integer.parseInt (in.readLine () ); System.out.println (“entered number Num = “ +num); } }

More Related