有关Java基本类型方法的问题 [英] Question about Java Primitive Types methods
问题描述
我对Java中的原始类型以及将一种类型转换为另一种类型的方法感到困惑.例如,如果我有一个整数,并且想要将其转换为字符串,则需要使用Integer或String的静态方法,例如
I'm confused with primitive types in Java and the methods of converting one type to another. If, say, I have an integer and I want to convert it to a string, I need to use a static method of Integer or String, e.g.
String.valueOf(some_integer);
但是,如果我想将stirng转换为char数组,可以使用类似的内容,
But if I want to convert a stirng to a char array I can use something like,
some_string.toCharArray();
我的问题是为什么?为什么第一个方法需要使用静态方法?
My question is why? Why do I need to use a static method for the first one?
推荐答案
由于您传递的参数- int
是基元,而基元不是对象-您无法在其上调用方法
Because the argument you pass - an int
is a primitive, and primitives are not objects - you can't invoke methods on them.
如果整数的包装类型为 Integer
,则可以使用 someInteger.toString()
If the integer was of the wrapper type Integer
, you could've used someInteger.toString()
这篇关于有关Java基本类型方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!