java:wrapper_classes

Βασικοί τύποι δεδομένων και ισοδύναμοι αναφορικοί τύποι

Στην εισαγωγική ενότητα των μεταβλητών είδαμε την διάκριση των μεταβλητών μεταξύ βασικών τύπων και αναφορικών τύπων. Για όλους τους βασικούς τύπους που συναντήσαμε ο Java ορίζει ισοδύναμούς αναφορικούς τύπους δεδομένων οι οποίοι βρίσκονται μέσα στο πακέτο java.lang. Οι αντιστοιχίες μεταξύ βασικών και αναφορικών τύπων είναι οι εξής:

Βασικός τύπος Αναφορικός τύπος
boolean Boolean
byte Byte
char Character
int Integer
long Long
short Short
float Float
double Double

Κάθε αναφορικός τύπος μπορεί να δημιουργηθεί από ένα βασικό τύπο ή από ένα αλφαριθμητικό (String). Για παράδειγμα η κλάση Integer έχει τους παρακάτω δύο κατασκευαστές:

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.

Eπίσης, κάθε μία από τις παραπάνω κλάσεις περιέχει μεθόδους για την εξαγωγή ενός βασικού τύπου δεδομένων από τον αναφορικό τύπο. Για παράδειγμα η κλάση Integer περιέχει μεθόδους για την εξαγωγή βασικών τύπων από των αναφορικό ως εξής:

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 περιέχει την μέθοδο toString(). Οι ισοδύναμοι αναφορικοί τύποι επανα-ορίζουν την μέθοδο toString(), ώστε να επιστρέφει ένα αλφαριθμητικό με την αριθμητική τιμή που αποθηκεύεται στον αναφορικό τύπο.

Εκτός από τα παραπάνω, κάθε αναφορικός τύπος περιέχει και άλλες μεθόδους που συνδέονται με την τιμή που είναι αποθηκευμένη στον αναφορικό τύπο ή static μεθόδους που επιτελούν συγκεκριμένες εργασίες. Για παράδειγμα, ένα μικρό τμήμα από τις μεθόδους που περιέχονται στην κλάση Integer είναι οι παρακάτω:

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.
 
κλπ.
java/wrapper_classes.txt · Last modified: 2017/02/16 12:34 by gthanos