En Java on peut soit utiliser une valeur "simple", appelée un type primitif, soit une valeur plus complexe étant l'instance d'une classe. Regardons en quoi consiste la simplicité de ces premières valeurs.
Il n'y a pas de différences énormes entre les types primitifs des données en Java et en autres langages. Il s'agit toujours d'éléments qui constituent les bases du langage. En Java on distingue 8 types primitifs : - int : concerne les entiers. - short : concerne les entiers. Contrairement à int, il est stocké sur 16 bits et non pas sur 32 bits. Il peut donc être utilisé dans les endroits pour économiser de la place dans la mémoire. - long : concerne les entiers. Sa déclaration se fait par ajout d'un suffixe "L" après le chiffre. - byte : concerne les entiers. Il peut prendre pour valeur uniquement les 0 et 1. Il est inscrit sur 8 bits. - float : concerne les nombres à virgule. - double : concerne les nombres à virgule. Contrairement à float, il est codé sur 64 bits et non pas 32. Sa précision (ou en vulgarisant, sa longueur) peut être plus grande que celle du float. - char : concerne les caractères UNICODE. Il est stocké sur 16 bits et doit être déclaré entre apostrophes (''). - boolean : concerne les résultats. Il contient true pour des tests positifs, false pour les négatifs.
Un traitement spécial est réservé aux chaînes de caractères (String). Même si le type string ne figure pas dans la liste des types primitifs, les chaînes de caractères peuvent en faire partie. Avec une simple déclaration contenue entre guillemets (""), chaque élément devient automatiquement une chaîne de caractères.
Récapitulons tout cela sur un exemple :
public class PrimitivesData { public static void main(String[] args) { TestPrimitives tp = new TestPrimitives(); } } class TestPrimitives { private String stringEx = "String test"; private int intEx = 43; private long longEx = 43L; private float floatEx = 43.04f; private double doubleEx = 55.03d; private short shortEx = 120; private byte byteEx = 00111; private char charEx = '\n'; private boolean booleanEx = true; public TestPrimitives() { System.out.println("String = " + stringEx + charEx); System.out.println("Int = " + intEx + charEx); System.out.println("Long = " + longEx + charEx); System.out.println("Float = " + floatEx + charEx); System.out.println("Double = " + doubleEx + charEx); System.out.println("Short = " + shortEx + charEx); System.out.println("Byte = " + byteEx + charEx); System.out.println("Boolean = " + booleanEx + charEx); } }
Les types primitifs ne sont pas des instances. Pour qu'ils en deviennent, il faut utiliser des enveloppeurs en Java.
Les conversions entre les types sont possibles. Regardons comment convertir les valeurs d'un type à l'autre grâce à ce formulaire :
Et voici une classe qui permet de tester les conversions :
public class PrimitiveDataConversions { public static void main(String[] args) { System.out.println("Converting from string : "); convertFromString(); System.out.println("Converting from int : "); convertFromInt(); System.out.println("Converting from long : "); convertFromLong(); System.out.println("Converting from float : "); convertFromFloat(); System.out.println("Converting from double : "); convertFromDouble(); System.out.println("Converting from short : "); convertFromShort(); System.out.println("Converting from byte : "); convertFromByte(); System.out.println("Converting from char : "); convertFromChar(); System.out.println("Converting from boolean : "); convertFromBoolean(); } private static void convertFromString() { String stringEx = "32"; // from String to int try { int stringInt = Integer.parseInt(stringEx.trim()); System.out.println("String => int = " + stringInt); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to int " + e.getMessage()); } // from String to long try { long stringLong = Long.parseLong(stringEx.trim()); System.out.println("String => long = " + stringLong); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to long " + e.getMessage()); } // from String to float try { float stringFloat = Float.parseFloat(stringEx.trim()); System.out.println("String => float = " + stringFloat); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to float " + e.getMessage()); } // from String to double try { double stringDouble = Double.parseDouble(stringEx.trim()); System.out.println("String => double = " + stringDouble); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to double " + e.getMessage()); } // from String to short try { short stringShort = new Short(Short.parseShort(stringEx.trim())); System.out.println("String => short = " + stringShort); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to short " + e.getMessage()); } // from String to byte try { byte stringByte = new Byte(Byte.parseByte(stringEx.trim())); System.out.println("String => byte = " + stringByte); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to byte " + e.getMessage()); } // from String to char try { char stringChar = new Character(stringEx.charAt(0)); // 0 => character position (0 is the first position) System.out.println("String => char = " + stringChar); } catch(NumberFormatException e) { System.out.println("Exception when trying to convert String to short " + e.getMessage()); } // from String to boolean boolean stringBool = Boolean.valueOf(stringEx.trim()).booleanValue(); System.out.println("String => boolean = " + stringBool); } private static void convertFromInt() { int intEx = 43; // from int to String String intString = Integer.toString(intEx); System.out.println("int => String = " + intString); // from int to long long intLong = intEx; System.out.println("int => long = " + intLong); // from int to float float intFloat = intEx; System.out.println("int => float = " + intFloat); // from int to double double intDouble = intEx; System.out.println("int => double = " + intDouble); // from int to short short intShort = (short)intEx; System.out.println("int => short = " + intShort); // from int to byte byte intByte = (byte)intEx; System.out.println("int => byte = " + intByte); // from int to char char intChar = (char)(intEx + '0'); System.out.println("int => char = " + intChar); // from int to boolean boolean intBool = (intEx == 1) ? Boolean.TRUE : Boolean.FALSE; System.out.println("int => boolean = " + intBool); } private static void convertFromLong() { long longEx = 43L; // from long to String String longString = Long.toString(longEx); System.out.println("long => String = " + longString); // from long to float float longFloat = new Float(longEx); System.out.println("long => float = " + longFloat); // from long to double double longDouble = new Double(longEx); System.out.println("long => double = " + longDouble); // from long to int int longInt = Integer.valueOf((int)longEx); System.out.println("long => int = " + longInt); // from long to short short longShort = new Short((short)longEx); System.out.println("long => short = " + longShort); // from long to byte byte longByte = new Byte((byte)longEx); System.out.println("long => byte = " + longByte); // from long to char char longChar = new Character((char)(longEx + '0')); System.out.println("long => char = " + longChar); // from long to boolean boolean longBool = (longEx == 1) ? Boolean.TRUE : Boolean.FALSE; System.out.println("long => boolean = " + longBool); } private static void convertFromFloat() { float floatEx = 1.0f; // from float to String String floatString = Float.toString(floatEx); System.out.println("float => String = " + floatString); // from float to long long floatLong = (long)floatEx; System.out.println("float => long = " + floatLong); // from float to double double floatDouble = floatEx; System.out.println("float => double = " + floatDouble); // from float to int double floatInt = (int)floatEx; System.out.println("float => int = " + floatInt); // from float to short short floatShort = (short)floatEx; System.out.println("float => short = " + floatShort); // from float to byte byte floatByte = new Byte((byte)floatEx); System.out.println("float => byte = " + floatByte); // from float to char char floatChar = new Character((char)floatEx); System.out.println("float => char = " + floatChar); // from float to boolean boolean floatBool = (floatEx == 1.0) ? Boolean.TRUE : Boolean.FALSE; System.out.println("float => boolean = " + floatBool); } private static void convertFromDouble() { double doubleEx = 55.03d; // from double to String String doubleString = Double.toString(doubleEx); System.out.println("double => String = " + doubleString); // from double to long long doubleLong = (long)doubleEx; System.out.println("double => long = " + doubleLong); // from double to float float doubleFloat = (float)doubleEx; System.out.println("double => float = " + doubleFloat); // from double to int double doubleInt = (int)doubleEx; System.out.println("double => int = " + doubleInt); // from double to short short doubleShort = (short)doubleEx; System.out.println("double => short = " + doubleShort); // from double to byte byte doubleByte = new Byte((byte)doubleEx); System.out.println("double => byte = " + doubleByte); // from double to char char doubleChar = new Character((char)doubleEx); System.out.println("double => char = " + doubleChar); // from double to boolean boolean doubleBool = (doubleEx == 1.0) ? Boolean.TRUE : Boolean.FALSE; System.out.println("double => boolean = " + doubleBool); } private static void convertFromShort() { short shortEx = 120; // from short to String String shortString = Integer.toString(shortEx); System.out.println("short => String = " + shortString); // from short to float float shortFloat = shortEx; System.out.println("short => float = " + shortFloat); // from short to double double shortDouble = shortEx; System.out.println("short => double = " + shortDouble); // from short to int int shortInt = shortEx; System.out.println("short => int = " + shortInt); // from short to long short shortLong = shortEx; System.out.println("short => long = " + shortLong); // from short to byte byte shortByte = (byte)shortEx; System.out.println("short => byte = " + shortByte); // from short to char char shortChar = (char)(shortEx + '0'); System.out.println("short => char = " + shortChar); // from short to boolean boolean shortBool = (shortEx == 1) ? Boolean.TRUE : Boolean.FALSE; System.out.println("short => boolean = " + shortBool); } private static void convertFromByte() { byte byteEx = 00111; // from byte to String String byteString = Integer.toString(byteEx); System.out.println("byte => String = " + byteString); // from byte to float float byteFloat = byteEx; System.out.println("byte => float = " + byteFloat); // from byte to double double byteDouble = byteEx; System.out.println("byte => double = " + byteDouble); // from byte to int int byteInt = byteEx; System.out.println("byte => int = " + byteInt); // from byte to long short byteLong = byteEx; System.out.println("byte => long = " + byteLong); // from byte to short short byteShort = byteEx; System.out.println("byte => short = " + byteShort); // from byte to char char byteChar = (char)(byteEx + '0'); System.out.println("byte => char = " + byteChar); // from byte to boolean boolean byteBool = (byteEx == 1) ? Boolean.TRUE : Boolean.FALSE; System.out.println("byte => boolean = " + byteBool); } private static void convertFromChar() { char charEx = '\n'; // from char to String String charString = String.valueOf(charEx); System.out.println("char => String = " + charString); // from char to float float charFloat = charEx; System.out.println("char => float = " + charFloat); // from char to double double charDouble = charEx; System.out.println("char => double = " + charDouble); // from char to int int charInt = charEx; System.out.println("char => int = " + charInt); // from char to long long charLong = charEx; System.out.println("char => long = " + charLong); // from char to short short charShort = (short)(charEx - '0'); System.out.println("char => short = " + charShort); // from char to byte byte charByte = (byte)(charEx - '0'); System.out.println("char => byte = " + charByte); // from char to boolean boolean charBool = (charEx == '1') ? Boolean.TRUE : Boolean.FALSE; System.out.println("char => boolean = " + charBool); } private static void convertFromBoolean() { boolean booleanEx = true; // from boolean to String String booleanString = String.valueOf(booleanEx); System.out.println("boolean => String = " + booleanString); // from boolean to float float booleanFloat = booleanEx ? 1.0f : 0.0f; System.out.println("boolean => float = " + booleanFloat); // from boolean to double double booleanDouble = booleanEx ? 1.0d : 0.0d; System.out.println("boolean => double = " + booleanDouble); // from boolean to int int booleanInt = booleanEx ? 1 : 0; System.out.println("boolean => int = " + booleanInt); // from boolean to long long booleanLong = booleanEx ? 1L : 0L; System.out.println("boolean => long = " + booleanLong); // from boolean to short short booleanShort = booleanEx ? (short)1 : (short)0; System.out.println("boolean => short = " + booleanShort); // from boolean to byte byte booleanByte = booleanEx ? (byte)1 : (byte)0; System.out.println("boolean => byte = " + booleanByte); // from boolean to char char booleanChar = booleanEx ? '1' : '0'; System.out.println("boolean => char = " + booleanChar); } }