Java modeling Operator (Cast)

Source: Internet
Author: User
Tags integer lowercase natural logarithm

The role of "styling" (Cast) is to "match a model". When appropriate, Java converts one data type automatically to another. For example, suppose we assign an integer value to a floating-point variable, and the computer automatically converts the int to float. By styling, we can explicitly set this type of conversion, or force it to proceed when it is not normally possible.
To make a styling, place the desired data type (including all modifiers) in parentheses to the left of any other value. Here is an example:

void casts () {
int i = 200;
Long L = (long) i;
Long L2 = (long) 200;
}

As you can see, it is not only a numerical model, but also a variable modeling process. But in both cases, the styling is redundant because the compiler automatically converts the int value to a long value when necessary. Of course, you can still set a shape, remind yourself to notice, also make the program clearer. In other cases, styling is important only when code is compiled.
In C and C + +, styling can sometimes make a person headache. In Java, styling is a safer operation. However, if you do an operation called narrowing conversion (that is, the script is a data type that can hold more information and convert it to a Narrowing type), you may be at risk of information loss. At this point, the compiler forces us to sculpt, as if to say, "This could be a dangerous thing--if you want me to do it with all my might, I'm sorry, please make it clear." "For magnification conversion (widening conversion), you do not have to make explicit styling, because the new type will certainly accommodate the original type of information and will not cause any loss of information."
Java allows us to "sculpt" any main type to any other main type, except for Boolean (Bollean), which does not allow any styling at all. "Class" does not allow styling. In order to convert one species to another, special methods must be used (the string is a special case, which is described later in this book to shape the object into a type "family"; for example, "oak" can be styled as a "tree" and vice versa.) However, for other exotic types, such as "rock", it cannot be shaped as "tree".

1. Literal value
In the beginning, if you insert a "literal" (Literal) in a program, the compiler will usually know exactly what type to generate. But in some cases, the type is ambiguous. If this happens, the compiler must be properly "instructed". The method is to add some additional information in the form of characters associated with the literal value. The following code shows you these characters.

: Literals.java

class Literals {
  char c = 0xffff;//Max char hex value
  byte b = 0x7f;//MAX byte hex value Short
  s = 0x7fff.//MAX short hex value
  int i1 = 0x2f;//hexadecimal (lowercase)
  int i2 = 0x2f;//Hexadeci Mal (uppercase)
  int i3 = 0177;//octal (leading zero)
  //Hex and OCT also work with long.
  Long N1 = 200L; Long suffix
  long n2 = 200l;//long suffix
  long n3 =
  //! Long L6 (200); Not allowed
  float f1 = 1;
  float F2 = 1F; float suffix
  float f3 = 1f;//float suffix
  float f4 = 1e-45f;//The Power
  float f5 = 1e+9f;//F Loat suffix
  double d1 = 1d;//double suffix
  double d2 = 1 D;//double suffix
  double d3 = 47e47d;// The Power
}///:~


Hexadecimal (Base 16)--it applies to all integer data types--with a preceding 0x or 0X indication. and followed by 0-9 and a-f in uppercase or lowercase form. If you try to initialize a variable to a value that is beyond your ability (regardless of the numerical form of that value), the compiler will report an error message to us. Note that in the above code, the largest hexadecimal value will only appear in Char,byte and short. If this limit is exceeded, the compiler will automatically change the value to an int and tell us that we need to "shrink shape" the assignment. In this way, we can clearly learn that we have overloaded the border.
Octal (Base 8) is indicated by a digit of 0 and 0-7 in a number. In c,c++ or Java, there is no corresponding "literal" representation of the binary number.
The trailing character after the literal value marks its type. For uppercase or lowercase l, for long, uppercase or lowercase f, for float, uppercase or lowercase d, for double.
Indices always use a notation that we think is not intuitive: 1.39e-47f. In the field of science and engineering, "E" represents the cardinality of the natural logarithm, approximately equal to 2.718 (Java a more precise double value in the form of MATH.E). It is used in an exponential expression like "1.39xe-47", meaning "1.39x2.718-47". However, since the invention of the Fortran language, people naturally feel that E represents "more than 10 less power". This is odd, because Fortran was originally oriented to the field of science and engineering design. Of course, its designers should be cautious about such confusing concepts (note ①). However, this particular expression is stubbornly preserved in c,c++ and now Java. So if you're accustomed to using e as the cardinal number of natural logarithms, when you see an expression like "1.39e-47f" in Java, convert your mind and think about it from a programming point of view; it really means "1.39x10-47-second party".

①:john Kirkham wrote: "I first used Fortran II on an IBM 1620 machine in 1962." At that time--including the 60 's and early 70 's, Fortran always used capital letters. This may be the case because the early input devices were mostly old-fashioned typewriters, using 5-bit Baudot codes, which did not have lowercase capabilities. The ' e ' in the power expression must also be uppercase, so it will not collide with the cardinality ' e ' of the natural logarithm, which is necessarily lowercase. The meaning of the letter ' E ' is actually very simple, that is, ' exponential ' means ' exponential ' or ' power ', which represents the base of the computing system--typically 10. At that time, the octal system was also widely used in programmers. Although I didn't see it myself, I would think of it as base 8 if I saw a octal number in the power expression. I remember the first time I saw the index in lowercase ' e ' that was at the end of the 70 's. I also thought it was a very easy to confuse. So, this problem is completely oneself ' sneak into ' Fortran, not from the beginning. If you really want to use the cardinality of the natural logarithm, there are existing functions available, but they are all uppercase. ”

Note If the compiler is able to recognize the type correctly, you do not have to use trailing characters. For the following statement:
Long n3 = 200;
It does not exist in a vague place, so a l large can be omitted after 200. However, for the following statement:
float F4 = 1e-47f; Power number of 10
The compiler usually handles the exponent as a double, so if you don't have this trailing F, you get an error that tells us to use a "styling" to convert the double to float.

2. Transformation
You will find that if you perform any arithmetic or bitwise operations on the main data type as long as they are "smaller than int" (i.e. char,byte or short), those values are automatically converted to int before the operation is formally performed. As a result, the resulting value is the int type. So as long as you assign a value back to a smaller type, you must use "styling". In addition, information loss may occur because the value is assigned back to a smaller type. Typically, the largest data type in an expression is the type that determines the size of the final result of an expression. If you multiply a float value by a double, the result is double, and if you add an int and a long value, the result is long.

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.