Download Introduction to JAVA

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 language derives much of its syntax from C
and C++ but has a simpler object model and fewer lowlevel 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
JAVA APPLICATIONS AND APPLETS
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.
3
COMPILING 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
PLATFORM INDEPENDENT JAVA
PROGRAMS COMPILING
 You can run bytecode on any computer that has a Java
Interpreter installed
“Hello.java”
“Hello.class”
5
FUNDAMENTALS
6
HELLO 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
SAVING, COMPILING AND RUNNING JAVA
PROGRAMS
 Saving 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
COMMENTS 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 comment
It ends at this line. */
// This comment starts here and ends at the end of this line.
9
GOOD 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
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
IDENTIFIER
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.
12
STATE
My be changed
 variable
All lowercase.
Capitalizing the first letter of each word in a multiword identifier, except for the
first word.
Cannot be changed  constant
All uppercase, separating words within a multiword identifier with the
underscore symbol, _.
13
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
JAVA BUILT-IN DATA TYPES
Constant or Variable
First Decision Level
Second Decision Level
Third Decision Level
Integer
Boolean
Character
Numeric
Floating-point
byte
float
short
double
char
String
boolean
Fourth Decision Level
int
15
long
PRIMITIVE DATA TYPES
Size
(bits)
Type
boolean
Range
Description
true, false
Stores a value that is either
true 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,808
to
+9,223,372,036,854,775,807
Stores an integer.
float
32 bits
accurate to 8 significant digits
Stores a single-precision
floating point number.
double
64 bits
accurate to 16 significant digits
Stores a double-precision
floating point number.
16
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
CONSTANT DECLARATION
final dataType constIdentifier = literal | expression;
final double PI
= 3.14159;
final int
MONTH_IN_YEAR
= 12;
final short FARADAY_CONSTANT = 23060;
These are called
literals.
final int MAX
= 1024;
final int MIN
= 128;
final int AVG
= (MAX + MIN) / 2;
This is called
expression.
18
VARIABLE DECLARATION
 A variable may be declared:
 With initial value.
 Without initial value
 Variable declaration with initial value;
dataType variableIdentifier = literal | expression;
double avg
int
i
int
= 0.0;
= 1;
x =5, y = 7, z = (x+y)*3;
 Variable declaration without initial value;
dataType variableIdentifier;
double avg;
int
i;
19
MORE DECLARATION EXAMPLES
 String declaration
 with initial value:
 String word="Apple";
 without initial value:
 String word;

Boolean declaration:
 with initial value:
 boolean flag=false;
 boolean valid=true;
 without initial value:
 boolean flag;
 char declaration
 with initial value:
 char symbol ='*';
 without initial value:
 char symbol;
20
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
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)
char aCharecter='A';
char aAscii =65;
System.out.println(aCharecter);
System.out.println(aAscii);
A
A
22
23
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
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
double x=7.9 ,y= 6.7;
int result;
result=(int)(7.9 + 6.7);
25
TYPE CONVERSION (CASTING)
8. (int)(‘A’)
9. (int)(‘8’)
10. (char)(65)
11. (char)(56)
= 65
= 56
= ‘A’
= ‘8’
26
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
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
Java Programming: From Problem Analysis to Program Design, Second Edition
28
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
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 fun
g
17
-1
30
Java Programming: From Problem Analysis to Program Design, Second Edition
30
SOME COMMONLY USED STRING METHODS
31
Java Programming: From Problem Analysis to Program Design, Second Edition
31
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 fun
18
12
8
32
SOME COMMONLY USED STRING METHODS
33
Java Programming: From Problem Analysis to Program Design, Second Edition
33
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
Java Programming: From Problem Analysis to Program Design, Second Edition
34
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’));
System.out.println(s1.indexOf(‘e’,5));
// 1
// 6
35
Java Programming: From Problem Analysis to Program Design, Second Edition
35