Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java Foundation - implicit conversion vs cast     - How Linux Log Analysis (Linux)

- Use libpq under Ubuntu 14.04 (Linux)

- To explore the Android ListView caching mechanism again (Programming)

- Linux server Php injection prevention (Linux)

- Debian (Wheezy) Install Java environment / replace OpenJDK as the SUN JDK (Linux)

- Parameters of the extra port MySQL 5.6 (Database)

- Easy to install CentOS 6.6 desktop environment (Linux)

- Setting grep Highlight Matches (Linux)

- Shell script on the variables with double quotation marks grep small problem (Programming)

- To use slay kill user processes (Linux)

- ElasticSearch basic usage and cluster structures (Server)

- Linux5 install MySQL 5.6 (RPM mode) (Database)

- HttpClient4 usage upgrade from HttpClient3 (Programming)

- How VMware extend disk without rebooting the system (Linux)

- Linux command ls (Linux)

- Build RubyMine + Ruby On Rails + MySQL development environment under Windows (Server)

- Docker Private Registry Installation Guide at CentOS6.X (Linux)

- Linux compression and decompression command (Linux)

- Neo4j map data processing tab (Database)

- How Datadog monitor Nginx (Server)

  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.
- TCP protocol flaw incomplete records (Linux)
- Ambari and Hadoop configuration, management and monitoring of the project Getting (Server)
- About ORA-02391 solution (Database)
- CentOS 5.x install Lua 5.2.3 error (LIBS = -lncurses) (Linux)
- Linux disk partition, format, mount the directory (Linux)
- Install Java on RHEL6 (Linux)
- Linux Detailed instructions alias settings (Linux)
- How common Linux automation tasks (Server)
- Linux dd command applies amplification SWAP partition (Linux)
- Docker study notes (Server)
- Docker data container protection the pros and cons (Server)
- OpenStack package problems and solutions under CentOS6 (Linux)
- Object Oriented Programming Java reflection (Programming)
- Depth understanding of the TCP protocol (Database)
- Virtualization and IT cooperation (Linux)
- Linux system last command usage (Linux)
- RHEL 5.7 Yum configure local source [Errno 2] No such file or directory (Linux)
- The Java way to stop a thread of execution (Programming)
- The method of installing software under Ubuntu Linux (Linux)
- Python variable type (Programming)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.