This shows you the differences between two versions of the page.
|
java:wrapper_classes [2017/01/30 10:50] gthanos [Autoboxing και Unboxing] |
java:wrapper_classes [2022/02/24 12:45] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Βασικοί τύποι δεδομένων και ισοδύναμοι αναφορικοί τύποι ====== | ||
| - | |||
| - | Στην εισαγωγική [[java: | ||
| - | |||
| - | ^ Βασικός τύπος ^ Αναφορικός τύπος ^ | ||
| - | | boolean | [[http:// | ||
| - | | byte | [[http:// | ||
| - | | char | [[http:// | ||
| - | | int | [[http:// | ||
| - | | long | [[http:// | ||
| - | | short | [[http:// | ||
| - | | float | [[http:// | ||
| - | | double | ||
| - | |||
| - | Κάθε αναφορικός τύπος μπορεί να δημιουργηθεί από ένα βασικό τύπο ή από ένα αλφαριθμητικό ([[http:// | ||
| - | |||
| - | <code java> | ||
| - | Integer(int value); | ||
| - | // | ||
| - | Integer(String s); | ||
| - | // | ||
| - | </ | ||
| - | |||
| - | Eπίσης, | ||
| - | <code java> | ||
| - | byte byteValue(); | ||
| - | //Returns the value of this Integer as a byte. | ||
| - | short shortValue(); | ||
| - | //Returns the value of this Integer as a short. | ||
| - | int intValue(); | ||
| - | //Returns the value of this Integer as an int. | ||
| - | long longValue(); | ||
| - | //Returns the value of this Integer as a long. | ||
| - | float floatValue(); | ||
| - | //Returns the value of this Integer as a float. | ||
| - | </ | ||
| - | |||
| - | Κάθε κλάση στην Java περιέχει την μέθοδο [[java: | ||
| - | |||
| - | Εκτός από τα παραπάνω, | ||
| - | <code java> | ||
| - | static Integer decode(String nm) | ||
| - | //Decodes a String into an Integer. Accepts decimal, hexadecimal, | ||
| - | static int highestOneBit(int i) | ||
| - | //Returns an int value with at most a single one-bit, in the position of the highest-order (" | ||
| - | int lowestOneBit(int i) | ||
| - | //Returns an int value with at most a single one-bit, in the position of the lowest-order (" | ||
| - | static int parseInt(String s) | ||
| - | //Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ' | ||
| - | static int reverse(int i) | ||
| - | //Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value. | ||
| - | |||
| - | κλπ. | ||
| - | </ | ||
| - | |||
| - | ===== Auto-boxing και Auto-unboxing ===== | ||
| - | |||
| - | Ο compiler της Java μας δίνει την δυνατότητα να χρησιμοποιήσουμε βασικούς τύπους σε σημεία του κώδικα που απαιτείται η ισοδύναμη αναφορική μορφή ή αναφορικούς τύπους σε σημεία του κώδικα που ζητείται η βασική μορφή. Ο compiler έχει την ιδιότητα να αναγνωρίζει σημεία του κώδικα που απαιτούν τις παραπάνω μετατροπές τύπου και κάνει αυτόματα τις μετατροπές αυτές. Η ιδιότητα αυτή του Java compiler απαντάται στην βιβλιογραφεία ως Auto-boxing και Auto-unboxing. Θα δώσουμε παρακάτω από ένα παράδειγμα μετατροπής πάνω στο οποίο θα συζητήσουμε τις διαδικασίες Auto-boxing και Auto-unboxing. | ||
| - | |||
| - | ==== Autoboxing ==== | ||
| - | |||
| - | <code java AutoboxExample.java> | ||
| - | public class AutoboxExample { | ||
| - | public static void main(String []args) { | ||
| - | int a=5; | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Ο παραπάνω κώδικας εκτυπώνει την τιμή της μεταβλητής //a// που τυγχάνει να είναι βασικού τύπου. Για να το επιτύχει αυτό καλείται να εκτυπώσει ένα αλφαριθμητικό (// | ||
| - | |||
| - | Η παραπάνω αυτόματη μετατροπή που κάνει ο compiler, μπορεί να γραφεί ισοδύναμα ως εξής: | ||
| - | |||
| - | <code java AutoboxExample.java> | ||
| - | public class AutoboxExample { | ||
| - | public static void main(String []args) { | ||
| - | int a=5; | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Οι δύο παραπάνω κλάσεις είναι ισοδύναμες. Η αιτία της ισοδυναμίας είναι η ιδιότητα του compiler να αναγνωρίζει την ανάγκη μετατροπής ενός βασικού τύπου σε αναφορικό. | ||
| - | |||
| - | <WRAP tip 80% center round> | ||
| - | Στο συγκεκριμένο παράδειγμα, | ||
| - | </ | ||
| - | |||
| - | ==== Unboxing ==== | ||
| - | |||
| - | <code java UnboxExample.java> | ||
| - | public class UnboxExample { | ||
| - | public static int sum(int a, int b) { | ||
| - | | ||
| - | } | ||
| - | | ||
| - | public static void main(String []args) { | ||
| - | Integer a = new Integer(5); | ||
| - | Integer b = new Integer(10); | ||
| - | int result = sum(a,b); | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Στο παραπάνω παράδειγμα, | ||
| - | |||
| - | <code java UnboxExample.java> | ||
| - | public class UnboxExample { | ||
| - | public static int sum(int a, int b) { | ||
| - | | ||
| - | } | ||
| - | | ||
| - | public static void main(String []args) { | ||
| - | Integer a = new Integer(5); | ||
| - | Integer b = new Integer(10); | ||
| - | int result = sum(a.intValue(), | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Ο compiler έχει την δυνατότητα να μετατρέπει τους βασικούς τύπους σε ισοδύναμους αναφορικούς και αντίστροφα, | ||
| - | |||
| - | <code java UnboxExample.java> | ||
| - | public class UnboxExample { | ||
| - | public static int sum(int a, int b) { | ||
| - | | ||
| - | } | ||
| - | | ||
| - | public static void main(String []args) { | ||
| - | Long a = new Long(5); | ||
| - | Integer b = new Integer(10); | ||
| - | int result = sum(a,b); | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | < | ||
| - | $ javac UnboxExample.java | ||
| - | UnboxExample.java: | ||
| - | int result = sum(a,b); | ||
| - | ^ | ||
| - | required: int,int | ||
| - | found: Long, | ||
| - | reason: actual argument Long cannot be converted to int by method invocation conversion | ||
| - | 1 error | ||
| - | </ | ||
| - | |||
| - | |Προηγούμενο: | ||