On the type conversion in Java

Source: Internet
Author: User
Tags float double tostring

To say type conversion, first of all, to say a Java data type. There are two kinds of data types in Java: base type, reference type. Basic data types There's nothing to say. Byte char short int long float Double Boolean, these types are not much different from the C language type except Boolean. Because the topic of this article is type conversion, the use of Boolean values is not discussed here.

The following is a reference type. References in some books are also referred to as handles, which are similar to pointers in C + +, but note that references and pointers are not the same concept. A pointer is a variable that holds an address. He makes the C + + programmer flexible access to memory, but this also gives the security of the program has a great hidden trouble, because the programmer can be arbitrary operation of the pointer, so a careless will destroy other storage units, resulting in unexpected results in the program. The reference inherits the advantage of the pointer saving memory and restricts the operation of the address, so he is safe. A reference type includes instances and arrays generated by all classes (whether an object array or a base type array implements the Cloneable interface, so he is also an object instance), and all reference types inherit from the object class. The point is that all variables in Java are a reference, whether it is a reference type or a basic type.

Now it's time to formally discuss the type conversions. The conversion of a basic type by a person who has used C + + is clear, and the base type conversion is divided into type elevation and cast.

For example:

int a=100;
long b=a+100;//这个地方就用到了类型提升,a+100从int提升到了long
a=(int)b;//这个地方用到了强制转换

Coercion of type conversions can lose precision in some cases, such as:

byte b;
int a=200;
b=(byte)a;//虽然这里用到了强制转换,但因为byte的范围是-127到127
//所以强制转换后宽度会被截短

In Java, in addition to these transformations, basic data types can be implicitly converted to strings, for example:

System.out.print("转换"+100);//如果在数据前面有字符串用+连接
//就会隐式的转换成String

Conversion of reference types is much simpler than C + +, and if an object has no inheritance relationship with another object, then they cannot do type conversions. If you want to assign a derived class object to a base class object, this is called the trace shape. If you want to assign a base class object to a derived class object, you need to force type conversion, which is called the next form, and there are some dangers to the next form, to be safe for the next form there is a front question, the base class object must be traced back from the derived class object.

For example:

class Base{}
class Child extends Base{
 public static void main(String[] args){
  Base base=new Child();//上溯造型
  Child child=(Child)base;//下溯造型
  Child child1=(Child)new
  Base();//抛出ClassCastException异常
 }
}

Finally, we talk about the conversion of string and reference type. As has been said before, all objects are inherited from object, and there is a ToString method in object. This method is that all objects can be converted to strings, and if you want to convert a custom class to a string, the safest way to do this is to override the ToString method. The same as the base type if the object has a string object in front of the + join, the object is implicitly converted to a string, which in effect implicitly invokes the ToString method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.