You are on page 1of 26

Introduction to Java

TTI 2014

Contact Info
Joseph Vinisky
Blog: codeny.blogspot.com
Email: codenyx@gmail.com

Any Programming Experience?

Course Topics

Introduction to Java
Fundamentals
Objects and Classes
Inheritance
Interfaces and Inner Classes
Deploying Applications
Debugging and Exceptions
Multithreading

Week 1

Introduction
What is Java?
Installing the Java SDK and Eclipse IDE
Language Fundamentals

History of Java
Began as a Sun Microsystems project called
Green
James Gosling

Intended to be used on a variety of architectures


All code is translated to the same Virtual
Machine code, and specific interpreters are
written for the VM
Chose to make it object-oriented like C++ instead
of like Pascal
First commercial application: applets (1995)

Javas Evolution

Java 1.0 First release


Java 1.1 Inner classes
Java 1.2-1.3 (no additions)
Java 1.4 Assertions
Java 5.0 [1.5] Generic classes, for each loops, variable
arguments, autoboxing, metadata, enums, static import
Java 6 Performance improvements, library enhancements
update 37
Java 7 More security and library enhancements update 9
Java 8 TDA September 2013

Versions of Java
Java SE Standard Edition
Java ME Micro Edition embedded devices
or resource constrained devices set top
boxes, blu-ray players, mobile devices
Java EE Enterprise Edition For server side
processing

Uses of Java
Write Once, Run Anywhere
Stand alone applications
Applets (java code embedded into webpages run
via we browser)
Servlets (server side Java code that interact with
clients typically using HTTP)

Android development

Programming Languages
Interpreted languages
Perl
Python
PHP

Compiled languages
BASIC
C/C++
Fortran
Java (to bytecode)

JVM Approach
Architecture neutral
Only need an implementation of JVM for the native
machine
Same Java code will run on all platforms

Portable
The results on x86 = results on ARM = results on PPC
Caveat: dont always fully utilize architecture capabilities

Object oriented
Everything is a class

Doesnt this all mean Java is slow?


On average: slower than compiled languages
But, using just-in-time (JIT) compiler Java is fast!

Grabbing Java and Eclipse


1. Go to
http://www.oracle.com/technetwork/java/javase/do
wnloads/index.html
2. Download Java SE 6 Update 37 JDK (includes the JRE)
3. Install the JDK
4. Install the JRE
5. Download Eclipse from:
http://www.eclipse.org/downloads/packages/eclipse-idejava-developers/heliossr2

6. Continue with installing Eclipse IDE

Hello World Example


Simplest possible program: prints one line
Will re-visit this program later
public class HiWorld {
public static void main(String[] args) {
System.out.println(Hello World);
}
}

Another Example
Uses an array of three strings and a loop
public class Greetings {
public static void main(String[] args) {
String[] greeting = new String[3];
greeting[0] = "Welcome to BNAI ZION";
greeting[1] = Introduction to Java";
greeting[2] = Spring 2012 ";
for(String thisline : greeting)
System.out.println(thisline);
}
}

Hello World In-depth


public class HiWorld {
public static void main(String[] args) {
// this is a comment.
/* so is this, but the following is
a statement: */
System.out.println(Hello World);
}
}
Java is case sensitive
public is an access modifier
Controls level of access other part of program have to this code
Everything in Java is a class used to create building blocks
System.out is an object, calling its println method with parameter Hello World

Class
Class is a container for the program logic that
defines the behavior of an application
Building blocks with which all Java applications
and applets are built.
Everything in a Java program must be inside a
class.
Following the keyword class is the name of the
class.
Names must begin with a letter, and after that,
they can have any combination of letters and
digits.

Simple Template with Javadoc


/**
* This is a simple template, documented.
* @version 0.01
* @author Your Name
*/
public class ClassName{
public static void main(String[] args){
program statements;
}
}

The 8 Primitive Data Types in Java

Variables
For any meaningful program you need to modify data
Variables are used to store values
Operators operate on one or two variables
Forming expressions
Declare a variable called Name of type type:
type Name;
Example:
String name;
int a, b;

Assigning Name a value val:


type Name=val;
Example:
String name=Don Knuth;
int a=3;
float k=3.3;

Integer Types
Range depends on size of each type:
long (8 bytes) -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
int (4 bytes) -2,147,483,648 to 2,147,483,647
short (2 bytes) -32,768 to 32,767
byte (1 byte) -128 to 127

All integer types are signed (the unsigned


keyword does not exist in Java)
All integer types are the same size regardless of
the devices architecture

Representing Floating Point


Used for positive and negative numbers with
fractional parts
Range and precision both depend on type
float (4 bytes) 3.40282347*1038
double (8 bytes) 1.79769313486231570*10308

Float stores up to 7 fractional digits


Double stores 15 decimal digits
In general, doubles should be used instead of Float
If speed or memory are constrained, floats may be
necessary

Representing Characters
Unlike C, where char is almost always a single
byte, a Java char can hold a multi-byte Unicode
character
Every char is 16 bits (2 bytes), and stores either a
complete character of Unicode U+0000 to U+FFFF
or half of a U+10000 to U+10FFFF character
In most cases, String variable should be used to
avoid having to worry about character types and
lengths.
String pi = "\u03C0;

//

Boolean Types
Can only indicate two values , true or false
Unlike C/C++, integer 0 and 1 are not equivalent to
false and true
Avoid easy-to-create bugs. For example:
if((x=1)) {
statement;
} //this would not compile in Java
Must use true and false when assigning boolean
variables
No implicit conversion is possible between boolean
and other data types

Strings

Java does NOT have a built-in string type


Standard library contains class called String
Every quoted string is an instance of this class
Java strings are sequence of Unicode
characters
Example: Java\u2122 consists of: J,a,v,a,
Example: String e = ; //an empty string
String planet = Earth;

More later with String API

Enumerated Types

Sometimes variable should only hold a value


from specific (restricted) set
Example:
Shirt size allowed to be small, medium, large

You can, of course do:


int SMALL=1;
int MEDIUM=2;
int LARGE=3;
int shirtSize=one-of-the-above;

But nothing prevents one from setting shirtSize=-1;


Solution: enums:
Enum Size {SMALL, MEDIUM, LARGE};

Size shirtSize=Size.one-of-the-above;

You might also like