48
Copyrights© 2008 BVU Amplify DITM Core Java Core Java Page:1 CORE-JAVA By Simi By Simi

Cr java concept by vikas jagtap

Embed Size (px)

Citation preview

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:1

CORE-JAVA

By SimiBy Simi

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:2

First, a bit of background…

11stst Generation – machine language Generation – machine language (raw machine code – lots of binary 0100101010001000111)

22ndnd Generation – assembly language Generation – assembly language (mnemonic representation – short series of chars to represent binary)

33rdrd Generation – structured programming Generation – structured programming (e.g. Pascal, C, C++, Java)

44thth Generation – application specific Generation – application specific (SQL, Mathematica, RPG II, PostScript)

55thth Generation – combining artificial intelligence Generation – combining artificial intelligence (best not mentioned…)

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:3

Towards Structured Programming

As computer programs become larger As computer programs become larger and more complex, more errors are and more complex, more errors are introduced.introduced.

It has been estimated that there are 15 It has been estimated that there are 15 bugs in every 1000 lines of commercial bugs in every 1000 lines of commercial code.code.

Windows 2000 had 40 million lines of Windows 2000 had 40 million lines of code!code!

Most bugs are caused by poor memory Most bugs are caused by poor memory management.management.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:4

Contd:

Clearly there is a need for a structured Clearly there is a need for a structured programming language that helps in programming language that helps in reducing the number of errors and reducing the number of errors and speeds development for programming speeds development for programming teams.teams.

C C →→ C++ C++ →→ Java Java

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:5

Contd:

Functional Programming Functional Programming →→ Object Object OrientationOrientation

“ “C makes it easy to shoot yourself in the C makes it easy to shoot yourself in the foot; C++ makes it harder, but when it foot; C++ makes it harder, but when it happens you tend to take off the whole happens you tend to take off the whole leg!”leg!”

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:6

What is Object-Oriented code?

Procedural coding is just a list of instructions.Procedural coding is just a list of instructions. Object-oriented code has a lot of similarity with Object-oriented code has a lot of similarity with

code from other procedural languages.code from other procedural languages. Basically, a lot of the ‘words’ are the same but the Basically, a lot of the ‘words’ are the same but the

way the words are put together (the structure) is way the words are put together (the structure) is different.different.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:7

Contd:

Object-oriented coding does also contain lists of Object-oriented coding does also contain lists of instructions but these lists are bound to specific instructions but these lists are bound to specific objects.objects.

What an object actually represents is up to the What an object actually represents is up to the programmer.programmer.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:8

Simple Example

AA car is an object (in the real world) and in our car is an object (in the real world) and in our program we can make a software object to program we can make a software object to represent it.represent it.

Fundamentally, an object contains data and Fundamentally, an object contains data and methods which can act on that data.methods which can act on that data.

The data we might want to have in our ‘software’ The data we might want to have in our ‘software’ car could be things like: body colour, engine size, car could be things like: body colour, engine size, current speed, whether it has electric windows etc. current speed, whether it has electric windows etc. Basically it’s up to you.Basically it’s up to you.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:9

Simple Example

The methods we might want our ‘car’ to have The methods we might want our ‘car’ to have could be things like: accelerate, brake, respray could be things like: accelerate, brake, respray body, open passenger window etc. Again, it’s up to body, open passenger window etc. Again, it’s up to you.you.

Whatever you need your software car to model Whatever you need your software car to model from the real world object must be present in your from the real world object must be present in your code.code.

The OO syntax enables you to do this intuitively.The OO syntax enables you to do this intuitively.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:10

What technology does it replace?

Procedural programming (C, Visual Basic, Fortran Procedural programming (C, Visual Basic, Fortran etc.)etc.)

In procedural programming, functions were the In procedural programming, functions were the most important part of the software.most important part of the software.

Where and how the data was stored was secondary Where and how the data was stored was secondary (at best).(at best).

Procedural code is process-oriented, OO code is Procedural code is process-oriented, OO code is data-oriented.data-oriented.

In procedural programming, if your code is in error In procedural programming, if your code is in error then it is relatively easy to fix but incorrect data then it is relatively easy to fix but incorrect data may be may be impossibleimpossible to fix! to fix!

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:11

Java (why use it?)

Advantages over C & C++Advantages over C & C++

WORA - Write Once, Run Anywhere (portable).WORA - Write Once, Run Anywhere (portable). Security (can run “untrusted” code safely).Security (can run “untrusted” code safely). Robust memory management (opaque references, Robust memory management (opaque references,

automatic garbage collection)automatic garbage collection) Network-centric programming.Network-centric programming.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:12

Java (why use it?)

Multi-threaded (multiple simultaneous tasks).Multi-threaded (multiple simultaneous tasks). Dynamic & extensible.Dynamic & extensible.

Classes stored in separate files Loaded only when needed Can dynamically extend itself to expand its

functionality (even over a network and the Internet!)

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:13

How does it help?

Large projects can be broken down into modules Large projects can be broken down into modules more easily.more easily.

Aids understanding.Aids understanding. Groupwork is easier.Groupwork is easier. Less chance of data corruption.Less chance of data corruption. Aids reusability/extensibility.Aids reusability/extensibility. Maintaining code is far easier.Maintaining code is far easier. Hides implementation details (just need to know Hides implementation details (just need to know

what methods to call but no need to understand what methods to call but no need to understand how the methods work to use them).how the methods work to use them).

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:14

What is Java?

Java (with a capital J) is a high-level, third generation programming language, like C, Fortran, Smalltalk, Perl, and many others.

You can use Java to write computer applications that crunch numbers, process words, play games, store data or do any of the thousands of other things computer software can do.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:15

Contd:

Compared to other programming languages, Java is most similar to C. However although Java shares much of C's syntax, it is not C. Knowing how to program in C or, better yet, C++, will certainly help you to learn Java morequickly, but you don't need to know C to learn Java.

Unlike C++ Java is not a superset of C.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:16

Contd:

A Java compiler won't compile C code, and most large C programs need to be changed substantially before they can become Java programs.

What's most special about Java in relation to other programming languages is that it lets you write special programs called applets that can be downloaded from the Internet and played safely within a web browser.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:17

Applets

embed into web page using applet tagembed into web page using applet tag <APPLET src = "myprogram.class" width =

"100" height = "100"> < /APPLET> source code extends Java Applet classsource code extends Java Applet class has skeleton user interfacehas skeleton user interface programmer can add code to be executed programmer can add code to be executed

when applet is initialised and paintedwhen applet is initialised and painted programmer can easily add GUI components programmer can easily add GUI components

such as buttons, labels, textfields, scrollbars…. such as buttons, labels, textfields, scrollbars…. and respond to their eventsand respond to their events

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:18

Java applications

stand-alone programsstand-alone programs not GUI by defaultnot GUI by default

text input and output to a console can add user interface components

execution always starts at a execution always starts at a main()main() methodmethod

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:19

Applets vs. applications

Java appletsJava applets a program embedded into a web page download and run on user's browser

(or applet viewer) internet programming

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:20

Applets vs. Applications

Java applicationsJava applications stand-alone programs – Java is a

fully-fledged programming language many Java class libraries for

GUI, graphics, networking data structures database connectivity

we will be writing applications in this we will be writing applications in this module.module.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:21

A Brief History

January 1996: first official release JDK 1.0

February 1997: JDK 1.1

May 2000: J2SDK v1.3

May 2001: J2SDK v1.4 beta

2002(?): J2SDK v1.5

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:22

Java is a Platform

Java (with a capital J) is a platform for application development.

A platform is a loosely defined computer industry buzzword that typically means some combination of hardware and system software that will mostly run all the same software.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:23

Java is a Platform

Java solves the problem of platform-independence by using byte code. The Java compiler does not produce native executable code for a particular machine like a C compiler would. Instead it produces a special format called byte code. Java byte code written in hexadecimal, byte by byte, looks like this:

CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:24

This looks a lot like machine language, but unlike machine language Java byte code is exactly the same on every platform. This byte code fragment means the same thing on a Solaris workstation as it does on a Macintosh PowerBook.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:25

Java is a Platform

Java programs that have been compiled into byte code still need an interpreter to execute them on any given platform.

The interpreter reads the byte code and translates it into the native language of the host machine.

The most common such interpreter is Sun's program java (with a little j)

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:26

Java is a Platform

Since the byte code is completely platform independent, only the interpreter and a few native libraries need to be ported to get Java to run on a new computer or operating system.

The rest of the runtime environment including the compiler and most of the class libraries are written in Java. All these pieces, the javac compiler, the java interpreter, the Java programming language, and more are collectively referred to as Java.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:27

Java is Simple

Java is an excellent teaching language, and an excellent choice with which to learn programming.

The language is small so it's easy to become fluent. The language is interpreted so the compile-run-link cycle is much shorter.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:28

Java is Platform Independent

Java was designed to not only be cross-platform in source form like C, but also in compiled binary form.

Since this is frankly impossible across processor architectures Java is compiled to an intermediate form called byte-code.

A Java program never really executes natively on the host machine. Rather a special native program called the Java interpreter reads the byte code and executes the corresponding native machine instructions.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:29

Java is Platform Independent

Thus to port Java programs to a new platform all that is needed is to port the interpreter and some of the library routines.

Even the compiler is written in Java. The byte codes are precisely defined, and remain the same on all platforms.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:30

Java is Platform Independent

The second important part of making Java cross-platform is the elimination of undefined or architecture dependent constructs.

Integers are always four bytes long, and floating point variables follow the IEEE 754 standard for computer arithmetic exactly.

You don't have to worry that the meaning of an integer is going to change if you move from a Pentium to a PowerPC.

In Java everything is guaranteed.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:31

Java is High Performance

Java byte codes can be compiled on the fly to code that rivals C++ in speed using a "just-in-time compiler.“

Several companies are also working on native-machine-architecture compilers for Java.

These will produce executable code that does

not require a separate interpreter, and that is indistinguishable in speed from C++.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:32

Java is Multi-Threaded

Java is inherently multi-threaded. A single Java program can have many different threads executing independently and continuously.

Three Java applets on the same page can run together with each getting equal time from the CPU with very little extra effort on the part of the programmer.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:33

Java is Dynamic(ly linked)

Java does not have an explicit link phase. Java source code is divided into .java files, roughly one per each class in your program.

The compiler compiles these into .class files containing byte code. Each .java file generally produces exactly one .class file.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:34

Java is Dynamic(ly linked)

The compiler searches the current directory and directories specified in the CLASSPATH environment variable to find other classes explicitly referenced by name in each source code file. If the file you're compiling depends on other, non-compiled files the compiler will try to find them and compile them as well.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:35

Java is Garbage Collected

You do not need to explicitly allocate or deallocate memory in Java.

Memory is allocated as needed, both on the stack and the heap, and reclaimed by the garbage collector when it is no longer needed.

There's no malloc(), free(), or destructor

methods.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:36

Characteristics of Java

• Simple • Object-oriented • Compiled • Architecture neutral

• Garbage collected • Multi-threaded • Robust • Secure • Extensible

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:37

Packages

• Java provides mechanisms to organize large-scale programs in a logical and maintainable fashion.

– Class --- highly cohesive functionalities – File --- one class or more closely related

classes – Package --- a collection of related classes or

packages

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:38

A simple Java application

class HelloJava{public static void main (String args[]){System.out.println("Hello Java!");}}

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:39

Writing Java code

When you've chosen your text editor, type or copy the above program into a new file.

Like C and unlike Fortran, Java is case sensitive so System.out.println is not the same as system.out.println. CLASS is not the same as class, and so on.

there are three steps to creating a Java program: – writing the code – compiling the code – running the code

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:40

First Java Program

Under Windows, it's similar. You just do this in a DOS shell.C:> javac HelloWorld.javaC:> java HelloWorldHello World

C:>• Notice that you use the .java extension when compiling a file, but you do not use the .class extension when running a file.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:41

First Java Program

Under Windows (98 and before) you set the CLASSPATH environment variable with a DOS

command like C:\> SET

CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

For Newer Windows platforms (including Windows XP), type the following-

C:\> set path=C:\j2sdk1.4.0-rc\bin And press ENTER (where j2sdk1.4.0 is the folder in your root

(C:\>) drive, where Java2SDK is installed)

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:42

First Java Program

The CLASSPATH variable is also important when you run Java applets, not just when you compile them. It tells the web browser or applet viewer where it should look to find the referenced .class files.

If the CLASSPATH is set improperly, you'll probably see messages like "Applet could not start."

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:43

If the CLASSPATH environment variable has not been set, and you do not specify one on the command line, then Java sets the CLASSPATH to the default.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:44

The class and its method in JAVA name of class is same as name of file name of class is same as name of file

(which has (which has ..java extension)java extension) body of class surrounded by { }body of class surrounded by { } this class has one method called mainthis class has one method called main

all Java applications must have a main method in one of the classes

execution starts here body of method within { }

all other statements end with semicolon; all other statements end with semicolon;

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:45

Java keywords

keywords appear in boldkeywords appear in bold reserved by Java for predefined

purpose don’t use them for your own variable,

attribute or method names! publicpublic

visibility could be private

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:46

Java keywords

voidvoid method does not return a value

staticstatic the main method belongs to the Hello

class, and not an instance (object) of the class.

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:47

Comments

important for documentation!!!!important for documentation!!!! ignored by compilerignored by compiler

//// single line (or part of line)single line (or part of line)

/* multiple line comments go here/* multiple line comments go hereeverything between the marks everything between the marks is ignored */is ignored */

Copyrights© 2008 BVU Amplify DITM

Core JavaCore Java

Page:48

THANK YOUTHANK YOU