Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

An Introduction to Java Programming Language

An introduction to Java programming language(for C programmers)
by

sungwon jeong

on 9 July 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of An Introduction to Java Programming Language

Platform
Independent
Object Oriented
Programming
Language
Garbage
Collection
High Performance
is
James Gosling
http://download.oracle.com/javase/tutorial/getStarted/intro/definition.html
http://en.wikipedia.org/wiki/James_Gosling
pass by value
pass by reference
http://stackoverflow.com/questions/40480/is-java-pass-by-reference
http://javadude.com/articles/passbyvalue.htm
public void foo(Dog d) {
d.name == "Max"; // true
d = new Dog("Fifi");
d.name == "Fifi"; // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Max"; // true
public void foo(Dog d) {
d.name == "Max"; // true
d.setname("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Fifi"; // true
But it's different
class Foo {
public static void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
public static void swap2(MyClass c1, MyClass c2) {
MyClass t = c1;
c1 = c2;
c2 = t;
}
public static void swap3(MyClass c1, MyClass c2) {
int t = c1.a;
c1.a = c2.a;
c2.a = t;
}
}
public class test {
public static void main(String[] args) {
int a=10, b=50;
Foo.swap1(a, b);
System.out.println( a +" "+ b );

MyClass c1 = new MyClass(10);
MyClass c2 = new MyClass(50);
Foo.swap2(c1, c2);
System.out.println( c1.a +" "+ c2.a );

MyClass c3 = new MyClass(10);
MyClass c4 = new MyClass(50);
Foo.swap3(c3, c4);
System.out.println( c3.a +" "+ c4.a );
}
}
Method Overloading
public class TestClass {
public static void main(String[] args) {
System.out.println(1);
System.out.println(1.0);
System.out.println("string");
System.out.println(false);
}
}
public class TestClass {
void printInt(int n);
void printFloat(float f);
void printDouble(double d);
void printIntandDouble(int n, double d);

void printVal(int n);
void printVal(float f);
void printVal(double d);
void printVal(int n, double d);

public static void main(String[] args) {
int a;
float b;
double c;

printInt( a );
printFloat( b );
printDouble( c );
printIntandDouble( a, c );

printVal( a );
printVal( b );
printVal( c );
printVal( a, c );
}
}
public class TestClass {
int myFunction( int n );
float myFunction( int n );
double myFunction( int n );

public static void main(String[] args) {
int a;
float b;
double c;

myFunction( (int)5 );
}
}
constructor
(Java has no destructor)
Car c = new car();
c.color = "white";
c.gearType = "auto"
c.door = 4;
Car c = new Car("white", "auto", 4);
When?
0. for convenience
1. have to be initialized
2. to check validity
class Car {
Car(String color, String gear, int door) {
this.color = color;
this.gear = gear;
if ( door>4 ) ERROR;
this.door=door;
}
}
C, C++
SmallTalk, Objective-C
Java
OOP
Interface
Multiple inheritance is only possible by using interface. Why?
Interface only contains "What to do"
not "How to do"
Encapulation
[Benefits]
•The fields of a class can be made read-only or write-only.
•A class can have total control over what is stored in its fields.
•The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.
Inheritance
public interface Animal {}

public class Mammal implements Animal{
}

public class Dog extends Mammal{
}
public class Vehicle{}

public class Speed{}

public class Van extends Vehicle{
private Speed sp;
}
no multiple inheritance
public class Dog extends Animal, Mammal{}
Overriding
class Animal{

public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal{

public void move(){
System.out.println("Dogs can walk and run");
}
}

public class TestDog{

public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();// runs the method in Animal class

b.move();//Runs the method in Dog class
}
}
class Animal{

public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal{

public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}

}

public class TestDog{

public static void main(String args[]){

Animal b = new Dog(); // Animal reference but Dog object
b.move();//Runs the method in Dog class

}
}
Abstraction
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
Packages
Write Once, Run Anywhere
if
for
int
while
+=
-=
float
double
!=
==
&&
break
continue
do-while
||
&
comment style
// comment
/* comment */
+
-
*
/
++
--
String s1 = new String("abcd");
String s2 = new String("1234");

String s3 =s1;
s3 += s2;
int length = s3.length();
Regular Expressions
Collections
Exceptions
import java.util.regex.Matcher;
import java.util.regex.Pattern;
HashMap, HashSet
LinkedList, ArrayList
try, catch, finally
public static void main(String[] args)
{
outer:
for( int i=0; i<10; i++ )
{
for( int j=10; j>0; j--)
{
if( j == 5 ) break outer; // exit entire loop
}
}
}
for (int i=0 ; i<10 ; i++)
{
for (int j=0 ; j<10 ; j++)
{
for (int k=0 ; k<10 ; k++)
{
if ( 5==k ) goto here;
}
}
}
here:
http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Popular Language!
Popular
Similar to
C syntax
Call by
value
Father
Only in Java
Only in C
String
class
garbage
collection
for (int i=0 ; i<10 ; i++)
int main(void) {
MyClass *a = new MyClass();
a->doSomething();

delete a; // must in C++ (to avoid memory leak)
}
void main(String[] args) {
MyClass a = new Myclass();
a.doSomething();

// don't need to do free, delete....
}
C++
Java
struct
global
variable
pointer
typedef
goto
Unit
Marine
Ground
Tank
Air
Mutalisk
Scout
Full transcript