Weitere Àhnliche Inhalte
Ăhnlich wie Overview of Java (20)
KĂŒrzlich hochgeladen (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.