1 / 42

WORKSHOP

JAVA. WORKSHOP. Learning JAVA. Understanding Salient features of JAVA. Implementing JAVA PROGRAMS on your own. Development of JAVA PROJECTS on your own. Objectives. INDEX. About JAVA language. Program structures Simple program JVM OOP principles. keywords Data types Operations

Download Presentation

WORKSHOP

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 WORKSHOP

  2. Learning JAVA. Understanding Salient features of JAVA. Implementing JAVA PROGRAMS on your own. Development of JAVA PROJECTS on your own. Objectives

  3. INDEX • About JAVA language. • Program structures • Simple program • JVM • OOP principles. • keywords • Data types • Operations • Arrays

  4. Java History • Computer language innovation and development occurs for two fundamental reasons: 1) to adapt to changing environments and uses 2) to implement improvements in the art of programming • The development of Java was driven by both in equal measures. • Many Java features are inherited from the earlier languages: B  C  C++  Java L 1.1

  5. C language designed by Dennis Ritchie in 1970s. • C++ designed by BjarneStroustrupin 1979. • In 1990, Sun Microsystems started a project called Green. • Objective: to develop software for consumer electronics. • Project was assigned to James Gosling, a veteran of classic network software design. Others included Patrick Naughton, ChrisWarth, Ed Frank, and Mike Sheridan. • In 1994, an early web browser called WebRunner was written in Oak. WebRunner was later renamed HotJava. • In 1995, Oak was renamed Java.

  6. Java Buzzwords • Simple • Secure • Portable • Object-oriented • Robust • Multithreaded • Architecture-neutral • Interpreted • High performance • Distributed • Dynamic L 1.9

  7. Structure of C

  8. Structure of JAVA

  9. /*A program to add two numbers*/ • package mypack; • import java.lang.*; import java. util.*; • class Addition { inta,b,c; void sum(int a, int b) { c=a+b; System.out.println(“sum is +c”); } } • class Additionop { public static void main(String args[]) { Addition ad=new Additon(); ad.sum(3,5) } }

  10. JAVA WORLD class Welcome { public static void main(String args[]) { System.out.println(“WELCOME TO JAVA WORLD”); } }

  11. Naming conventions specify the rules to be followed by a Java programmer while writing the names of packages, classes, methods etc. • Package names are written in small letters. • ex: java.io, java.lang, java.awt etc • Each word of class name and interface name starts with a capital • ex: Sample, AddTwoNumbers • Method names start with small letters then each word start with a capital • ex: sum (), sumTwoNumbers (), minValue () • · Variable names also follow the same above method rule • ex: sum, count, totalCount • · Constants should be written using all capital letters • ex: PI, COUNT • · Keywords are reserved words and are written in small letters. • ex: int, short, float, public, void

  12. Compiling the Program • To compile the program, execute the compiler, javac, specifying the name of the source file on the command line. C:\>javac Welcome.java • To actually run the program, you must use the Java interpreter, called java. To do C:\>java Welcome • When the program is run, the following output is displayed: “WELCOME TO JAVA WORLD”.

  13. Java Virtual Machine(JVM) • Java compiler translate source code into machine code. • java compilers produces an intermediate code known as Byte code for a machine. • this machine is called Java Virtual Machine, and it exists only inside the computer memory. • the virtual machine code is not machine specific, the machine code is generated by Java interpreter.

  14. source code Byte code Process of compilation virtual machine real machine Process of converting byte code into machine code Java program Java compiler Virtual machine Byte code Java interpreter Machine code

  15. OOP Vs POP

  16. PRINCIPLES • Encapsulation: It is the mechanism that binds together code and the data it manipulates. info. in info. out Data &Method

  17. Inheritance: It is the process by which objects of one class acquire the properties of objects of another class. Bird attributes Flying birds Nonflying birds Robin Swallow Penguin Kiwi

  18. Polymorphism: It means the ability to take more than one form. Shape Draw() Circle object Draw(circle) Box object Draw(box) Triangle object Draw(triangle)

  19. CLASS • A class is a blueprint ,that defines the variables and methods common to all objects of a certain kind. • An object holds values for the variables defines in the class. • “Object” is an instance of a class. Each object has a class which defines its data and behavior. • An object is called an instance of the Class L 4.3

  20. Structure of Class • A class is declared by use of the class keyword. • class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list } // ... type methodnameN(parameter-list) { // body of method } }

  21. The Java Keywords

  22. Data Types • Java defines eight simple types: 1)byte – 8-bit integer type 2)short – 16-bit integer type 3)int – 32-bit integer type 4)long – 64-bit integer type 5)float – 32-bit floating-point type 6)double – 64-bit floating-point type 7)char – symbols in a character set 8)boolean – logical values true and false L 1.14

  23. Variables • Java uses variables to store data. • To allocate memory space for a variable JVM requires: 1) to specify the data type of the variable 2) to associate an identifier with the variable 3) the variable may be assigned an initial value • All done as part of variable declaration. L 2.2

  24. Variable Declaration • datatype identifier [=value]; • datatype must be • A simple datatype • User defined datatype (class type) • We can declare several variables at the same time: type identifier [=value][, identifier [=value] …]; Examples: int a, b, c; int d = 3, e, f = 5; byte g = 22; double pi = 3.14159; char ch = 'x'; L 2.4

  25. Variable Scope • Scope determines the visibility of program elements with respect to other program elements. • In Java, scope is defined separately for classes and methods: 1) variables defined by a class have a global scope 2) variables defined by a method have a local scope A scope is defined by a block: { … } A variable declared inside the scope is not visible outside: { int n; } n = 1;// this is illegal L 2.5

  26. Arrays • An array is a group of liked-typed variables referred to by a common • name, with individual variables accessed by their index. • Arrays are: 1) declared 2) created 3) initialized 4) used • Also, arrays can have one or several dimensions. L 2.7

  27. Array Declaration • Array declaration involves: 1) declaring an array identifier 2) declaring the number of dimensions 3) declaring the data type of the array elements • Two styles of array declaration: type array-variable[]; or type [] array-variable; L 2.8

  28. Array Creation • After declaration, no array actually exists. • In order to create an array, we use the new operator: type array-variable[]; array-variable = new type[size]; • This creates a new array to hold size elements of type type, which reference will be kept in the variable array-variable. L 2.9

  29. Array Initialization • Arrays can be initialized when they are declared: • intmonthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31}; • Note: 1) there is no need to use the new operator 2) the array is created large enough to hold all specified elements L 2.11

  30. Array Indexing • Later we can refer to the elements of this array through their indexes: • array-variable[index] • The array index always starts with zero! • The Java run-time system makes sure that all array indexes are in the correct range, otherwise raises a run-time error. L 2.10

  31. int[] abc, xyz; abc = new int[5];// Instantiate an array of 5 integers xyz = abc;// xyz and abc refer to the same array xyz[3] = 100;// Changing xyz changes abc as well. System.out.println (abc[3]);// 100 is displayed

  32. Multidimensional Arrays • Multidimensional arrays are arrays of arrays: 1) declaration: int array[][]; 2) creation: int array = new int[2][3]; 3) initialization int array[][] = { {1, 2, 3}, {4, 5, 6} }; Two-dimensional array L 2.12

  33. Average an array of values class Average { public static void main(String args[]) { double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; double result = 0; inti; for(i=0; i<5; i++) result = result + nums[i]; System.out.println("Average is " + result / 5); } }

  34. Two-dimensional array class TwoDArray { public static void main(String args[]) { inttwoD[][]= new int[4][5]; inti, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }

  35. Output • 0 1 2 3 4 • 5 6 7 8 9 • 10 11 12 13 14 • 15 16 17 18 19

  36. Operators Types • Java operators are used to build value expressions. • Java provides a rich set of operators: 1) assignment 2) arithmetic 3) relational 4) logical 5) bitwise L 2.13

  37. Arithmetic assignments L 2.14

  38. Basic Arithmetic Operators L 2.15

  39. Relational operator L 2.16

  40. Logical operators L 2.17

  41. Bitwise operators program • class Bits { public static void main(String args[]) { byte x,y; x=10; y=11; System.out.println("~x="+(~x)); -> -11 System.out.println("x&y="+(x&y)); -> 10 System.out.println("x/y="+(x/y)); -> 11 System.out.println("x^y="+(x^y)); -> 1 System.out.println("x<<2="+(x<<2)); -> 40 System.out.println("x>>2="+(x>>2)); -> 22 System.out.println("x>>>2="+(x>>2)); -> 22 } }

  42. presented by U. LOVARAJU Assoc. Professor IT-Department

More Related