Download
some quick reviews of java n.
Skip this Video
Loading SlideShow in 5 Seconds..
Some Quick Reviews of Java PowerPoint Presentation
Download Presentation
Some Quick Reviews of Java

Some Quick Reviews of Java

226 Vues Download Presentation
Télécharger la présentation

Some Quick Reviews of Java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Some Quick Reviews of Java

  2. Background • Java was developed in the early 90s by Sun Microsystems • Java is a high-level language • Java programs are portable across platforms • Each program is translated into Java byte-code • Each machine has a Java Virtual Machine (JVM) which knows how to execute Java byte-code. • Can be either stand-alone programs or applets. • Java is object-oriented • Suggested IDE (Eclipse, Jbuilder, Visual Java, etc.)

  3. Java and C/C++ Include existing codes or libraries C/C++ java import package1.sub-package.classname or import package.sub-package.* #include <…> or #include “…”

  4. Java and C/C++ Screen I/O streams C java #include <stdio.h> printf(“<formatting>”, var1, var2…); scanf(“<formatting>”, &var1, &var2…); System.out.printf(“<formatting>”, var1, var2,…); System.out.print(String…); //in same line System.out.println(String…); //next line import java.util.Scanner//for input Scanner keyboard = new Scanner(System.in); int var1 = keyboard.nextInt(); double var2 = keyboard.nextDouble(); String s1 = keyboard.next(); // read one word String s2 = keyboard.nextline(); //read one line C++ #include <iostream> cout << <constant string | varables> << endl; cin >> variable

  5. Java and C/C++ What are similar or same • Basic types, e.g. int, float, double, char, … automatic type conversion • byteshortintlongfloatdouble • char • Basic operations, e.g. “+”, “-”, “*”, “/”, “++”, “--”, “>”, “<“, “==”, “!=“… • Flow controls • if-else statement, if - else if - else statement • for loops • while loops, do-while loops • switch-case statement

  6. Java and C/C++ What are similar or same • Basic types, e.g. int, float, double, char, …automatic type conversion • Basic operations, e.g. “+”, “-”, “*”, “/”, “++”, “--”, “>”, “<“, “==”, “!=“… • Flow controls • if-else statement, if - else if - else statement • for loops • while loops, do-while loops • switch-case statement Java does not have pointers!

  7. Java and C/C++ execution entrance C/C++ java • import package.class • … • public classclass_name/* must have the same name • as the file*/ • { • public void main(<argument list>) • { • …… • } • } #include <stdio.h> void main (<argument list>) { …… }

  8. Java and C/C++ Local and global variables In C/C++, the variables defined in a function or other block enclosed by a pair of braces “{ }” are local. The variables declared outside any methods and classes are global. In Java, all variables are local. There are NO global variables, i.e. all the variables have to be declared within a method/ function or a class. Also, the variables declared in a block “{ }” cannot be repeated in the same function.

  9. Java and C/C++ Local and global variables In C/C++, the variables defined in a function or other block enclosed by a pair of braces “{ }” are local. The variables declared outside any methods and classes are global. In Java, all variables are local. There are NO global variables, i.e. all the variables have to be declared within a method/function or a class. Also, the variables declared in a block “{ }” cannot be repeated in the same function. void func() { { inti; … } inti; //OK …… } public void func() { { inti; … } inti; //illegal! …… }

  10. Class in Java

  11. What we have learned • Classes are the most important language feature that make object-oriented programming (OOP) possible. • Combination of both attributes and behaviors • Can be used to generate many objects with the same behaviors • Information hidden and encapsulation

  12. Similar in Java, yet different • Classes are the most important language feature that make object-oriented programming (OOP) possible. • Combination of both attributes and behaviors • Can be used to generate many objects with the same behaviors • Information hidden and encapsulation • Programming in Java consists of defining a number of classes • Every program is a class • All helping software consists of classes • All programmer-defined types are classes • Classes are central to Java

  13. How to define a class in Java? How did we do that in C++?

  14. How to define a class in Java? How did we do that in C++? class class_name { public: //member func. … private: // attribute list };

  15. How to define a class in Java? How did we do that in C++? How should we do that in Java? class class_name { public: //member func. … private: // attribute list };

  16. How to define a class in Java? How did we do that in C++? How should we do that in Java? public class class_name { public member func1 public member func2 … private member var1 private member var2 … } class class_name { public: //member func. … private: // attribute list }; No semi-column!

  17. One Example Recall the day of the year example: This is what we did in C++ How should we implement it in java? class DayOfYear { public: DayOfYear(); DayOfYear(intmonthVal, intdayVal); void input(); void output(); private: int month, day; };

  18. One Example Recall the day of the year example: This is what we did in C++ How should we implement it in java? public class DayOfYear { public DayOfYear() {} public DayOfYear(intmonthVal, intdayVal) {} public void input() {} public void output() {} private int month, day; } class DayOfYear { public: DayOfYear(); DayOfYear(intmonthVal, intdayVal); void input(); void output(); private: int month, day; };

  19. One Example How do we implement the member functions? This is what we did in C++ Likely in a different file. This is how we implement it in java. public class DayOfYear { public DayOfYear() {} public DayOfYear(intmonthVal, intdayVal) {} public void input() {} public void output() {} private int month, day; } DayOfYear::DayOfYear(){} DayOfYear::DayOfYear(intmonthVal, intdayVal) :month(monthVal),day(dayVal) { } void DayOfYear::input() { …… } void DayOfYear::output() { }

  20. class in java need to specify the modifier of the class • public class DayOfYear • { • private int month, day; • public DayOfYear() • { month = 0; day = 0;} • public DayOfYear(intmonthVal, intdayVal) • { month = monthVal; • day = dayVal; • } • public void input(intmVal, intdVal) • { month = mVal; • day = dVal; • } • public void output() • {System.out.print(“month=“+month + “; ”); • System.out.println(“day=“+day); • } • }

  21. class in java need to specify the modifier of the class • public class DayOfYear • { • private int month, day; • publicDayOfYear() • { month = 0; day = 0;} • publicDayOfYear(intmonthVal, intdayVal) • { month = monthVal; • day = dayVal; • } • publicvoid input(intmVal, intdVal) • { month = mVal; • day = dVal; • } • publicvoid output() • {System.out.print(“month=“+month + “; ”); • System.out.println(“day=“+day); • } • } Each member function needs a modifier

  22. class in java need to specify the modifier of the class • public class DayOfYear • { • private int month, day; • publicDayOfYear() • { month = 0; day = 0;} • publicDayOfYear(intmonthVal, intdayVal) • { month = monthVal; • day = dayVal; • } • publicvoid input(intmVal, intdVal) • { month = mVal; • day = dVal; • } • publicvoid output() • {System.out.print(“month=“+month + “; ”); • System.out.println(“day=“+day); • } • } Each member function needs a modifier Declaration and definition in one place, e.g. in a ‘.java’ file that has same name as the class.

  23. Create Objects • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • } • } Use newoperator! invoke constructor!

  24. Create Objects • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • } • } Use newoperator! invoke constructor! Note that this is different from C++! DayOfYeardate1(2, 25); DayOfYear*date2 = newDayofYear();

  25. Constructors Very similar to C++, you can have multiple overloaded constructors • public class DayOfYear • { • public DayOfYear() • { month = day = 1; • } • public DayOfYear(intmonthVal, intdayVal) • { month = monthVal; day = dayVal; • } • public DayOfYear(int month) • { • this.month = month; day = 1; • } • public void input() • {} • public void output() • {} • private int month, day; • } no argument constructor overloaded constructor

  26. Alternative Way to Initialize Instance Variables You can initialize instance variables when you declare them in a class definition as follows This is much different from C++!

  27. Exercise Given the following definition public class YourClass { private int information; private char moreInformation; public YourClass( intnewInfo, char moreNewInfo) { <Details not shown.> } public YourClass() {<Details not shown.> } public void doStuff() { <Details not shown.> } } Which of the following are legal? YourClassanObject = new YourClass(42, 'A'); YourClassanotherObject = new YourClass(41.99, 'A'); YourClassyetAnotherObject = new YourClass(); yetAnotherObject.doStuff(); YourClassoneMoreObject; oneMoreObject.doStuff(); oneMoreObject.YourClass(99, 'B');

  28. Exercise Given the following definition public class YourClass { private int information; private char moreInformation; public YourClass( intnewInfo, char moreNewInfo) { <Details not shown.> } public YourClass() {<Details not shown.> } public void doStuff() { <Details not shown.> } } Which of the following are legal? YourClassanObject = new YourClass(42, 'A'); YourClassanotherObject = new YourClass(41.99, 'A'); YourClassyetAnotherObject = new YourClass(); yetAnotherObject.doStuff(); YourClassoneMoreObject; oneMoreObject.doStuff(); oneMoreObject.YourClass(99, 'B');

  29. Access Members • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • date1.month = 3; • date1.day = 1; • date1.output(); • date2.output(); • } • } Using dot operator ‘.’ to access members of an object. Any problems of this code?

  30. Access Members • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • date1.month = 3; //illegal • date1.day = 1; //illegal • date1.output(); //legal • date2.output(); //legal • } • } Using dot operator ‘.’ to access members of an object. Any problems of this code?

  31. Access Members • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • date1.month = 3; //illegal • date1.day = 1; //illegal • date1.output(); //legal • date2.output(); //legal • } • } How to fix it?

  32. Access Members • public class DayOfYearDemo • { • publicvoid main() • { • DayOfYear date1, date2; //declaration • date1 = newDayofYear(); //creates an object • date2 = newDayofYear(2, 25); • …… • date1.month = 3; //illegal • date1.day = 1; //illegal • date1.output(); //legal • date2.output(); //legal • } • } How to fix it? Use accessor and mutators to access and modify the private member variables!

  33. Static Members Static methods In Java, you can define a method so that it requires no calling object. Such methods are known as static methods. Still defined in a class! Let us assume the above static function is defined in a class named ‘ABC’, the way to invoke this static function is as follows int budget = ABC.maximum (yourMoney, myMoney);

  34. Static Members Static methods In Java, you can define a method so that it requires no calling object. Such methods are known as static methods. Still defined in a class! Let us assume the above static function is defined in a class named ‘ABC’, the way to invoke this static function is as follows int budget = ABC.maximum (yourMoney, myMoney); You cannot invoke a non-static function within a static function!

  35. Static Members Static variables A static variable is a variable that belongs to the class as a whole and not just to one object. private staticint turn; Still defined in a class! or private staticint turn = 0; • If you do not initialize a static variable, it is automatically initialized to a default value: • boolean – false • int, float, double, char, short, long – zero • class type – null

  36. The methods equals and toString • Java expects certain methods, such as equalsand toString, to be in all, or almost all, classes • The purpose of equals, a boolean valued method, is to compare two objects of the class to see if they satisfy the notion of "being equal" • Note: You cannot use == to compare objects public boolean equals(ClassNameobjectName) • The purpose of the toString method is to return a String value that represents the data in the object public String toString()

  37. Example

  38. Example (continued) What does it output?

  39. Example (continued)

  40. Does Java support operator overloading?

  41. Does Java support operator overloading? NO! It does not. You cannot overload the operator for your complex objects. However, ‘+’ is overloaded for String type of objects.

  42. Does Java support overloading functions at all?

  43. Does Java support overloading functions at all? Yes! We have seen the overloading constructors in the previous example!

  44. What is the signature of a method?

  45. What is the signature of a method? method name + argument list

  46. Are the following two overloading functions? public void setDate (int year) { …… } public booleansetDate (int month) { return true; }

  47. Are the following two overloading functions? public void setDate (int year) { …… } public booleansetDate (int month) { return true; } They are not! because they have exactly the same signature!

  48. Java Inheritance

  49. What do we know already? • Inheritance is one of the main techniques of object-oriented programming (OOP). • It enables potentially infinite reuse of resources. • It can be used to enhance and improve the previous programs for the changing requirements