• Tidak ada hasil yang ditemukan

Topic : INTRODUCTION TO JAVA

N/A
N/A
Protected

Academic year: 2024

Membagikan "Topic : INTRODUCTION TO JAVA"

Copied!
27
0
0

Teks penuh

(1)

PowerPoint On

Java Programming

Topic : INTRODUCTION TO JAVA

By,

Hitha Paulson

Assistant Professor, Dept. of Computer Science LF College, Guruvayoor

(2)

 Invented by James Gosling

 at SUN Microsystems

 Released in 1995

 Earlier it was named as Oak

(3)

Birth of Java

Motivation

Platform independent language for Embedded systems

Language that can use with Internet

Developed by

Green Team led by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sheridan

Developed at

Sun Microsystems

Initially called

Oak in 1991

Renamed as

JAVA in 1995

(4)

JAVA Programming Language

 Syntax borrowed from C language

 Fully object oriented language

 OOP concept derived from C++

 Compiled and Interpreted

 Platform Independent

(5)

Byte code and class files

 Source Code: .java file

 Byte Code:

Intermediate code generated after

compilation (.class file)

 Object Code: Ready to

execute program

(6)

Java Virtual Machine (JVM)

 A Java Virtual Machine (JVM) is a set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts

 Java Virtual Machines operate on Java bytecode, which is normally (but not necessarily) generated

from Java source code

(7)

 Bytecode is a highly optimized set of instructions designed to be executed by JVM

 Different JVM are there for different

platforms

(8)

Java and C

 Excluded

Keywords such as goto, sizeof, typedef

Data types such as Struct, union, enum

Data type modifiers such as auto, extern, register, signed, unsigned

Pointers

Preprocessors

Variable arguments for functions

void in functions without parameters

 Included

New operators instanceof and >>>

Labelled break and continue

(9)

Java and C++

 No operator overloading

 No template classes

 No Multiple Inheritance

 No Global variable

 Destructors are replaced by finalize

 Header files are replaced by packages

(10)

Java Buzzwords

 Simple

 Secure

 Portable

 Object-Oriented

 Robust

 Multithreaded

 Architecture-neutral (Write once; run anywhere;

any time; forever)

 Interpreted

 High performance (JIT Compiler)

 Distributed

 Dynamic

(11)

Java and Internet

 What is Internet

 How data is moving in WWW

 Need of portability

 Passive data and Dynamic, active program

 Solution: Java Applet

Java program transmitted over network and executed by a Java enabled Browser

 Security of data

(12)

Types of Java Programs

 Application Program

 Applet Program

 Servlet Program

 JSP Program

 J2EE Program

 J2ME Program

(13)

Overview of Java Programs

 All Java Programs are Fully object oriented

 Main building blocks of Java programs are predefined Classes (Class library)

 Class Libraries are bundled as Packages

 All packages are resides under the core package java

 Example of packages

java.io

java.lang

java.awt

java.net

java.swing

 Packages are included by using import keyword

Eg: import java.io.DataInputStream;

(14)

Classes & Methods

 Java is strongly typed and case sensitive

 Predefined class names are started with capital letter

 If class name contains more than one words

Each words are started with Capital Letter

No white space between these words

Eg: DataInputStream

 Functions of a class are known as methods

 Predefined method names started with small letter

 If method name contains more than one words

Each words are started with Capital Letter

No white space between these words

Eg:readLine();

(15)

Keywords and Identifiers

 Most of the keywords are borrowed from C language

 These keywords have same meaning as in C

 Eg: static, for, while, void, public

 Identifiers

Programmer defined tokens

Used to name classes, methods, variables, objects, labels, packages and Interfaces

Rules

Can contain alphabets, digits, underscore and dollar sign

Cannot start with digit

Upper case and lower case letters are distinct

They can be of any length

(16)

Java Program Structure

Documentation Section Package statement Import statements Interface statements

Class definitions Main method class

{

Main method definition }

(17)

Sample Application Program

class testpgm

{ public static void main(String s[]) {

System.out.println(“Hello”);

} }

Note: All Classes in java.lang package will

automatically import

(18)

Java Compiler and Interpreter

 Compiler: javac

Input: .java file

Output: .class file

Eg: javac testpgm.java

 Interpreter: java

Input: .class file (Mention filename without .class)

Output: get program executed

Eg: java testpgm

(19)
(20)

Literals

 Integer Literals

Decimal (default). Eg: 1, 67, 987L(Long Integer)

Octal. Eg: 01, 067

Hexadecimal. Eg: 0x1, 0x67

 Floating-point Literals

Float Literal. Eg: 2.0F, 3.14F

Double Literal (Default). Eg: 2.0, 3.14

 Boolean Literals

True (Not equal to non-zero)

False (Not equal to Zero)

 Character Literals

Unicode Character Set

ASCII Characters. Eg: „x‟, „5‟

Backslash constants. Eg: „\n‟, „\”‟, „\f‟

 String Literals

Eg: “Hello World”, “two \nlines”, “\”this is in quotes\””

(21)

Type conversion and Casting

 Automatic Type Conversion

Two types must be compatible

Destination type should be larger than source type

Integer type and Float type are compatible

Numeric type is not compatible with char or boolean

char and boolean are not compatible each other

 Type casting

Narrowing conversion. Eg: int y=100; byte x=(byte) y;

Truncation occurs

 Automatic Type Promotion

All lower datatype variables will promote to higher data type

(22)

Arrays

 Array concept is very same

 Dynamic memory allocation

 Declaration

Eg: int x[]; x=new int[10];

Eg: int y[]= new int[10];

New is used to allocate memory

Numeric array locations are always initialized to 0

 Initialization

Eg: float num[] = { 10.1, 11.2, 12.3, 13.4 };

 When array index goes out of range, java generates Runtime exception called

(ArrayIndexOutofBoundsException)

(23)

Multidimensional Array

Array of Arrays

 Eg: int twoD[][] = new int[4][5]

[0][0] [0][1] [0][2] [0][3] [0][4]

[1][0] [1][1] [1][2] [1][3] [1][4]

[2][0] [2][1] [2][2] [2][3] [2][4]

[3][0] [3][1] [3][2] [3][3] [3][4]

(24)

Multidimensional Array – cont..

 int twoD[][] = new int[4][];

 twoD[0] = new int[1];

 twoD[1] = new int[2];

 twoD[2] = new int[3];

 Alternate Array Declaration

Int a[] = new int[3];

Int[] b = new int[3];

[0][0]

[1][0] [1][1]

[2][0] [2][1] [2][2]

(25)

Java Operators

 Arithmetic

+, -, *, /, %, ++, --, +=, -=, *=, /=, %=

Operands must be numeric type. Boolean is not allowed, but char is allowed

% can apply to both Integer and Float type operands

 Relational

==, !=, >, <, >=, <=

 Logical

&, |, ^, ||, &&, !, &=, |=, ^=, = =, !=, ?:

 Bitwise

~, &, |, ^, >>, >>>, <<, &=, |=, ^=, >>=,

>>>=, <<=

(26)

Operator Precedence

Highest

() [] .

++ -- ~ !

* / %

+ -

>> >>> <<

> >= < <=

= = !=

&

^

|

&&

||

?:

= Op=

Lowest

Note: Operator precedence can override by using parentheses

(27)

Control Statements

 If stmt

Simple if

Nested if

If-else-if ladder

 Switch stmt

 Iteration stmts

While loop

Do..while loop

 Break & Continue

 Return stmt

Referensi

Dokumen terkait

Since a minimal class contains at most one weakly eutactic lattice (up to similarity), the (strongly) eutactic lattices C p and C p ′ are similar, hence isometric since they have

Again, note that the Square object has no specific draw method, but that it inherits one from its parent Rectangl class.. It also inherits any methods that the parent of

In a language such as Java, the pseudo-random number generation has fortunately been imple- mented in a library class, so all we have to do to receive a pseudo-random number is to make

Sensor signal A measurable quantity that depends on the sensor itself Except few cases, Most sensors are passive devices: a circuit is necessary to generate the signal...

Some words in Javanese Mengare, primarily some reduplicated words, have form that are quite unique because they do not comply with reduplication process in the Java language, yet more

Table 2 Comparison of winter-forage class names and certainties Classes Year Higher certainty Lower certainty 2013 Brassica grazed to bare soil by July/September Brassica

programmer- defined classes class Bicycle { // Data Member private String ownerName; //Constructor: Initialzes the data member public Bicycle { ownerName = "Unknown"; } //Returns

For every non-terminal defined in the abstract grammar .ast files, JastAdd generates a corresponding Java class in gen/abs/frontend/ast, which represents a type of Abstract Syntax Tree