Overview of Java: Dr. Turkan Ahmed Khaleel
Overview of Java: Dr. Turkan Ahmed Khaleel
What is java?
Based on C/C++
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 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
http://www.eclipse.org/
9
Java Features
• Interpreted
o java compiler generate byte-codes, not native machine code
• Portable
o same application runs on all platforms
o the sizes of the primitive data types are always the same
Java Features
• Reliable
o extensive compile-time and runtime error checking
• Secure
o usage in networked environments requires more security
Java Features
• Multithreaded
o multiple concurrent threads of executions can run simultaneously
• Dynamic
o java is designed to adapt to evolving environment
Windows
compiler
Executable (Windows)
Computer Mac OS
program compiler
Executable (Mac)
UNIX
compiler
Executable (UNIX)
13
Machine language
instruction (UNIX)
The name of the online example is: Smallest.java (Important note: file
name matches the word after the keyword ‘class’)
Important Note
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
\t Horizontal tab
\r Carriage return
\n New line
\” Double quote
\\ Backslash
19
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
• Format:
<type of information> <name of variable>;
• Example:
char myFirstInitial;
}
}
24
Java Keywords
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
Unary Operator/Order/Associativity
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
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
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
import java.util.Scanner;
Reference