Vous êtes sur la page 1sur 23

Introduction to Java

Lecture 1

School of Computer Science psaxena.scs@dauniv.ac.in


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

28 March 2007 Java : Lecture 1 2


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

28 March 2007 Java : Lecture 1 3


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
28 March 2007 Java : Lecture 1 4
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

28 March 2007 Java : Lecture 1 5


How it is different from C++
No preprocessor. Heavy reliance on preprocessor.
No goto goto
No global variables. Global variables.
Pure object-oriented language. Hybrid between procedural and object-oriented.
All functions (methods) are part of a class. Can have stand-along functions.
No multiple inheritance. Multiple inheritance.
No operator overloading. Operator overloading.
All methods (except final methods) are
Virtual functions are dynamically bound.
dynamically bound.
Separate types for structs, unions, enums, and
All nonprimitive types are objects.
arrays.
All numeric types are signed. Signed and unsigned numeric types.
All primitive types are a fixed size for all
Primitive type size varies by platform.
platforms.
16-bit Unicode characters. 8-bit ASCII characters.
Boolean data type primitive. No explicit boolean data type.

28 March 2007 Java : Lecture 1 6


How it is different from C++
Variables are automatically initialized. No automatic initialization of variables.
References, with no explicit pointer Pointers, with dereferencing (* or ->) and
manipulation and no pointer arithmetic. address (&) operators.
Array references are not translated to pointer
Array references translate to pointer arithmetic.
arithmetic.
Strings are objects. Strings are null-terminated character arrays.
Built-in string concatenation operator(+). String concatenation through a library function.
No typedef. typedef to define types.
Specifically attuned to network and Web
No relationship to networks or the Web.
processing.
Automatic garbage collection. No automatic garbage collection.
Combination of compiled and interpreted. Compiled.
Slower execution when interpreted. Fast execution.
Architecture neutral. Architecture specific.
Supports multithreading. No multithreading.

28 March 2007 Java : Lecture 1 7


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 Java
compiler, 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.

28 March 2007 Java : Lecture 1 8


Java Development Kit

• javac - The Java Compiler


• java - The Java Interpreter
• jdb - The Java Debugger
• Appletviewer - Tool to run the applets

28 March 2007 Java : Lecture 1 9


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.

28 March 2007 Java : Lecture 1 10


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
28 March 2007 Java : Lecture 1 11
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.

28 March 2007 Java : Lecture 1 12


How it works…!
Compile-time Environment Compile-time Environment

Class Java
Loader Class
Libraries
Java
Source
(.java)

Just in
Java Java
Time
Bytecodes Interpreter Java
Compiler
move locally Virtual
or through machine
Java network
Compiler
Runtime System

Java Operating System


Bytecode
(.class )
28 March 2007 Java : Lecture 1 13
Hardware
28 March 2007 Java : Lecture 1 14
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.

28 March 2007 Java : Lecture 1 15


Building Standalone JAVA
Programs

28 March 2007 Java : Lecture 1 16


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

28 March 2007 Java : Lecture 1 17


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
28 March 2007 Java : Lecture 1 18
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
*/

28 March 2007 Java : Lecture 1 19


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;

28 March 2007 Java : Lecture 1 20


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
28 March 2007 Java : Lecture 1 21
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;

28 March 2007 Java : Lecture 1 22


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

28 March 2007 Java : Lecture 1 23

Vous aimerez peut-être aussi