1 / 106

Java programming

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

emcneal
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)

  2. Contents • 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. 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.

  5. 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.

  6. Java Milestones

  7. Java Milestones

  8. Java Attributes • Familiar, Simple, Small • Compiled and Interpreted • Platform-Independent and Portable • Object-Oriented • Robust and Secure • Distributed • Multithreaded and Interactive • High Performance • Dynamic and Extensible

  9. 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

  10. Compiled Languages Programmer Object Code Executable Code Source Code Text Editor Compiler linker .c file .o file a.out file Notepad,emacs,vi gcc

  11. 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

  12. Architecture Neutral & Portable • Java Compiler - Java source code(.java file) to byte code (file with .class) • Bytecode- an intermediate form, closer to machine representation • A interpreter interprets the bytecode.

  13. Architecture Neutral & Portable • Porting the java system to any new platform any where any time. • The interpreter will figure out what the equivalent machine dependent code to run.

  14. 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

  15. How Does Java Compares to C++ and Other OO Languages

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

  17. 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

  18. Object Oriented Languages -A Comparison

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

  20. Simple java program cntd... Public: The keyword public is an access specifies 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. If static keyword is specified then it is invoked first and no object creation is done for method Void: The type modifier void states that the main method does not return any value. Main() it is method to

  21. 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.

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

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

  24. 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.

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

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

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

  28. Costants 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

  29. 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”.

  30. Backslash character constants

  31. Symboliuc 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.

  32. Variables Variable is an idenitfier that denotes a storage location used to store data value. • 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.

  33. 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.

  34. 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;

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

  36. Scope of variable • Instance 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

  37. Class variables are global to class. • Belongs to objects that class creates • Only one memory location is created for class variables. • Variables used and defined inside the method are local variables • Not available to outside the method definition.

  38. Eg :statement1; { int x=0; \\ available to all { int n=5; \\ only with in braces } Statment2; { int m=20; \\ local variable } }

  39. 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;

  40. Casting with no loss of information

  41. 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

  42. DT cntd…

  43. Integer types Java support 4 types of integers.

  44. Floating point Data types

  45. DT 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.

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

  47. 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); } }

  48. Output: Enter a Number: 123 entered number Num = 123

  49. Operators Arithmetic operators

  50. Relational operators: Compare two quantities and depend on their relation take certain decisions.

More Related