Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java Foundation - implicit conversion vs cast     - Row-level security and application-level solutions for the new features of PostgreSQL9.5 (Database)

- PHP Performance Analysis and Experiment: Performance Micro Analysis (Programming)

- C ++ Object Model Comments (Programming)

- Linux Kernel 4.2.2 compiler installation tutorial (Linux)

- Perl said method B if A judge (Programming)

- Nginx start, stop, smooth start, smooth upgrade (Server)

- Configuration based on open source Lucene Java development environment (Server)

- WordPress blog installation Redis Cache (Server)

- Using 30 seconds to write a detailed analysis of garbage removal system (Linux)

- Linux security configuration (Linux)

- SELinux security permissions HTTP + PHP service changes (Server)

- React Native (Programming)

- Linux port mapping system (routing and forwarding) (Server)

- KVM QEMU virtual machine installation configuration under CentOS (Linux)

- How Linux system password security guarantee (Linux)

- How to find on Linux and delete duplicate files: FSlint (Linux)

- Those things packaged using Gradle to Android (Programming)

- Linux iptables firewall and vsftpd to resolve the issue (Linux)

- KVM installation under CentOS 5.5 (Linux)

- Using Linux / Unix Text processing (Linux)

  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.
- GDB remote connections RX Probe online debug program (Programming)
- Linux Powerful command Awk Introduction (Linux)
- xCAT deployment (Linux)
- Jigsaw project will solve the problem of Java JAR hell Mody (Programming)
- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)
- Linux system with a firewall to prevent the DOS attack (Linux)
- RedHat 6.5 installation and deployment Openfire (Server)
- HTML5 postMessage cross-domain data exchange (Programming)
- Linux startup and logon security settings (Linux)
- PHP security Programming Advice (Programming)
- MySQL 5.6 database code changes (Database)
- Linux file permissions bit forced bits and adventure Comments (Linux)
- Intel Graphics Installer installation on Ubuntu 15.10 (Linux)
- Distributed transaction management Spring declarative transactions (Programming)
- Linux Operating System Security Management Experience (Linux)
- Linux System Getting Started Learning: On Linux how to convert text files to PDF (Linux)
- How to use the Linux command compress JPEG images (Linux)
- Experts teach you safety precautions Linux Sniffer (Linux)
- I like Linux Security (Linux)
- ethtool command Detailed (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.