Movatterモバイル変換


[0]ホーム

URL:


Loading
  1. Elastic Docs/
  2. Reference/
  3. Elasticsearch/
  4. Scripting languages/
  5. Painless/
  6. Painless language specification

Casting

A cast converts the value of an original type to the equivalent value of a target type. An implicit cast infers the target type and automatically occurs during certainoperations. An explicit cast specifies the target type and forcefully occurs as its own operation. Use thecast operator '()' to specify an explicit cast. You can also check out thetype casting tutorial for related examples, and for help with troubleshooting refer to "type casting issues" in the Troubleshooting section.

Refer to thecast table for a quick reference on all allowed casts. To help resolve any issues, checkDebug type casting errors in Painless.

Errors

  • If during a cast there exists no equivalent value for the target type.
  • If an implicit cast is given, but an explicit cast is required.

Grammar

cast: '(' TYPE ')' expression

Examples

  • Valid casts

    int i = (int)5L;Map m = new HashMap();HashMap hm = (HashMap)m;
    1. declareint i; explicit castlong 5 toint 5int 5; storeint 5 toi
    2. declareMap m; allocateHashMap instance →HashMap reference; implicit castHashMap reference toMap referenceMap reference; storeMap reference tom
    3. declareHashMap hm; load frommMap reference; explicit castMap reference toHashMap referenceHashMap reference; storeHashMap reference tohm

Anumeric type cast converts the value of an original numeric type to the equivalent value of a target numeric type. A cast between two numeric type values results in data loss when the value of the original numeric type is larger than the target numeric type can accommodate. A cast between an integer type value and a floating point type value can result in precision loss.

The allowed casts for values of each numeric type are shown as a row in the following table:

byteshortcharintlongfloatdouble
byteimplicitimplicitimplicitimplicitimplicitimplicit
shortexplicitexplicitimplicitimplicitimplicitimplicit
charexplicitexplicitimplicitimplicitimplicitimplicit
intexplicitexplicitexplicitimplicitimplicitimplicit
longexplicitexplicitexplicitexplicitimplicitimplicit
floatexplicitexplicitexplicitexplicitexplicitimplicit
doubleexplicitexplicitexplicitexplicitexplicitexplicit

Examples

  • Valid numeric type casts

    int a = 1;long b = a;short c = (short)b;double e = (double)a;
    1. declareint a; storeint 1 toa
    2. declarelong b; load fromaint 1; implicit castint 1 tolong 1long 1; storelong 1 tob
    3. declareshort c; load fromblong 1; explicit castlong 1 toshort 1short 1; storeshort 1 value toc
    4. declaredouble e; load fromaint 1; explicit castint 1 todouble 1.0; storedouble 1.0 toe; (note the explicit cast is extraneous since an implicit cast is valid)
  • Invalid numeric type casts resulting in errors

    int a = 1.0;int b = 2;byte c = b;
    1. declareint i;error → cannot implicit castdouble 1.0 toint 1; (note an explicit cast is valid)
    2. declareint b; storeint 2 tob
    3. declare bytec; load frombint 2;error → cannot implicit castint 2 tobyte 2; (note an explicit cast is valid)

Areference type cast converts the value of an original reference type to the equivalent value of a target reference type. An implicit cast between two reference type values is allowed when the original reference type is a descendant of the target type. An explicit cast between two reference type values is allowed when the original type is a descendant of the target type or the target type is a descendant of the original type.

Examples

  • Valid reference type casts

    List x;ArrayList y = new ArrayList();x = y;y = (ArrayList)x;x = (List)y;
    1. declareList x; store default valuenull tox
    2. declareArrayList y; allocateArrayList instance →ArrayList reference; storeArrayList reference toy;
    3. load fromyArrayList reference; implicit castArrayList reference toList referenceList reference; storeList reference tox; (noteArrayList is a descendant ofList)
    4. load fromxList reference; explicit castList reference toArrayList referenceArrayList reference; storeArrayList reference toy;
    5. load fromyArrayList reference; explicit castArrayList reference toList referenceList reference; storeList reference tox; (note the explicit cast is extraneous, and an implicit cast is valid)
  • Invalid reference type casts resulting in errors

    List x = new ArrayList();ArrayList y = x;Map m = (Map)x;
    1. declareList x; allocateArrayList instance →ArrayList reference; implicit castArrayList reference toList referenceList reference; storeList reference tox
    2. declareArrayList y; load fromxList reference;error → cannot implicit castList reference toArrayList reference; (note an explicit cast is valid sinceArrayList is a descendant ofList)
    3. declareArrayList y; load fromxList reference;error → cannot explicit castList reference toMap reference; (note no cast is valid since neitherList norMap is a descendant of the other)

Adynamic (def) type cast converts the value of an originaldef type to the equivalent value of any target type or converts the value of any original type to the equivalent value of a targetdef type.

An implicit cast from any original type value to adef type value is always allowed. An explicit cast from any original type value to adef type value is always allowed but never necessary.

An implicit or explicit cast from an originaldef type value to any target type value is allowed if and only if the cast is normally allowed based on the current type value thedef type value represents.

Examples

  • Valid dynamic type casts with any original type to a targetdef type

    def d0 = 3;d0 = new ArrayList();Object o = new HashMap();def d1 = o;int i = d1.size();
    1. declaredef d0; implicit castint 3 todef; storeint 3 tod0
    2. allocateArrayList instance →ArrayList reference; implicit castArrayList reference todefdef; storedef tod0
    3. declareObject o; allocateHashMap instance →HashMap reference; implicit castHashMap reference toObject referenceObject reference; storeObject reference too
    4. declaredef d1; load fromoObject reference; implicit castObject reference todefdef; storedef tod1
    5. declareint i; load fromd1def; implicit castdef toHashMap reference → HashMap reference; callsizeonHashMap referenceint 0; storeint 0toi; (notedefwas implicit cast toHashMap referencesinceHashMapis the child-most descendant type value that thedef` type value represents)
  • Valid dynamic type casts with an originaldef type to any target type

    def d = 1.0;int i = (int)d;d = 1;float f = d;d = new ArrayList();List l = d;
    1. declaredef d; implicit castdouble 1.0 todefdef; storedef tod
    2. declareint i; load fromddef; implicit castdef todouble 1.0double 1.0; explicit castdouble 1.0 toint 1int 1; storeint 1 toi; (note the explicit cast is necessary since adouble type value is not converted to anint type value implicitly)
    3. storeint 1 tod; (note the switch in the typed represents fromdouble toint)
    4. declarefloat i; load fromddef; implicit castdef toint 1int 1; implicit castint 1 tofloat 1.0float 1.0; storefloat 1.0 tof
    5. allocateArrayList instance →ArrayList reference; storeArrayList reference tod; (note the switch in the typed represents fromint toArrayList)
    6. declareList l; load fromddef; implicit castdef toArrayList referenceArrayList reference; implicit castArrayList reference toList referenceList reference; storeList reference tol
  • Invalid dynamic type casts resulting in errors

    def d = 1;short s = d;d = new HashMap();List l = d;
    1. declaredef d; implicit castint 1 todefdef; storedef tod
    2. declareshort s; load fromddef; implicit castdef toint 1int 1;error → cannot implicit castint 1 toshort 1; (note an explicit cast is valid)
    3. allocateHashMap instance →HashMap reference; implicit castHashMap reference todefdef; storedef tod
    4. declareList l; load fromddef; implicit castdef toHashMap reference;error → cannot implicit castHashMap reference toList reference; (note no cast is valid since neitherHashMap norList is a descendant of the other)

Use the cast operator to convert aString type value into achar type value.

Errors

  • If theString type value isn’t one character in length.
  • If theString type value isnull.

Examples

  • Casting string literals intochar type values

    char c = (char)"C";c = (char)'c';
    1. declarechar c; explicit castString "C" tochar Cchar C; storechar C toc
    2. explicit castString 'c' tochar cchar c; storechar c toc
  • Casting aString reference into achar type value

    String s = "s";char c = (char)s;
    1. declareString s; storeString "s" tos;
    2. declarechar c load fromsString "s"; explicit castString "s" tochar schar s; storechar s toc

Use the cast operator to convert achar type value into aString type value.

Examples

  • Casting aString reference into achar type value

    char c = 65;String s = (String)c;
    1. declarechar c; storechar 65 toc;
    2. declareString s load fromcchar A; explicit castchar A toString "A"String "A"; storeString "A" tos

Boxing is a special type of cast used to convert a primitive type to its corresponding reference type. Unboxing is the reverse used to convert a reference type to its corresponding primitive type.

Implicit boxing/unboxing occurs during the following operations:

  • Conversions between adef type and a primitive type are implicitly boxed/unboxed as necessary, though this is referred to as an implicit cast throughout the documentation.
  • Method/function call arguments are implicitly boxed/unboxed as necessary.
  • A primitive type value is implicitly boxed when a reference type method is called on it.

Explicit boxing/unboxing is not allowed. Use the reference type API to explicitly convert a primitive type value to its respective reference type value and vice versa.

Errors

  • If an explicit cast is made to box/unbox a primitive type.

Examples

  • Uses of implicit boxing/unboxing

    List l = new ArrayList();l.add(1);Integer I = Integer.valueOf(0);int i = l.get(i);
    1. declareList l; allocateArrayList instance →ArrayList reference; storeArrayList reference tol;
    2. load fromlList reference; implicit castint 1 todefdef; calladd onList reference with arguments (def); (note internallyint 1 is boxed toInteger 1 to store as adef type value)
    3. declareInteger I; callvalueOf onInteger with arguments of (int 0) →Integer 0; storeInteger 0 toI;
    4. declareint i; load fromIInteger 0; unboxInteger 0int 0; load fromlList reference; callget onList reference with arguments (int 0) →def; implicit castdef toint 1int 1; storeint 1 toi; (note internallyint 1 is unboxed fromInteger 1 when loaded from adef type value)
  • Uses of invalid boxing/unboxing resulting in errors.

    Integer x = 1;Integer y = (Integer)1;int a = Integer.valueOf(1);int b = (int)Integer.valueOf(1);
    1. declareInteger x;error → cannot implicit boxint 1 toInteger 1 during assignment
    2. declareInteger y;error → cannot explicit boxint 1 toInteger 1 during assignment
    3. declareint a; callvalueOf onInteger with arguments of (int 1) →Integer 1;error → cannot implicit unboxInteger 1 toint 1 during assignment
    4. declareint a; callvalueOf onInteger with arguments of (int 1) →Integer 1;error → cannot explicit unboxInteger 1 toint 1 during assignment

Promotion is when a single value is implicitly cast to a certain type or multiple values are implicitly cast to the same type as required for evaluation by certain operations. Each operation that requires promotion has a promotion table that shows all required implicit casts based on the type(s) of value(s). A value promoted to adef type at compile-time is promoted again at run-time based on the type thedef value represents.

Errors

  • If a specific operation cannot find an allowed promotion type for the type(s) of value(s) given.

Examples

  • Uses of promotion

    double d = 2 + 2.0;def x = 1;float f = x + 2.0F;
    1. declaredouble d; promoteint 2 anddouble 2.0 @0: resultdouble; implicit castint 2 todouble 2.0 @1double 2.0 @1; adddouble 2.0 @1 anddouble 2.0 @0double 4.0; storedouble 4.0 tod
    2. declaredef x; implicit castint 1 todefdef; storedef tox;
    3. declarefloat f; load fromxdef; implicit castdef toint 1int 1; promoteint 1 andfloat 2.0: resultfloat; implicit castint 1 tofloat 1.0float1.0; addfloat 1.0andfloat 2.0float 3.0; storefloat 3.0tof; (note this example illustrates promotion done at run-time as promotion done at compile-time would have resolved to adef` type value)

The following tables show all allowed casts. Read the tables row by row, where the original type is shown in the first column, and each subsequent column indicates whether a cast to the specified target type is implicit (I), explicit (E), boxed/unboxed for methods only (A), a reference type cast (@), or is not allowed (-). Seereference type casting for allowed reference type casts.

Primitive/Reference Types

ONTbyscijfdBYSCIJFDRdef
Object ( O )@@--------@@@@@@@@@I
Number ( N )I----------@@-@@@@@I
String ( T )I------------E-----I
boolean ( b )A---------A--------I
byte ( y )AA--IEIIII-AA-AAAA-I
short ( s )AA--EEIIII--A-AAAA-I
char ( c )A-E-EEIIII---AAAAA-I
int ( i )AA--EEEIII----AAAA-I
long ( j )AA--EEEEII-----AAA-I
float ( f )AA--EEEEEI------AA-I
double ( d )AA--EEEEEE-------A-I
Boolean ( B )A--A--------------@I
Byte ( Y )AI--AA-AAAA-A-AAAA@I
Short ( S )AI---A-AAAA---AAAA@I
Character ( C )A-----AAAAA---AAAA@I
Integer ( I )A------AAAA----AAA@I
Long ( J )A-------AAA-----AA@I
Float ( F )A--------AA------A@I
Double ( D )A---------A-------@I
Reference ( R )I@@--------@@@@@@@@@I

def Type

ONTbyscijfdBYSCIJFDR
def as StringI-I---E-------E----@
def as boolean/BooleanI--I-------I-------@
def as byte/ByteI---IIEIIII-IIEIIII@
def as short/ShortI---EIEIIII-EIEIIII@
def as char/CharacterI---EEIIIII-EEIIIII@
def as int/IntegerI---EEEIIII-EEEIIII@
def as long/LongI---EEEEIII-EEEEIII@
def as float/FloatI---EEEEEII-EEEEEII@
def as double/DoubleI---EEEEEEI-EEEEEEI@
def as Reference@@@--------@@@@@@@@@

[8]ページ先頭

©2009-2026 Movatter.jp