Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
105 views

Overview of Java: Dr. Turkan Ahmed Khaleel

Uploaded by

Duaa Hussein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Overview of Java: Dr. Turkan Ahmed Khaleel

Uploaded by

Duaa Hussein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

 Overview of Java

Dr. Turkan Ahmed Khaleel


2
Dr. Turkan Ahmed Khaleel

What is java?

 Developed by Sun Microsystems (James Gosling)

 A general-purpose object-oriented language

 Based on C/C++

 Designed for easy Web/Internet applications

 Write Once, Run Anywhere.


3
Dr. Turkan Ahmed Khalee l

Java History
o Problem: There was already a programming language called Oak.
o The “Green” team met at a local coffee shop to come up with another
name...
•Java!
Dr. Turkan Ahmed Khalee
4

Java: Write Once, Run Anywhere


• Consequence of Java’s history:
platform-independence

Click on link to Applet

Mac user running Safari


Web page stored on Unix server
Virtual machine translates byte code to
native Mac code and the Applet is run Byte code is downloaded

Windows user running Internet Explorer


Byte code
(part of web
page)
5

Java: Write Once, Run Anywhere


• Consequence of Java’s history:
platform-independent

Mac user running Safari


Web page stored on Unix server

Click on link to Applet


Byte code is downloaded

Windows user running Internet Explorer

Virtual machine translates byte code to


native Windows code and the Applet is run
6
Dr. Turkan Ahmed Khalee

Java Features
 Simple
 fixes some clumsy features of C++
 no pointers
 automatic garbage collection
 rich pre-defined class library http://java.sun.com/j2se/1.4.2/docs/api/
 Object oriented
 focus on the data (objects) and methods manipulating the data
 all functions are associated with objects
 almost all datatypes are objects (files, strings, etc.)
 potentially better code organization and reuse
7

Java: Write Once, Run Anywhere


• Java has been used by large and reputable companies to create
serious stand-alone applications.
• Example:
o Eclipse1: started as a programming environment created by IBM for
developing Java programs. The program Eclipse was itself written in
Java.

Dr. Turkan Ahmed Khalee


7
1 For more information: http://www.eclipse.org/downloads/
8

Install Java on Windows


This guide will guide you through the process of downloading and installing Java on
Microsoft Windows.
Visit the Java website and download the installer
To install Java, you first need to download the installer program from
Oracle:
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-
2133151.html

Eclipse for Java


Eclipse (@ www.eclipse.org) is an open-source Integrated Development
Environment (IDE) supported by IBM. Eclipse is popular for Java application
development (Java SE and Java EE) and Android apps.

How To Install Eclipse and Get Started with Java Programming

http://www.eclipse.org/
9

Java Features
• Interpreted
o java compiler generate byte-codes, not native machine code

o the compiled byte-codes are platform-independent

o java bytecodes are translated on the fly to machine readable


instructions in runtime (Java Virtual Machine)

• Portable
o same application runs on all platforms

o the sizes of the primitive data types are always the same

o the libraries define portable interfaces


10
Dr. Turkan Ahmed Khalee

Java Features
• Reliable
o extensive compile-time and runtime error checking

o no pointers but real arrays. Memory corruptions or unauthorized


memory accesses are impossible

o automatic garbage collection tracks objects usage over time

• Secure
o usage in networked environments requires more security

o memory allocation model is a major defense

o access restrictions are forced (private, public)


11

Java Features
• Multithreaded
o multiple concurrent threads of executions can run simultaneously

o utilizes a sophisticated set of synchronization primitives (based on


monitors and condition variables paradigm) to achieve this

• Dynamic
o java is designed to adapt to evolving environment

o libraries can freely add new methods and instance variables


without any effect on their clients

o interfaces promote flexibility and reusability in code by specifying


a set of methods an object can perform, but leaves open how these
methods should be implemented.
12
Dr. Turkan Ahmed Khalee l

Compiled Programs With Different Operating Systems

Windows
compiler

Executable (Windows)

Computer Mac OS
program compiler

Executable (Mac)

UNIX
compiler

Executable (UNIX)
13

A High Level View Of Translating/Executing Java


Programs
Stage 1:
Compilation

Filename.java Java compiler Filename.class


(javac)
Java
Java program bytecode
(generic
binary)
14

A High Level View Of Translating/Executing Java


Programs (2)

Stage 2: Interpreting and executing the byte


code

Machine language
instruction (UNIX)

Filename.class Java interpreter Machine language


(java) instruction (Windows)
Java
bytecode
(generic
binary) Machine language
instruction (Apple)
15

Smallest Compliable And Executable Java Program

The name of the online example is: Smallest.java (Important note: file
name matches the word after the keyword ‘class’)

public class Smallest


{
public static void main (String[] args)
{
}
}
16

Important Note

• Each Java instruction must be followed by a semi-colon!

General format Examples


Instruction1; int num = 0;
Instruction2; System.out.println(num);
Instruction3; : :
: :
17

Java Output

•Format:
System.out.print(<string or variable name one> + <string or variable name
two>..);
OR
System.out.println(<string or variable name one> + <string or variable
name two>..);
•Examples (online program called “OutputExample1.java”)
public class OutputExample1
{
public static void main (String [] args)
{
int num = 123; // More on this shortly
System.out.println("Good-night gracie!");
System.out.print(num);
System.out.println("num="+num);
}
}
18

Output : Some Escape Sequences For Formatting


Escape sequence Description

\t Horizontal tab

\r Carriage return

\n New line

\” Double quote

\\ Backslash
19

Example Formatting Codes


• Name of the online example: FormattingExample.java

public class FormattingExample


{
public static void main (String [] args)
{
System.out.print("lol\tz\n");
System.out.println("hello\rworld");
System.out.println("\"Geek\" talk slash (\\) com");
}
}
20

Variables
• Unlike Python variables must be declared before they can be used.
• Variable declaration:
o Creates a variable in memory.
o Specify the name of the variable as well as the type of information that it
will store.
o E.g. int num;
o Although requiring variables to be explicitly declared appears to be an
unnecessary chore it can actually be useful for minimizing insidious logic
errors.
• Using variables
o Only after a variable has been declared can it be used.
o E.g., num = 12;
21

Declaring Variables: Syntax

• Format:
<type of information> <name of variable>;

• Example:
char myFirstInitial;

• Variables can be initialized (set to a starting value) as they’re


declared:
char myFirstInitial = ‘j’;
int age = 30;
22

Some Built-In Types of Variables In Java


Type Description
byte 8 bit signed integer

short 16 but signed integer

int 32 bit signed integer

long 64 bit signed integer

float 32 bit signed real number

double 64 bit signed real number

char 16 bit Unicode character (ASCII and


beyond)
boolean 1 bit true or false value

String A sequence of characters between double


quotes ("")
23

Location of Variable Declarations

public class <name of class>


{
public static void main (String[] args)
{
// Local variable declarations occur here

<< Program statements >>


: :

}
}
24

Java Keywords

abstract boolean break byte case catch char

class const continue default do double else

extends final finally float for goto if

implements import instanceof int interface long native

new package private protected public return short

static super switch synchronized this throw throws

transient try void volatile while


25

Common Java Operators / Operator Precedence


Precedence Operator Description Associativity
level

1 expression++ Post-increment Right to left


expression-- Post-decrement

2 ++expression Pre-increment Right to left


--expression Pre-decrement
+ Unary plus
- Unary minus
! Logical negation
~ Bitwise complement
(type) Cast
26

Common Java Operators / Operator Precedence


Precedence Operator Description Associativity
level

3 * Multiplication Left to right


/ Division
% Remainder/modulus
4 + Addition or String Left to right
concatenation
- Subtraction
5 << Left bitwise shift Left to right
>> Right bitwise shift
27

Common Java Operators / Operator Precedence


Precedence Operator Description Associativity
level

6 < Less than Left to right


<= Less than, equal to
> Greater than
>= Greater than, equal to
7 == Equal to Left to right
!= Not equal to
8 & Bitwise AND Left to right

9 ^ Bitwise exclusive OR Left to right


28

Common Java Operators / Operator Precedence


Precedence Operator Description Associativity
level
10 | Bitwise OR Left to right

11 && Logical AND Left to right

12 || Logical OR Left to right


29

Post/Pre Operators
The name of the online example is: Order1.java
public class Order1
{
public static void main (String [] args)
{
int num = 5;
System.out.println(num);
num++;
System.out.println(num);
++num;
System.out.println(num);
System.out.println(++num);
System.out.println(num++);
}
}
30

Common Java Operators / Operator Precedence


Precedence Operator Description Associativity
level

13 = Assignment Right to left


+= Add, assignment
-= Subtract, assignment
*= Multiply, assignment
/= Division, assignment
%= Remainder, assignment
&= Bitwise AND, assignment
^= Bitwise XOR, assignment
|= Bitwise OR, assignment
<<= Left shift, assignment
>>= Right shift, assignment
31

Post/Pre Operators (2)


The name of the online example is: Order2.java

public class Order2


{
public static void main (String [] args)
{
int num1;
int num2;
num1 = 5;
num2 = ++num1 * num1++;
System.out.println("num1=" + num1);
System.out.println("num2=" + num2);
}
}
32

Unary Operator/Order/Associativity

The name of the online example: Unary_Order3.java

public class Unary_Order3.java


{
public static void main (String [] args)
{
int num = 5;
float fl;
System.out.println(num);
num = num * -num;
System.out.println(num);
}
}
33

Decision Making In Java


• Java decision making constructs
o if
o if, else
o if, else-if
o switch
34

Decision Making: Logical Operators


Logical Operation Python Java

AND and &&

OR or ||

NOT not, ! !
35

Decision Making: If
• Indenting the body of
the branch is an
Format: important stylistic
if (Boolean Expression) requirement of Java
Body but unlike Python it is
not enforced by the
Example: syntax of the
if (x != y) language.
System.out.println("X and Y are not equal");
• What distinguishes the
if ((x > 0) && (y > 0)) body is either:
{
1.A semi colon (single
System.out.println("X and Y are positive");
statement branch)
}
2.Braces (a body that
consists of multiple
statements)
36

Decision Making: If, Else

Format:
if (Boolean expression)
Body of if
else
Body of else

Example:
if (x < 0)
System.out.println("X is negative");
else
System.out.println("X is non-negative");
37

Example Program: If-Else


• Name of the online example: BranchingExample1.java
import java.util.Scanner;
public class BranchingExample1
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
final int WINNING_NUMBER = 131313;
int playerNumber = -1;
System.out.print("Enter ticket number: ");
playerNumber = in.nextInt();
if (playerNumber == WINNING_NUMBER)
System.out.println("You're a winner!");
else
System.out.println("Try again.");
}
}
38

If, Else-If

Format:
if (Boolean expression)
Body of if
else if (Boolean expression)
Body of first else-if
: : :
else if (Boolean expression)
Body of last else-if
else
Body of else
39

If, Else-If (2)


Name of the online example: BranchingExample.java

import java.util.Scanner;

public class BranchingExample2


{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
int gpa = -1;
System.out.print("Enter letter grade: ");
gpa = in.nextInt();}
40

If, Else-If (3)


if (gpa == 4)
System.out.println("A");
else if (gpa == 3)
System.out.println("B");
else if (gpa == 2)
System.out.println("C");
else if (gpa == 1)
System.out.println("D");
else if (gpa == 0)
System.out.println("F");
else
System.out.println("Invalid letter grade");
}
}
41

Reference

Java™: The Complete Reference,. Seventh


Edition. Herbert Schildt. New York .

You might also like