Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java Foundation - implicit conversion vs cast     - Linux novice common commands (Linux)

- How to adjust the system time CentOS (Linux)

- JavaScript function closures Quick Start (Programming)

- Linux Network Programming - raw socket instance: MAC header message analysis (Programming)

- Ubuntu 14.04 LTS NTFS partition can not access solution (Linux)

- Use Markdown editor for document work under Linux (Linux)

- Install Java JDK 8 in CentOS 7 / 6.5 / 6.4 (Linux)

- iostat command Detailed (Linux)

- Ubuntu installed racing game Speed Dreams 2.1 (Linux)

- E: Sub-process / usr / bin / dpkg returned an error code (1) error solution (Linux)

- Nginx version of helloworld (Server)

- How to implement large-scale distributed Yahoo depth study on the Hadoop cluster (Server)

- To compile and install OpenCV-2.3.1 FFmpeg-2.1.2 under CentOS (Linux)

- Ubuntu users to install the system indicator SysPeek 0.3 (Linux)

- The simple multi-threaded Python (Programming)

- Install Oracle JDK 8 and JVM class loading mechanism in Linux (Linux)

- IIS virtual host of safety knowledge (Linux)

- HTTPS and SSH and use the difference between the way: Git User's Manual (Linux)

- Fedora 19/20 and Debian Jessie / Sid users to install FFmpeg 2.3.2 (Linux)

- Story timestamp and time zones: daily programmer (Programming)

  Java Foundation - implicit conversion vs cast
  Add Date : 2017-08-31      
  In the definition of variables, there are many problems to be aware, accidentally there will be loss of accuracy or incompatibility types and other issues.
    1. When defining long data, you must add the suffix l or L
              Long l = 123456789012345L
    2. When defining a single precision type (7-8 significant digits), the suffix f or F must be added
              Float f = 12.5F
    3. boolean type can not be converted to other data types.
Of these, we often encounter data type conversion problem, the most common to be an implicit conversion and coercion, and we analyze.
Implicit conversion
From small to large, can be implicit conversion, the data type will automatically upgrade.
Byte, short, char -> int -> long -> float -> double
Note: long is 8 bytes, float is 4 bytes.
Long is an integer, float is a floating-point, integer and floating-point storage rules are not the same, remember that the scope of a little long is less than float.
example :
Byte a = 10;
Int b = a;
When compiling intb = a, a is implicitly converted to an int.
Forced conversion
     From large to small (if you explicitly know that the data can be used to represent the data type, you can use the cast)
    (Converted data type) variable or value.
Note: Under normal circumstances, do not recommend the use of mandatory type conversion.
           example 1 :
Int a = 10;
Byte b = (byte) a;
When compiling byte b = (byte) a, a is cast to byte.
 Class QiangZhiDemo
  Public static void main (String [] args)
   Byte b = (byte) 130;
   System.out.println (b); // Print the result -126

Data 130 is the default type of int decimal data,
The first step: decimal 130 converted to binary data.
The second step: 130 in memory is represented as follows
Original: 0000000000000000 00000000 10000010
The third step: seeking int130 complement
Because 130 is a positive number, so the code and complement are the original code and consistent.
Complement: 0000000000000000 00000000 10000010
The fourth step: the complement code to intercept, leaving only the last 8.
(Byte) 130 of the complement is: 10000010
The fifth step: the complement code into the original code.
Since the sign bit (first bit) is 1, the number is negative,
Counter-code: 10000001 (complement-1)
Original code: 11111110 (sign bit unchanged data bit inversion)
Converted to decimal -126, so the final print -126.
     Example 3:
Shorts = 1;
S = s + 1;
Shorts = 1;
S + = 1;
Is there a problem? Why?
The first program will report error: Error: Incompatible type: From int to short there may be losses

Reason: s = s + 1; s + 1 will be implicitly converted to int type, when an int type assignment to the short type is, may be lost.
The second program can compile and run.

S + = 1, although there can be seen as s = s +1, but there are differences, s + = 1 in a coercion, namely s = (short) (s + 1) Cast to short type, it will not error.
Data type conversion problems If some small program, we may be able to see at a glance, but when writing a large system, with a huge amount of data, these small problems may lead to system errors or even collapse, so the previous code The rigor of preparation must rely on our own grasp.
- Several start-up mode of Tomcat (Server)
- RedHat Linux 6 desktop installation (Linux)
- General Linux interface server parameter tuning (Server)
- How to configure MongoDB replica set (Database)
- Linux yum command Detailed (Linux)
- IBM Data Studio to use ---- window displays all rows (Database)
- Protobuf compiled and used on the Ubuntu 14.04 (Programming)
- To_teach you three strategies to prevent the LAN IP address theft (Linux)
- Camera-based face recognition OpenCV crawl and storage format (Python) (Linux)
- GoldenGate update is missing (Database)
- Five useful commands to manage file types and system time in linux (Linux)
- Restore database fault encountered ORA-0600 (Database)
- The Linux OOM Terminator (Server)
- LVM management parameters commonly used commands explained in detail (Linux)
- Vim Common Command Summary (Linux)
- bash login and welcome message: / etc / issue, / etc / motd (Linux)
- Android project using the command to create and install the package (Programming)
- Ubuntu installation module Python rq (Linux)
- How to enhance the Nagios server security (Linux)
- Kali Linux virtualbox rc = Error 1908 workaround (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.