This article source: making here | | GitEE, click here
First, basic types
1. Basic types
It is more efficient to declare a variable that is not passed by reference, and the value of the variable is placed directly on the stack, and the size does not vary with the runtime environment. Reference objects created with new are stored in the heap.
2. Basic information
The basic types are as follows: byte, short, int, long, float, double, Boolean, and char. You can use related methods to view the range size.
public class IntType01 {
public static void main(String[] args) {
System.out.println("Base number:"+Integer.SIZE);
System.out.println("Minimum:"+Integer.MIN_VALUE);
System.out.println("Maximum value:"+Integer.MAX_VALUE);
System.out.println("Base number:"+Double.SIZE);
System.out.println("Minimum:"+Double.MIN_VALUE);
System.out.println("Maximum value:"+Double.MAX_VALUE); }}Copy the code
Ii. Case usage
1. Type conversion
Automatic conversion: A small range of data types can be automatically converted to a large range of data types.
Cast: To convert one data type to another.
Type promotion: Expression operations have different data types, and the type is automatically promoted to a wide range.
public class IntType02 {
public static void main(String[] args) {
// Automatic conversion
int i = 112 ;
long j = i ;
System.out.println(j);
// Cast
double d = 13.14 ;
int f = (int)d;
System.out.println(f);
// Type promotion
longr = i * j ; System.out.println(r); }}Copy the code
Note: The biggest concern in type conversions is scope size.
2. Wrapper type
The primitive data types are not object-oriented, hence the wrapper types, and the wrapper adds more properties and methods, and the automatic wrapper function converts the primitive to the wrapper type. Java provides a wrapper class for each primitive type, Integer, Double, Long, Boolean, Byte, and so on.
public class IntType03 {
public static void main(String[] args) {
Integer int1 = null ;
Double dou1 = 13.14 ;
Long lon1 = 123L; }}Copy the code
The default value of the Integer variable is null, indicating that Integer can tell the difference between an unassigned value and a value of 0, like the difference between a score of 0 on an exam and no exam.
3. Character types
A char variable is used to store Unicode encoded characters, which include Chinese characters.
public class IntType04 {
public static void main(String[] args) {
char cha1 = 'know'; System.out.println(cha1); }}Copy the code
Note: There may be special rare words that are not included in the Unicode encoding character set.
4. Assignment and operations
Short s1=1; S1 = s1 + 1 and short s1 = 1; s1+=1; The problem.
public class IntType05 {
public static void main(String[] args) {
short s1 = 1 ;
// s1 = s1 + 1 ; // error: s1 automatically converts to int
s1 += 1; System.out.println(s1); }}Copy the code
The += operator is specified by the Java language and is recognized by the compiler so it compiles correctly.
5. Boolean types
Two logical values, true and false, are commonly used to represent the result of a relational operation.
public class IntType06 {
public static void main(String[] args) {
// Error: 0.30000000000000004
System.out.println(3*0.1);
// true
System.out.println(0.3= =0.3);
// false
System.out.println(3*0.1= =0.3); }}Copy the code
Float and Dubble
1. Basic concepts
The relationship and distinction between these two types may be largely unintelligible, but it is important to understand a few basic concepts first.
Floating point: Used in computers to approximate any real number. Specifically, the real number is obtained by multiplying an integer or fixed-point number by an integer power of some radix (usually 2 in computers)
Single-precision floating-point numbers: Single-precision floating-point numbers are used to represent real numbers with fractional parts and are generally used in scientific calculations. Occupies 4 bytes (32 bits) of storage space
Double: A double is a data type used by computers that uses 64 bits (8 bytes) to store a floating point number.
2. Comparative analysis
- Float Basic Description
Bits: 32 Minimum value: 1.4E-45 Maximum value: 3.4028235E38Copy the code
- Double basic description
Bits: 64 Minimum value: 4.9E-324 Maximum value: 1.7976931348623157E308Copy the code
- Case description
Float and double declare and transform related demonstration cases.
public class IntType07 {
public static void main(String[] args) {
/ / float statement
float f1 = 12.3 f ;
/ / double statement
double d1 = 13.4 ;
// A cast is required for a downward transition
float f2 = (float) d1 ;
System.out.println("f1="+f1+"; d1="+d1+"; f2="+f2); }}Copy the code
Four, high precision type
1, BigInteger
Support any size of the integer operation, and there will be no loss in the operation process, there is no corresponding basic type, operation will become relatively complex, operation speed will naturally decline.
2, BigDecimal
Support fixed-point numbers with arbitrary precision, often used for precise currency calculations, which are often mandatory in the daily development of a company.
public class IntType08 {
public static void main(String[] args) {
BigDecimal dec1 = new BigDecimal(3.0); BigDecimal dec2 =new BigDecimal(2.11);// Exact addition
BigDecimal res1 = dec1.add(dec2) ;
System.out.println(res1);
// Subtract exactly, and intercept the result
// HALF_UP: round
BigDecimal res2 = dec1.subtract(dec2);
System.out.println(res2.setScale(1, RoundingMode.HALF_UP));
// Exact multiplication
BigDecimal res3 = dec1.multiply(dec2) ;
System.out.println(res3.doubleValue());
// Divide exactly, and intercept the result
// ROUND_DOWN: Intercepts data by reserved bits
BigDecimal res4 = dec1.divide(dec2,2,BigDecimal.ROUND_DOWN); System.out.println(res4); }}Copy the code
Five, source code address
Making address GitEE, https://github.com/cicadasmile/java-base-parent, https://gitee.com/cicadasmile/java-base-parentCopy the code