SYDE223 Tutorial 3: Introduction to Java Annie En-Shiun Lee January 19, 2010
Outline 1.Java v.s. C++ 2.Language Basics 3.HelloWorld example 4.Introduction to OOP
1. Java v.s. C++
Java v.s. C++ Smaller/easier Object Oriented Programming (OOP) Single inheritance Platform independence
Java v.s. C++ Platform independence
Java v.s. C++ Java Virtual Machine No Pointers or Templates Extensive Library Simpler String and Data Structures
2. Language Basics
Variables Scope & Types – Instance variable (non static) – Class variable (static fields) static int numGears = 6; – Local variable – Parameter public static void main(String[] args)
Variables Primitive data – Byte – Short – Int – Long – Float – Double – Boolean – Char – NOTE: String is an “object”
Operators Assignment – Operand = value – Ex. int i = 0 Arithmetic: +, -, *, /, % (remainder) – Math – 2 operands
Operators Unary: +, -, ++, --, ! – One operand act on one variable – Ex. Int i = 1 i = -i i++;
Operators Equality – == – != – > – >= – < – <= conditional operators – && – ||
Operators int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1");
If-then, if-else-then int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); }
Switch int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break;
While while (expression) { statement(s) } int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; }
Do-While do{ statement(s) } while (expression) int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11);
For for (initialization; termination; increment){ statement(s) } for(int i=1; i<11; i++){{ System.out.println("Count is: " + count); }
3. Hello World Example
“Main” method Each executable Java application has a “main” method. Using the keyword “private” one can restrict the access to methods to the classes within the same directory “void” means the method returns no object
4. Intro to OOP
Object Oriented Programming object has 2 characteristics – state, – behaviour Ex. Dog – State – name, color, breed, mood Mood = sad, happy, angry – Behaviour – barking, wagging tail, whining Ex. Java – State – Object store fields – Behaviour – methods
Object Oriented Programming Advantages – Modularity – Information-hiding – Code re-use – Pluggability and debugging ease
Objects Instance of the class of objects Ex. Professor Tizhoosh’s dog is named “alpha” Ex. Annie the TA’s dog is named “beta” Ex. Student A’s dog is named “delta”
Inheritance A class inherit commonly used state and behaviour from other class – Ex. Dog small dog, big dog class SmallDog extends Dog { // new fields and methods defining a Dog would go here }
Inheritance A class inherit commonly used state and behaviour from other class – Ex. Dog small dog, big dog class SmallDog extends Dog { // new fields and methods defining a Dog would go here class SmallDog extends Dog { // new fields and methods defining a Dog would go here } }