introduction to java - devi ahilya vishwavidyalaya

23
Introduction to Java Lecture 1 School of Computer Science [email protected]

Upload: others

Post on 12-Sep-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java - Devi Ahilya Vishwavidyalaya

Introduction to Java

Lecture 1

School of Computer Science [email protected]

Page 2: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 2

Topics

• About JAVA

• Features of Java

• How it is different from C, C++

• Introduction to Java Virtual Machine

• Present the syntax of Java

• Data types

Example programs

Page 3: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 3

History of Java

• Java

– Originally for intelligent consumer-electronic devices

– Then used for creating Web pages with dynamic

content

– Now also used to:

• Develop large-scale enterprise applications

• Enhance WWW server functionality

• Provide applications for consumer devices (cell phones, etc.)

Page 4: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 4

Why Java?

• Because of its design goals , which are summarized in the form of several keywords:

– Java is simple

– It’s almost entirely object-oriented

– It’s distributed

– It’s more platform independent & architectural neutral• this makes it great for Web programming

– It’s more secure

– It’s robust

– It’s multithreaded

– It’s Portable

– It’s Interpreted

Page 5: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 5

Java Programs : Applets, Servlets

and Applications

• An applet is designed to be embedded in a Web

page and run by a browser

• Applets run in a sandbox with numerous

restrictions; for example, they can’t read files

• A servlet is designed to be run by a web server

• An application is a conventional program

Page 6: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 6

How it is different from C++

No explicit boolean data type.Boolean data type primitive.

8-bit ASCII characters.16-bit Unicode characters.

Primitive type size varies by platform.All primitive types are a fixed size for all

platforms.

Signed and unsigned numeric types.All numeric types are signed.

Separate types for structs, unions, enums, and

arrays.All nonprimitive types are objects.

Virtual functions are dynamically bound.All methods (except final methods) are

dynamically bound.

Operator overloading.No operator overloading.

Multiple inheritance.No multiple inheritance.

Can have stand-along functions.All functions (methods) are part of a class.

Hybrid between procedural and object-oriented.Pure object-oriented language.

Global variables.No global variables.

gotoNo goto

Heavy reliance on preprocessor.No preprocessor.

Page 7: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 7

How it is different from C++

No multithreading.Supports multithreading.

Architecture specific.Architecture neutral.

Fast execution.Slower execution when interpreted.

Compiled.Combination of compiled and interpreted.

No automatic garbage collection.Automatic garbage collection.

No relationship to networks or the Web.Specifically attuned to network and Web

processing.

typedef to define types.No typedef.

String concatenation through a library function.Built-in string concatenation operator(+).

Strings are null-terminated character arrays.Strings are objects.

Array references translate to pointer arithmetic.Array references are not translated to pointer

arithmetic.

Pointers, with dereferencing (* or ->) and

address (&) operators.

References, with no explicit pointer

manipulation and no pointer arithmetic.

No automatic initialization of variables.Variables are automatically initialized.

Page 8: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 8

Java Compilers & Tools

• Sun Microsystems' Java Development Kit (JDK) (Java 2 Platform Second Edition, J2SE)– for a number of platforms (Windows, Linux, Solaris SPARC,

Solaris x86) from the creator of Java. The kits include the Javacompiler, Java debugger and Java class libraries for generating Java bytecode

• Other compiler and IDE vendors - Symantec's Visual Cafe, Borland's JBuilder, IBM's VisualAge, Asymetrix's Supercede and Microsoft's Visual J++ : provide Java compilers that have the JIT compilation option. – eg. JBuilder is a cross-platform Java development environment for

Windows, Linux, Solaris and Mac OS.

Page 9: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 9

Java Development Kit

• javac - The Java Compiler

• java - The Java Interpreter

• jdb - The Java Debugger

• Appletviewer - Tool to run the applets

Page 10: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 10

Java Products & Technologies

• Java SE - Java Platform, Standard Edition (also known as Java 2 Platform) lets you develop and deploy Java applications on desktops and servers, as well as today's demanding Embedded and Real-Time environments.

• Java EE - Java Platform, Enterprise Edition (Java EE) is the industry standard for developing portable, robust, scalable and secure server-side Java applications.

• Java ME - Java™ Platform, Micro Edition (Java ME) is the most popular application platform for mobile devices across the globe. It provides a robust, flexible environment for applications running on a broad range of embedded devices, such as mobile phones, PDAs, TV set-top boxes, and printers.

• Java Card - Java Card technology provides a secure environment for applications that run on smart cards and other devices with very limited memory and processing capabilities.

Page 11: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 11

Java Virtual Machine

• The .class files generated by the compiler are not executable binaries

– so Java combines compilation and interpretation

• Instead, they contain “byte-codes” to be executed by the Java Virtual Machine

– other languages have done this, e.g. UCSD Pascal

• This approach provides platform independ-ence, and greater security

Page 12: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 12

JVM

• Java is independent only for one reason:

– Only depends on the Java Virtual Machine

(JVM),

– code is compiled to bytecode, which is

interpreted by the resident JVM,

– JIT (just in time) compilers attempt to increase

speed.

Page 13: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 13

Compile-time EnvironmentCompile-time Environment

Java

Bytecodes

move locally

or through

network

Java

Source

(.java)

Java

Compiler

Java

Bytecode

(.class )

Java

Interpreter

Just in

Time

Compiler

Runtime System

Class

LoaderJava

Class

Libraries

Operating System

Hardware

Java

Virtual

machine

How it works…!

Page 14: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 14

Page 15: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 15

JRE

• The Java Virtual Machine forms part of a large

system, the Java Runtime Environment (JRE). Each

operating system and CPU architecture requires a

different JRE.

• The JRE comprises a set of base classes, which are an

implementation of the base Java API, as well as a

JVM.

• The portability of Java comes from implementations

on a variety of CPUs and architectures. Without an

available JRE for a given environment, it is

impossible to run Java software.

Page 16: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 16

Building Standalone JAVA

Programs

Page 17: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 17

JAVA Program

• Prepare the file HelloWorld.java using an

editor

• Invoke the compiler:javac HelloWorld.java

• This creates HelloWorld.class

• Run the java interpreter: java HelloWorld

Page 18: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 18

HelloWorld (standalone)

public class HelloWorld

{

public static void main(String[] args)

{

System.out.println("Hello World!");

}

}

• Note that String is built in

• println is a member function for the

System.out class

Page 19: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 19

Comments are almost like C++

• /* This kind of comment can span

multiple lines */

• // This kind is to the end of the line

• /**

* This kind of comment is a special

* ‘javadoc’ style comment

*/

Page 20: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 20

Primitive data types are like C

� Main data types are int, double,

boolean, char

� Also have byte, short, long, float

� boolean has values true and false

� Declarations look like C, for example,

– double x, y;

– int count = 0;

Page 21: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 21

Primitive data types• Integers :

– Byte : occupies 1 byte , Range : -128 to +127

– short : occupies 2 byte , Range : -32768 to

+32767

– int : occupies 4 byte , Range : -231 to +

(231 -1)

– Long : occupies 8 byte , Range : -263 to + (263

-1)

• Floating-Point Numbers :– Float : occupies 4 byte , Range : ± 3.4E+38

– Double : occupies 8 byte , Range : ± 1.8E+308

• Character : char :16-bit unicode representation

• boolean : has values true and false

Page 22: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 22

Constants

• Symbolic constants are initialized final

variables:

private final int stepLength = 48;

private static final int BUFFER_SIZE = 1024;

public static final int PIXELS_PER_INCH = 6;

Page 23: Introduction to Java - Devi Ahilya Vishwavidyalaya

28 March 2007 Java : Lecture 1 23

Java identifier

• Series of characters consisting of letters, digits,

underscores ( _ ) and dollar signs ( $ )

• Does not begin with a digit, has no spaces

• Examples: Welcome1, $value, _value,

button7

� 7button is invalid

• Java is case sensitive (capitalization matters)

� a1 and A1 are different