Beruflich Dokumente
Kultur Dokumente
PROGRAMMING
5/12/16
Getting Started
(1) Create the source file:
open a text editor, type in the code which defines a class (HelloWorldApp) and then save it in a
file (HelloWorldApp.java)
file and class name are case sensitive and must be matched exactly (except the .java part)
Example Code: HelloWorldApp.java
Getting Started
(2) Compile the program:
compile HelloWorldApp.java by using the following
command:
javac HelloWorldApp.java
5/12/16
Getting Started
(3) Run the program:
run the code through:
java HelloWorldApp
5/12/16
5/12/16
5/12/16
class
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
5/12/16
Lexical Issues
Literals
A constant value in Java is created by using a literal representation. A literal is
allowed to use anywhere in the program.
Eg:
Comments
// This comment extends to the end of the line.
/*..............the multi-line comments are given with in this comment
............................................................................................................*/
/**....... Documentation comments..........*/
(readable to both human and computer)
5/12/16
Lexical Issues
Separators
Separators are used to terminate statements. In java there are few
characters are used as separators . They are, parentheses(), braces{},
brackets[], semicolon;, period., and comma,.
Java Keywords ( 49 )
5/12/16
Data Types
1.
2.
3.
4.
10
Data Types
Integers
Name
Width(Size)
Range
byte
8 bits
-128 to 127
short
16 bits
-32768 to 32767
int
32 bits
-2147483648 to 2147483647
long
64 bits
-264-1 to 264-1 -1
Floating-point
5/12/16
Name
Width(Size)
Range
float
32 bits
1.4e-045 to 3.4e+038
double
64 bits
4.9e-324 to 1.8e+038
11
Data Types
Characters
A char variable stores a single character from the Unicode
character set
A character set is an ordered list of characters, and each
character corresponds to a unique number
Unicode is an international character set, containing symbols
and characters from many world languages
The Unicode character set uses 16 bits per character, allowing
for 65,536 unique characters
Character literals are enclosed in single quotes:
A' c' 3' '$' .' '\n
5/12/16
12
Data Types
You might have heard about the ASCII character set
ASCII is older and smaller than Unicode, but is still quite popular
The ASCII characters are a subset of the Unicode character set, including:
uppercase letters A, B, C,
lowercase letters a, b, c,
digits 0, 1, 2,
special symbols *, &, +, ?,
control characters
backspace \b
Characters
Data type used to store character is char.
It requires 16 bits.
Range of a char is 0 to 65536 (no negative char)
5/12/16
13
Data Types
Boolean
Used to test a particular condition
It can take only two values: true and false
Uses only 1 bit of storage
All comparison operators return boolean value
5/12/16
14
Constants
Refer to fixed values that do not change during the execution of a
program.
Integer Constants: sequence of digits
123 037 0X2
Real Constants: numbers containing fractional parts
0.0083 -0.75 435.36
Single character Constants: single character
5 A ;
String Constants: sequence of characters
Hello World ?+;;;;.#
Backslash character (Symbolic) Constants: used in output methods
\n\b\t \ \\ \\
5/12/16
15
Constants
5/12/16
16
Symbolic Constants
Some constants may appear repeatedly in a number of places in
the program.
These constants can be defined as a symbolic name
Make a program:
easily modifiable
More understandable
final type symbolic name = value
ex:
final float PI = 3.14159
5/12/16
17
Variables
An identifier that denotes a storage location used to store a data value.
May take different values at different times during the execution of the
program
Naming Conventions: may consists of alphabets, digits, underscore and dollar
with following conditions
Must not begin with a digit
Uppercase and Lowercase are distinct.
Should not be a keyword
Space is not allowed
Variable declaration and assignment
<type> variable_name;
Variable_name =value; // initializtaion
Variable_name=Math.sqrt(Variable_name* Variable_name);
dynamic initialization
5/12/16
//
18
Scope of Variables
Classified into three kinds:
Instance Variables
Created when the objects are instantiated
Take different values for each object
Class Variables
Global to a class
Belong to the entire set of objects that class creates
Only one memory location is created
Local Variables
Used inside methods or inside program blocks.
Blocks are defined between opening brace { and a closing brace }
Visible to program only from the beginning to the end of the block.
5/12/16
19
Array
5/12/16
20
Creation of Arrays
After declaring arrays, we need to allocate memory for
storage array items.
In Java, this is carried out by using new operator, as
follows:
stude
Arrayname = new type[size];
nts
0
Examples:
1
students = new int[7];
2
3
5/12/16
21
Initialization of Arrays
1. Once arrays are created, they need to be initialised with
some values before access their content. A general form of
initialisation is:
Arrayname [index/subscript] = value;
2. Example:
stude
nts
students[0] = 50;
0
50
students[1] = 40;
1
3. Array index starts with 0 and ends with n-1
2
3
4. Trying to access an array beyond its
4
boundaries will generate an error message.
5
students[7] = 100;
6
5/12/16
22
Explicit initialization
1.
Arrays can also be initialised like standard variables at the time of their
declaration.
Type arrayname[] = {list of values};
2.
Example:
int[] students = {55, 69, 70, 30, 80, 90, 45};
3.
4.
5/12/16
stude
nts
0
55
1
69
70
30
80
90
45
23
24
5/12/16
25
Array as Parameters
Methods can accept arrays via parameters
Use square brackets [ ] in the parameter declaration:
5/12/16
26
Returning Array
A method may return an array as its result
double [] readArray(int n)
{
double result[] = new double[n];
for (i = 0; i < n; i++)
{
result[i] = i;
}
return result;
}
5/12/16
27
5/12/16
10
20
85
45
56
58
25
58
45
85
65
78
65
14
28
56
BY: Raju Pal
28
5/12/16
29
30
5/12/16
31
[0]
[0
]
[1]
[2]
[1
]
5/12/16
32
Multidimensional Arrays
Example: A farmer has 10 farms of beans each in 5
countries, and each farm has 30 fields!
Three-dimensional array:
long[][][] beans=new long[5][10][30];
//beans[country][farm][fields]
5/12/16
33
5/12/16
34
Classes
A class is a user-defined data type. Once defined, this
new type can be used to create variables of that type.
These variables are termed as instances of classes,
which are the actual objects.
A class is a template for an object, and an object is an
instance of a class.
5/12/16
35
A class template
Data Fields:
radius is _______
Methods:
getArea
Circle Object 1
Circle Object 2
Circle Object 3
Data Fields:
radius is 10
Data Fields:
radius is 25
Data Fields:
radius is 125
Three objects of
the Circle class
36
Class cont..
//Basic form of a class
class classname
{
type variable1
type variable2
-----------------------------type methodname1(parameter_list)
{
//body of method
}
type methodname2(parameter_list)
{
//body of method
}
--------------}
5/12/16
37
Box myBox;
Creating Objects
objectReference = new ClassName();
Example: myBox = new Box();
The object reference is assigned to the object reference variable.
Declaring/Creating Objects in a Single Step
ClassName objectReference = new ClassName();
Example: Box myBox = new Box();
5/12/16
38
Accessing Objects
Referencing the objects data:
objectRefVar.data
e.g., myBox.width
myBox.height
myBox.depth
39
myBox
null
5/12/16
40
null
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
Create a Box
5/12/16
41
Assign object
reference to myBox
Box
Width
Height
Depth
yourBox.depth= 50;
5/12/16
reference value
42
reference value
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
yourBox
null
Declare yourBox
5/12/16
43
reference value
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
null
yourBox
Create a new
Box object
5/12/16
Box
Width
Height
Depth
44
reference value
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
yourBox
Assign object
reference to yourBox
5/12/16
reference value
Box
Width
Height
Depth
45
reference value
yourBox.width = 100;
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
reference value
yourBox
Change variables in
yourBox
5/12/16
Box
Width =100
Height=10
Depth =50
46
reference value
Box
yourBox.height = 10;
Width
Height
Depth
yourBox.depth= 50;
myBox = yourBox;
yourBox
reference value
Box
Assigning Object
Reference Variables
5/12/16
Width =100
Height=10
Depth =50
47
Garbage Collection
5/12/16
48
Introducing Methods
type name(parameter-list)
{
//body of method
return value; (if type is not void)
}
Define the interface to most classes.
Hide specific layout of internal data structures(Abstraction)
Add method to Box class
void volume()
{
System.out.print(Volume is: );;
System.out.println(width*height*depth);
}
5/12/16
49
Returning a Value
double volume()
{
return width*height*depth;
}
50
Parameterized Methods
void setDim(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
51
Constructors
Box()
{
//default constructor
}
Box()
{
width = 10;
height = 10;
depth = 10;
}
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box myBox = Box();
Box yourBox = Box(20,20,20);
5/12/16
52
Constructors cont
A constructor with no parameters is referred to as a default
constructor.
Constructors must have the same name as the class itself.
Constructors do not have a return typenot even void.
Implicit return type of a class constructor is the class type
itself.
Constructors are invoked using the new operator when
an object is created.
Constructors play the role of initializing objects.
5/12/16
53
Overloading Methods
Method overloading defines several different versions of a method
all with the same name, but each with a different parameter list.
At the time of method call, java compiler will know which one you
mean by the number and/or types of the parameters.
Overloaded methods must differ in the type and/or number of their
parameters.
Implements polymorphism
May have different return types.
To overload a method, you just define it more than once,
specifying a new parameter list different from every other
5/12/16
54
55
Overloading Constructors
Works like overloading other methods.
Define the constructor a number of times, each time with a different parameter
list.
//when all dimensions specified
Box(double w, double h, double d){
Width = w;
Height = h;
Depth = d;
}
//when cube is created
Box(double l){
Width = Height = Depth = l;
}
Box myBox = new Box(10,20,15);
Box yourBox = new Box(25);
5/12/16
56
57
58
59
5/12/16
20
10
60
5/12/16
61
5/12/16
62
5/12/16
63
static Methods
Methods declared as static have several restrictions:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way.final Keyword
A variable can be declared as final.
contents of the variable cannot can not be modified.
Must initialize a final variable when it is declared.
64
Using Command-Line
Arguments
// Display all command-line
arguments.
class CommandLine {
public static void main(String args[])
{
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: "
+args[i]);
}
} CommandLine this is a test 100 -1
java
args[0]:
args[1]:
args[2]:
args[3]:
args[4]:
args[5]:
5/12/16
this
is
a
test
100
-1
65
66