35
INTRODUCTION TO JAVA INTRODUCTION TO JAVA CHAPTER 1 CHAPTER 1 1

INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Embed Size (px)

Citation preview

Page 1: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

INTRODUCTION TO JAVAINTRODUCTION TO JAVACHAPTER 1CHAPTER 1

1

Page 2: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

WHAT IS JAVA ?

• Java is a programming language and computing platform first released by Sun Microsystems in 1995. 

• The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities.

• The Java language is accompanied by a library of extra software that we can use when developing programs.• The library provides the ability to create graphics,

communicate over networks, and interact with databases.

• The set of supporting libraries is huge. 2

Page 3: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

JAVA APPLICATIONS AND APPLETSJAVA APPLICATIONS AND APPLETS

3

Applications – standalone Java programs

Applets – Java programs that run inside web browsers

Java is the first programming language to deliberately embrace the concept of writing programs that can be executed on the Web..

Page 4: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

COMPILING JAVA PROGRAMSCOMPILING JAVA PROGRAMS

The Java compiler produces bytecode (a “.class” file) not machine code from the source code (the “.java” file).

Bytecode is converted into machine code using a Java Interpreter

4

Page 5: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

PLATFORM INDEPENDENT JAVA PLATFORM INDEPENDENT JAVA PROGRAMS COMPILINGPROGRAMS COMPILING

You can run bytecode on any computer that has a Java Interpreter installed

5

“Hello.java” “Hello.class”

Page 6: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

FUNDAMENTALS 6

Page 7: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

HELLO WORLD JAVA PROGRAMHELLO WORLD JAVA PROGRAM

// import section

public class MyFirstprogram {

// main method

public static void main( String args[] ){

System.out.println(“Hello World”);

} // end main

} // end class 7

Page 8: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

SAVING, COMPILING AND RUNNING SAVING, COMPILING AND RUNNING JAVA PROGRAMSJAVA PROGRAMSSaving a Java program :

A file having a name same as the class name should be used to save the program. The extension of this file is ”.java”.

“MyFirstprogram.java”.

Compiling a Java program : Call the Java compiler javac

The Java compiler generates a file called” MyFirstprogram.class” (the bytecode).

Running a Java program Call the Java Virtual Machine java:

•java MyFirstprogram.class

8

Page 9: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

COMMENTS IN A JAVA PROGRAMCOMMENTS IN A JAVA PROGRAM• Comments are used to describe what your code

does its improve the code readability. • The Java compiler ignores them.• Comments are made using

// which comments to the end of the line, /* */, everything inside of it is considered a

comment (including multiple lines).

Examples:/* This comment begins at this line.This line is included in this commentIt ends at this line. */

// This comment starts here and ends at the end of this line.

9

Page 10: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

GOOD PROGRAMMING PRACTICEGOOD PROGRAMMING PRACTICE

• Be careful java is a sensitive language.• It is good to begin every program with a comment that

states the purpose of the program and the author.• Every java program consist of at least one class that the

programmer define.• Begin each class name with capital letter.• The class name doesn’t begin with a digit and doesn’t

contain a space.• Use blank lines and spaces to enhance program

readability• When you type an opining left brace{ immediately type the closing right brace}• Indent the entire body of each class declaration 10

Page 11: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

PROGRAMS AND DATA

Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory.

A space memory has three characteristics• Identifier

• Data Type

• State

11

Page 12: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

IDENTIFIERIDENTIFIER

12

is a sequence of characters that denotes the name of the space memory to be used.

•This name is unique within a program.

Identifier Rules

•It cannot begin with a digit (0 – 9).

•It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _.

•No spaces or punctuation, except the underscore symbol, _, are allowed. 

Identifiers in Java are case-sensitive. The identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

Page 13: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

STATESTATE

My be changed variableAll lowercase.

Capitalizing the first letter of each word in a multiword identifier, except for the first word.

Cannot be changed constantAll uppercase, separating words within a multiword identifier with the underscore symbol, _.

13

Page 14: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

DATA TYPE

•The data type defines what kinds of values a space memory is allowed to store.

•All values stored in the same space memory should be of the same data type.

•All constants and variables used in a Java program must be defined prior to their use in the program.

14

Page 15: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

JAVA BUILT-IN DATA TYPESJAVA BUILT-IN DATA TYPES

15

Constant or Variable

Numeric Character Boolean

Integer Floating-point

short

int

char String boolean

float

double

First Decision Level

Second Decision Level

Third Decision Level

Fourth Decision Level

long

byte

Page 16: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

PRIMITIVE DATA TYPESPRIMITIVE DATA TYPES

16

TypeSize (bits)

Range Description

boolean true, false Stores a value that is eithertrue or false.

char 16 0 to 65535 Stores a single 16-bit Unicode character.

byte 8 -128 to +127 Stores an integer.

short 16 -32768 to +32767 Stores an integer.

int 32 bits -2,147,483,648 to+2,147,483,647

Stores an integer.

long 64 bits -9,223,372,036,854,775,808to+9,223,372,036,854,775,807

Stores an integer.

float 32 bits accurate to 8 significant digits Stores a single-precisionfloating point number.

double 64 bits accurate to 16 significant digits Stores a double-precisionfloating point number.

Page 17: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

VARIABLE/CONSTANT DECLARATION

When the declaration is made, memory space is allocated to store the values of the declared variable or constant.

The declaration of a variable means allocating a space memory which state (value) may change.

The declaration of a constant means allocating a space memory which state (value) cannot change.

17

Page 18: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

CONSTANT DECLARATION

final dataType constIdentifier = literal | expression;

final double PI = 3.14159;final int MONTH_IN_YEAR = 12;final short FARADAY_CONSTANT = 23060;

final int MAX = 1024;

final int MIN = 128;

final int AVG = (MAX + MIN) / 2;

18

These are called literals.

These are called literals.

This is called expression.

This is called expression.

Page 19: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

VARIABLE DECLARATIONVARIABLE DECLARATION

A variable may be declared: With initial value. Without initial value

Variable declaration with initial value;dataType variableIdentifier = literal | expression;

double avg = 0.0;int i = 1;

int x =5, y = 7, z = (x+y)*3;

Variable declaration without initial value;

dataType variableIdentifier;

double avg;int i;

19

Page 20: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

MORE DECLARATION EXAMPLESMORE DECLARATION EXAMPLES

String declaration

with initial value:

String word="Apple";

without initial value:

String word;

char declaration

with initial value:

char symbol ='*';

without initial value:

char symbol;

20

Boolean declaration: with initial value:

boolean flag=false; boolean valid=true;

without initial value: boolean flag;

Page 21: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

VARIABLES

Float The default type of floating point numbers is

double . The declaration :

float rate = 15.5f ;

without the f , the compiler will generate an error

21

Page 22: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

VARIABLES Char

When using the char data type, you enclose each character represented within single quotations marks.

Ex:

char c = ‘A’

char space = ‘ ‘

Each character is represented by a value (‘A’ is represented by the value 65)

22

char aCharecter='A';char aAscii =65;System.out.println(aCharecter);System.out.println(aAscii);

AA

Page 23: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

23

Page 24: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

TYPE CONVERSION (CASTING)

Used:

• to change one data type to another .

• to avoid implicit type correction.

Syntax:

(dataTypeName) expression

Expression evaluated first, then the value is converted to dataTypeName

24

Page 25: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

TYPE CONVERSION (CASTING)

Examples:1. (int)(7.9 + 6.7) = 14

2. (int)(7.9) + (int)(6.7) = 13

3. (double)(17) = 17.0

4. (double)(7) /2 = 7.0/2 = 3.5

5. (double)(7/2) = 3.0

6. (int)(7.8+(double)(15)/2) =(int)15.3 =15

25double x=7.9 ,y= 6.7;int result;result=(int)(7.9 + 6.7);

Page 26: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

TYPE CONVERSION (CASTING)

8. (int)(‘A’) = 65 9. (int)(‘8’) = 5610. (char)(65) = ‘A’ 11. (char)(56) = ‘8’

26

Page 27: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

THE CLASS STRING

Contains operations to manipulate strings.

String: Sequence of zero or more characters.

Enclosed in double quotation marks.

Is processed as a single unit .

Null or empty strings have no characters. “ “

Every character has a relative position , the first character is in position 0 .

27

Page 28: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 28

THE CLASS STRING

Java system automatically makes the class String available (i.e no need to import this class )

Example :

Consider the following declaration :

String sentence ;

sentence = “programming with java”

28

Page 29: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

THE CLASS STRING

Length of the string is the number of characters in it .

When determining the length of a string , blanks count .

Example : “ “ has length = 0 “abc” has length = 3 , position of a = 0 ,b= 1 ,

c= 2 “a boy” has length = 5

29

Page 30: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 30

SOME COMMONLY USED STRING METHODS

String mystr=new String("programming with Java is fun");

System.out.println(mystr); System.out.println(mystr.charAt(3)); System.out.println(mystr.indexOf('J')); System.out.println(mystr.indexOf(‘j'));

programming with Java is fung17-1

30

Page 31: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 31

SOME COMMONLY USED STRING METHODS

31

Page 32: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

EXAMPLE

String mystr=new String("programming with Java is fun");System.out.println(mystr); System.out.println(mystr.indexOf('a',10));System.out.println(mystr.indexOf("with"));System.out.println(mystr.indexOf("ing"));

programming with Java is fun18128

32

Page 33: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 33

SOME COMMONLY USED STRING METHODS

33

Page 34: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 34

EXAMPLES ON STRING METHODS

String s1 , s2 , s3 ;

s1 = “abcdefeg” ;

System.out.println( s1.length() ); // 8

System.out.println(s1.charAt(3)); //d

System.out.println(s1.indexOf(‘e’)); //4

System.out.println(s1.indexOf(“cd”)); //2

System.out.println(s1.toUpperCase()); //ABCDEFEG

34

Page 35: INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in 1995. The

Java Programming: From Problem Analysis to Program Design, Second Edition 35

EXAMPLES ON STRING METHODS

System.out.println(s1.substring(1 , 4)); //bcd

System.out.println(s1 + “xyz”); // abcdefegxyz

System.out.println( s1.replace(‘d’ ,’D’)); // abcDefeg

System.out.println(s1.charAt(4) ); // e

System.out.println(s1.indexOf(‘b’)); // 1

System.out.println(s1.indexOf(‘e’,5)); // 6

35