함수 type()

(Typeof에서 넘어옴)
  다른 뜻에 대해서는 리눅스 type 문서를 참조하십시오.

1 개요[ | ]

.getClass()
gettype
type
typeof
.class

2 C[ | ]

#include <stdio.h>

#define type(x) _Generic((x),                                                     \
        _Bool: "_Bool",                  unsigned char: "unsigned char",          \
         char: "char",                     signed char: "signed char",            \
    short int: "short int",         unsigned short int: "unsigned short int",     \
          int: "int",                     unsigned int: "unsigned int",           \
     long int: "long int",           unsigned long int: "unsigned long int",      \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
        float: "float",                         double: "double",                 \
  long double: "long double",                   char *: "char *",                 \
       void *: "void *",                         int *: "int *",                  \
      default: "unknown")

int main() {
    char  ch;
    int   a;
    long  b;
    char* str;
    char  str2[5];

    printf( "%s\n", type(ch)   ); // char
    printf( "%s\n", type(a)    ); // int
    printf( "%s\n", type(b)    ); // long int
    printf( "%s\n", type(str)  ); // char *
    printf( "%s\n", type(str2) ); // char *
}

3 Go[ | ]

package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println(reflect.TypeOf(123))     // int
    fmt.Println(reflect.TypeOf(123.45))  // float64
    fmt.Println(reflect.TypeOf("hello")) // string
    
    fmt.Println(reflect.TypeOf(true))    // bool
    fmt.Println(reflect.TypeOf(false))   // bool
    fmt.Println(reflect.TypeOf(nil))     // <nil>
}

4 Java[ | ]

import java.util.ArrayList;
import java.util.HashMap;
class MyClass{public static void main(String[] args){
	System.out.println( "hello".getClass() );  // class java.lang.String

	Object obj1 = new ArrayList<Object>();
	Object obj2 = new HashMap<String,Object>();
	System.out.println( obj1.getClass() );     // class java.util.ArrayList
	System.out.println( obj2.getClass() );     // class java.util.HashMap

	System.out.println( obj1.getClass() == ArrayList.class );  // true
	System.out.println( obj1.getClass() == HashMap.class   );  // false
	System.out.println( obj2.getClass() == ArrayList.class );  // false
	System.out.println( obj2.getClass() == HashMap.class   );  // true
}}

5 JavaScript[ | ]

console.log( typeof [] );            // object
console.log( typeof {} );            // object
console.log( typeof 42 );            // number
console.log( typeof "hello" );       // string
console.log( typeof function(){} );  // function

var func = function() {};
console.log( typeof func );          // function

6 PHP[ | ]

echo gettype(123)  ."\n";  # integer
echo gettype('123')."\n";  # string
echo gettype([])   ."\n";  # array

7 Python[ | ]

Python 3
print(type(123))     # <class 'int'>
print(type('123'))   # <class 'str'>
print(type({}))      # <class 'dict'>
print(type([]))      # <class 'list'>

print(type('안녕'))   # <class 'str'>
print(type(u'안녕'))  # <class 'str'>
→ Python 3는 항상 str = unicode
Python 2
print(type(123))     # <type 'int'>
print(type('123'))   # <type 'str'>
print(type({}))      # <type 'dict'>
print(type([]))      # <type 'list'>

print(type('안녕'))   # <type 'str'>
print(type(u'안녕'))  # <type 'unicode'>

8 Perl[ | ]

use Scalar::Util qw(reftype);
my $x = bless {}, 'Wiki::ZetaWiki';
print "type of x: "      . ref($x)     . "\n";  # type of x: Wiki::ZetaWiki
print "base type of x: " . reftype($x) . "\n";  # base type of x: HASH

9 R[ | ]

class(42)             #[1] "numeric"
class(TRUE)           #[1] "logical"
class("Hello World")  #[1] "character"

10 Ruby[ | ]

puts "123".class         # String
puts 123.class           # Integer
puts 123.45.class        # Float

puts [].class            # Array
puts :hello.class        # Symbol
puts Proc.new {}.class   # Proc

11 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}