|
|
|
java:wrapper_classes [2017/02/16 12:20] gthanos |
java:wrapper_classes [2022/02/24 12:45] |
| ====== Βασικοί τύποι δεδομένων και ισοδύναμοι αναφορικοί τύποι ====== | |
| |
| Στην εισαγωγική [[java:variables|ενότητα]] των μεταβλητών είδαμε την διάκριση των μεταβλητών μεταξύ **βασικών τύπων** και **αναφορικών τύπων**. Για όλους τους βασικούς τύπους που συναντήσαμε η Java ορίζει ισοδύναμούς αναφορικούς τύπους δεδομένων οι οποίοι βρίσκονται μέσα στο πακέτο [[http://docs.oracle.com/javase/7/docs/api/java/lang/package-summary.html|java.lang]]. Οι αντιστοιχίες μεταξύ βασικών και αναφορικών τύπων είναι οι εξής: | |
| |
| ^ Βασικός τύπος ^ Αναφορικός τύπος ^ | |
| | boolean | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html|Boolean]] | | |
| | byte | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html|Byte]] | | |
| | char | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html|Character]] | | |
| | int | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html|Integer]] | | |
| | long | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html|Long]] | | |
| | short | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Short.html|Short]] | | |
| | float | [[http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html|Float]] | | |
| | double | [[http://docs.oracle.com/javase/7/docs/api/java/lang/.html|Double]] | | |
| |
| Κάθε αναφορικός τύπος μπορεί να δημιουργηθεί από ένα βασικό τύπο ή από ένα αλφαριθμητικό ([[http://docs.oracle.com/javase/7/docs/api/java/lang/String.html|String]]). Για παράδειγμα η κλάση //Integer// έχει τους παρακάτω δύο κατασκευαστές: | |
| |
| <code java> | |
| Integer(int value); | |
| //Constructs a newly allocated Integer object that represents the specified int value. | |
| Integer(String s); | |
| //Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. | |
| </code> | |
| |
| Eπίσης, κάθε μία από τις παραπάνω κλάσεις περιέχει μεθόδους για την εξαγωγή ενός βασικού τύπου δεδομένων από τον αναφορικό τύπο. Για παράδειγμα η κλάση //Integer// περιέχει μεθόδους για την εξαγωγή βασικών τύπων από των αναφορικό ως εξής: | |
| <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. | |
| </code> | |
| |
| Κάθε κλάση στην Java περιέχει την μέθοδο [[java:toString|toString()]]. Οι ισοδύναμοι αναφορικοί τύποι επανα-ορίζουν την μέθοδο //toString()//, ώστε να επιστρέφει ένα αλφαριθμητικό με την αριθμητική τιμή που αποθηκεύεται στον αναφορικό τύπο. | |
| |
| Εκτός από τα παραπάνω, κάθε αναφορικός τύπος περιέχει και άλλες μεθόδους που συνδέονται με την τιμή που είναι αποθηκευμένη στον αναφορικό τύπο ή static μεθόδους που επιτελούν συγκεκριμένες εργασίες. Για παράδειγμα, ένα μικρό τμήμα από τις μεθόδους που περιέχονται στην κλάση //Integer// είναι οι παρακάτω: | |
| <code java> | |
| static Integer decode(String nm) | |
| //Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers | |
| static int highestOneBit(int i) | |
| //Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value. | |
| int lowestOneBit(int i) | |
| //Returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value. | |
| 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 '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. | |
| 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. | |
| |
| κλπ. | |
| </code> | |
| |
| |
| |Προηγούμενο: [[:java:tostring | H μέθοδος toString() ]] | [[:toc | Περιεχόμενα]] | Επόμενο: [[:java:autoboxing | Autoboxing & Auto-unboxing ]]| | |
| |