Java Data types

Source: Internet
Author: User

1.1 Why Java needs to keep basic data types

Http://www.importnew.com/11915.html

The basic data types are not of much use to most business-related or network applications, which are typically in client/server mode with a database on the backend. However, the basic data types are essential for applications that are primarily numeric-based.
Since the release of Java in 1996, the basic data type is part of the Java language, so Java is a two-type system that has basic data types and object types, referred to as basic types and objects. There are 8 predefined basic types in Java, each with a corresponding object wrapper class. Base types are based on values, and object types are based on references.

1.2 Basic data types for Java (primitive)

Java provides eight basic types, including six numeric types (four integers, two floating-point types), a character type, and a Boolean type.
Integral type: short, int, long, byte;
Float type: float, double;
Character type: char;
Boolean type: Boolean.
Numeric types in Java do not have unsigned, their range is fixed, and will not change as the machine hardware environment or operating system changes. For the range of values of the base type of a numeric type, there is no need to force memory, and their values are already defined in the corresponding wrapper class in a commonsense manner.

short 4byte= 32bit
Basic Type size the corresponding packaging class Minimum Value Maximum Value
byte 8-bit Java.lang.Byte -128 +127
2byte= 16bit Java.lang.Short -32768 +32767
int 4byte= 32bit Java.lang.Integer -2^31 +2^31-1
long 8byte= 64bit Java.lang.Long -2^63 +2^63-1
float Java.lang.Float 1.4e-45 3.40e38
double 8byte= 64bit Java.lang.Double 4.9e-324 1.79e308
char 2 Byte=16bit Java.lang.Character 0 65535
boolean - Java.lang.Boolean - -

For more information about Java basic types, see
Http://www.cnblogs.com/kelin1314/archive/2010/09/11/1824145.html
http://blog.csdn.net/bingduanlbd/article/details/27790287

1.3 Java basic data type conversions

The Java language is a strongly typed language. There are several requirements for strongly typed languages:
(1) A variable or constant must have a type, and can only be used after the declaration;
(2) The type must be identical when assigning values, the type of the value must be exactly the same as the type of the variable or constant;
(3) The type of the operation must be the same, the data type of the participating operation must be consistent to operate.
However, in practical applications, it is often necessary to operate between different types of values, which requires the conversion of data types.
There are two types of data type conversions:
(1) Automatic type conversion: The compiler automatically completes the type conversion, does not need to write code in the program;
Rule: From a type with a small storage scope to a type with a large storage range.
Specific rules: Byte→short (char) →int→long→float→double.
(2) Coercion type conversion: Forces the compiler to type-convert and must write code in the program. This type of conversion is likely to present a loss of precision.
Rule: A type from a large storage range to a small storage range.
Specific rules: Double→float→long→int→short (char) →byte.
Http://www.cnblogs.com/ggjucheng/archive/2012/11/20/2779081.html

1.4 Conversions between the base data type and the wrapper class:

The basic variables are packaged into wrapper class objects by passing parameters to the corresponding wrapper, and the wrapper class object is transformed into a basic type variable using the Xxxvalue () method provided by the wrapper class.
Conversions such as int and integer:
int goes Integer:integer integer = new integer (int);
Integer turn Int:int i = Integer.intvalue ();
Double turn double:double B = double.doublevalue ();
float to float:float C = Float.floatvalue ();
......

wrapper classes can implement conversions between basic type variables and strings:
In addition to character, all wrapper classes provide the Parsexxx () method to convert a specific string to a primitive type variable, and the string class provides the valueof () method to convert the base type to a string.

Java Data types

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.