Conversion between java Data types

Source: Internet
Author: User

If an object cannot be directly converted to a type. You need a shape.
For example:
If an int type can be directly converted to a double type, no styling is required.
A double type cannot be directly converted to an int type.
Int a = 1;
Double d1 = a; // no styling required
Int a2 = (int) d1 // The shape is required.

Note:
1) a boolean cannot be of any other type, nor can it be of any other type.
2) subclasses can be directly converted to parent classes without styling. The parent class cannot be directly converted to a subclass and needs to be modeled.
3) When you force the shape, the shape may not be successful. At this time, the correct shape is compiled, but an error occurs during execution.

Data type conversion

Class StringConversions {
Public static void main (String args []) {
Int num = 19648;

System. out. println (num + "in binary:" + Integer. toBinaryString (num ));

System. out. println (num + "in octal:" + Integer. toOctalString (num ));

System. out. println (num + "in hexadecimal:" + Integer. toHexString (num ));
  }
}


The so-called "shape" does not change an object of a type to a certain type, but "as" a certain type. For example, there is a parent class human, sub-class man and woman. If a new man does not need an explicit shape, it can be regarded as a human. (For example, human aHuman = new man ();) when you get a human object, it may actually be a man, a woman, or even a human. If you still think of the new man as a man (that is, man m1 = (man) aHuman;, this is the so-called "downsampling "), but if you think of it as "woman (like this: woman w1 = (woman) aHuman;), an exception will occur.
Therefore, "up-and-down styling" must be safe, while "down-and-down styling" is not safe.
Instance 2

Class Animal {
Public String toString (){
Return "This is an animal ";
  }
}
Class Dog extends Animal {
Public void sound (){
System. out. println ("Woof ");
  }
}
Class Cat extends Animal {
Public Cat (String aName ){
Name = aName;
  }
Public String toString (){
Return super. toString () + "nIt's" + name + "the" + breed;
  }
Public void sound (){
System. out. println ("Miiaooww ");
  }
Private String name;
Private String breed;
}
Public class MainClass {
Public static void main (String [] ){
Cat aPet = new Cat ("Fang ");
Animal theAnimal = (Animal) aPet;
  }
}

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.