Más contenido relacionado
La actualidad más candente (12)
Similar a Overview of Java (20)
Overview of Java
- 2. Table of Contents
Introduction
Data Types and Operators
Program Control Statements
Methods, Classes & Objects
Other Topics
Exercise
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 4. Introduction
Originally developed by James Gosling at Sun
Microsystems - 1991
Derives much of its syntax from C and C++
Applications are typically compiled to
bytecode (class file) that can run on any Java
Virtual Machine (JVM)
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 5. Introduction
Is a general-purpose, concurrent, class-based,
object-oriented language
“Write once, run anywhere"
Current stable release: Java Standard Edition 6
(1.6.0)
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 7. Introduction
Can be broken down into:
Java Card
Micro Edition (ME)
Standard Edition (SE)
Enterprise Edition (EE)
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 8. Introduction
/*
This is a simple Java program.
Call this file Example.java. Compile and run in Eclipse.
*/
class Example {
// A Java program begins with a call to main().
public static void main(String args[]) {
System.out.println("Java drives the Web.");
}
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 9. Introduction
/*
This demonstrates a variable.
Call this file Example2.java. Compile and run in Eclipse.
*/
class Example2 {
public static void main(String args[]) {
int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
System.out.println("var1 contains " + var1);
var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2);
}
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 11. Data Types and Operators
• Contains 2 general categories of built-in data
types: object-oriented and non-object
oriented.
• There are eight primitives
• Primitive means these types are not objects
but rather normal binary values
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 12. Data Types and Operators
Type Meaning
boolean Represents true/false values
byte 8-bit integer
char Character
double Double-precision floating point
float Single-precision floating point
int Integer
long Long integer
short Short integer
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 13. Data Types and Operators
• Contains 2 general categories of built-in data
types: object-oriented and non-object
oriented.
• There are eight primitives
• Primitive means these types are not objects
but rather normal binary values
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 14. Data Types and Operators
• An operator is a symbol that tells the compiler
to perform a specific mathematical or logical
manipulation.
• Java has four general classes of operators:
arithmetic, bitwise, relational, and logical.
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 15. Data Types and Operators
Arithmentic operator examples:
+ implies addition
/ implies division
% implies modulus
++ implies increment
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 16. Data Types and Operators
Relational refers to the relationships that
values can have with one another
Examples include:
== implies Equal to
!= implies Not Equal to
> implies Greater than
<= implies Less than or Equal to
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 17. Data Types and Operators
Logical refers to the ways in which true and
false values can be connected together
Examples include:
& implies AND
| implies OR
! implies NOT
&& implies Short-circuit AND
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 18. Data Types and Operators
The outcome of the relational and logical
operators is a boolean value.
The Assignment operator: var = expression;
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 19. Data Types and Operators
• Approximately 50 keywords are currently
defined in the Java language
• Examples: enum, true, false, null, import, do,
break, for, int
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 20. Data Types and Operators
An identifier is a name given to a method, a
variable, or any other user-defined item
Identifiers can be from one to several
characters long
Variable names may start with any letter of
the alphabet, an underscore, or a dollar sign.
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 22. Program Control
Statements
• The if statement
• if(condition) statement;
• Example:
if(10 < 11) System.out.println("10 is less than 11");
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 23. Program Control
Statements
The general form of the if, using blocks of
statements, is:
if(condition)
{
statement sequence
}
else
{
statement sequence
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 24. Program Control
Statements
• A common programming construct that is based
upon the nested if is the if-else-if ladder.
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 25. Program Control
Statements
• The for Loop:
• for(initialization; condition; iteration) statement;
• Example
for(count = 0; count < 5; count = count+1)
System.out.println("This is count: " + count);
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 26. Program Control
Statements
• The Switch statement:
switch(expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
...
default:
statement sequence
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 27. Program Control
Statements
int i = …
switch(i) {
case 0:
System.out.println("i is zero");
break;
case 1:
System.out.println("i is one");
break;
case 2:
System.out.println("i is two");
break; break;
default:
System.out.println("i is three or more");
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 28. Program Control
Statements
The general form of the while loop is:
while(condition) statement;
Example:
// print the alphabet using a while loop
char ch;
ch = 'a';
while(ch <= 'z') {
System.out.print(ch);
ch++;
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 29. Program Control
Statements
• The general form of the do-while loop is
do {
statements;
} while(condition);
• Very similar to while loop
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 31. Classes & Objects
Java’s basic unit of encapsulation is the class
A class defines the form of an object
It specifies both the data and the code that
will operate on that data
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 32. Classes & Objects
Java uses a class specification to construct
objects
Objects are instances of a class.
Thus, a class is essentially a set of plans that
specify how to build an object
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 33. Classes & Objects
• The general form of a class definition:
class classname {
// declare instance variables
type var1;
type var2;
// ...
type varN;
// declare methods
type method1(parameters) {
// body of method
}
type method2(parameters) {
// body of method
}
// ...
type methodN(parameters) {
// body of method
}
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 34. Classes & Objects
How objects are declared:
Classname referenceName = new
Classname(arguments);
The general form of a method is:
ret-type name( parameter-list ) {
// body of method
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 35. Classes & Objects
A constructor initializes an object when it is created.
• A simple example that uses a constructor:
// A simple constructor.
class MyClass {
int x;
MyClass() {
x = 10;
}
}
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 37. Other Topics
• Casting: A cast is an instruction to the
compiler to convert one type into another.
• A cast has this general form:
(target-type) expression
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 38. Other Topics
• Inheritance is the process by which one object
can acquire the properties of another object.
• Use of hierarchies.
• The inheritance mechanism that makes it
possible for one object to be a specific
instance of a more general case
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 39. Other Topics
Polymorphism - “one interface, multiple
methods.”
This means that it is possible to design a
generic interface to a group of related
activities.
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 40. Other Topics
• Packages are groups of related classes.
• Packages help organize your code and provide
another layer of encapsulation.
• An interface defines a set of methods that will
be implemented by a class.
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 41. Other Topics
An interface does not, itself, implement any
method.
It is a purely logical construct.
Packages and interfaces give you greater
control over the organization of your program
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 42. Other Topics
An exception is an error that occurs at run
time.
Using Java’s exception handling subsystem you
can, in a structured and controlled manner,
handle run-time errors.
Use try-catch and Throwable
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 43. Other Topics
Java has built-in support for multithreaded
programming.
A multithreaded program contains two or more
parts that can run concurrently.
Each part of such a program is called a thread,
and each thread defines a separate path of
execution.
Thus, multithreading is a specialized form of
multitasking.
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 45. Exercise
• Program to simulate a School
• Attributes of school include:
– Name
– Location
• School has Teacher and Students
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 46. Exercise
• Teacher has:
– First & Last Name
– Course taught
– Years employed
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.
- 47. Exercise
• Student has:
– First & Last Name
– 5 subjects with grades
– Can get average grade
– Has a fee balace
Copyright © Tawi Commercial Services Ltd. 2015. All Rights
Reserved.