The Java language is a strongly typed language. This means that each variable must have a well-declared type. The Java language provides eight basic types. Six types of numbers ( four integers, two floating-point types), one character type, and one Boolean type. Java also provides a large number object, but it is not a Java data type.
1, integers:
Definition: Numbers with no decimal parts, negative numbers are allowed.
Category: Java provides four types of integers:
Byte 1 bytes (8bit)-128 to 127 ( -27~27-1) defaults to 0, as
BYTE b=28;
Short 2 bytes (16bit)-32,768 to 32,767 ( -215~215-1) defaults to 0, as
Short s=280;
int 4 bytes (32bit)-2,147,483,648 to 2,147,483,647 (( -231~231-1)) defaults to 0, as
A long 8 bytes (64bit) -9,223,372,036,854,775,808l to 9,223,372,036,854,775,807l ( -263~263-1) defaults to 0L, as
Long decimal= 12345L;
Description: The highest bit representation of the integer value, denoted by 0 for positive numbers, 1 for negative numbers, and the remaining bits to represent values.
Constants in Java that represent integers can be represented by an out-of-the-box binary.
(1) Decimal: If
int length=30;
(2) Octal: When using octal, you need to add 0to the front of the number, as
int five=05;
(3) 16 binary: When using hex, you need to add 0X or oxin front of the number, as
int x=0x7ff;
2, floating-point number:
Definition: Numbers that contain fractional parts.
Category: Java provides two types of floating-point numbers:
float 4 bytes (32bit) approx. +-3.40282347e+38f (6~ 7 valid decimal digits) default is 0.0F
Double 8 bytes (64bit) approx. +-1.79769313486231570e+308 (15 significant digits) default = 0.0D
Description:
1) floating-point constants default to double type . A value of type float has a suffix of f or f, and if there is no suffix F, the default is double. A numeric value of type Double can also use the suffix d.
2) Occurs when these numbers encounter a range error (overflow overflow), and underflow (underflow) occurs when an image is removed by 0.
3, character type:
Definition: Single quotation marks are used to denote char constants, each char variable accounts for 16 bits ( 2 bytes ). For example,
Char a= ' a ';
Description
1) double quotes indicate a string , which is an object of Java and not a data type.
2) char type represents a character in a Unicode encoding scheme , default to ' \u0000 ', two byte (16bit) range (' \u0000 ' ~ ' \uffff ')
Unicode can contain 65,536 characters, Ascii/ansi contains only 255 characters, and is actually a subset of Unicode. Unicode characters are usually expressed in hexadecimal encoding schemes, ranging from ' \u0000 ' to ' \uffff ' . \u0000 to \U00FF represents the Ascii/ansi character. \u indicates that this is a Unicode value.
3) in Java, in addition to this \u form to represent characters, you can also use a code-changing sequence to represent special characters.
\b Backspace \u0008 \ t tab tab \u0009 \ n line break \u000a \ r hard Return \u000d
\ "Double quote \u0022 \ ' single quote \u0027 \ \ backslash \u005c
4) In theory, Unicode characters are used in Java applications and applets, but whether they can actually be displayed depends on the browser and operating system in use, where the operating system is the most fundamental.
5) The character type is actually a 16-bit unsigned integer, or it can be assigned using integers, but the integer range is between 0~65535, such as
Char a=78; Denotes capital letter ' N '
4. Boolean type:
The Boolean type has only two values: false and True, one byte (8bit), and the default is False. Range (True,false)
Conversion of data types
Original link: http://java.chinaitlab.com/base/725590.html
Some novice Java friends may encounter problems with the conversion of Java data types, such as conversions between integers and float,double, conversions between integers and string types, and processing, display-time issues, and so on. The following author on the development of some of the experience introduced to everyone.
We know that Java data types are divided into three categories, namely, Boolean, character, and numeric, where the numeric type is divided into integer and floating point type, and the Java variable type is Boolean Boolean with respect to the data type char; integer byte, short, int, long Floating-point float, double. Of these, four types of integer variables and two floating-point variables correspond to different precision and range respectively. In addition, we often use two kinds of variables, namely, string and date. The conversions between these variable types are often used in our programming, and in the following discussion we will explain how to implement these transformations.
1 Types of data type conversionsClass
The conversion of Java data types is generally divided into three types, namely:
(1). Conversions between simple data types
(2). Conversion of strings to other data types
(3). Other useful data type conversions
Here we discuss the three types of conversions separately.
2 conversions between simple data types
In Java, Integer, real, and character types are treated as simple data type, from low to Advanced (Byte,short,char)--int--long--float--double
Conversions between simple data types can also be categorized as:
Low-to-advanced automatic type conversion
advanced to low-level coercion type conversions
Wrapper class transition types can be converted
2.1 Automatic type conversion
Low-level variables can be directly converted to advanced variables, which I call automatic type conversion, for example, the following statement can be directly in Java through:
BYTE B;
int i=b;
Long l=b;
float f=b;
Double d=b;
If the low-level type is char, conversion to the advanced type (int) is converted to the corresponding ASCII value, for example R
Char c= ' C ';
int i=c;
System.out.println ("Output:" i);
Output: output:99;
For Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can be cast using the following coercion type.
Short I=99;char c= (char) i; System.out.println ("Output:" C);
Output: OUTPUT:C;
However, according to the author's experience, byte,short,int three types are integer, so if you manipulate integer data, it is best to use the int type uniformly.
2.2 Coercion of type conversions
When you convert an advanced variable to a low-level variable, the situation becomes more complex, and you can use forced type conversions. That is, you must use the following statement format:
int i=99;byte b= (byte) I;char c= (char) i;float f= (float) i;
As you can imagine, this conversion is certainly likely to result in a drop in overflow or precision, so the author does not recommend using this conversion.
2.3 Wrapper class transition type conversion
As we discuss the conversion of other variable types, we need to look at the wrapper class of Java, the so-called wrapper class, that is, you can directly represent a simple type of variable as a class, we will use a lot of these wrapper classes when performing the conversion of variable types. There are six wrapper classes in Java, namely Boolean, Character, Integer, long, float, and double, literally we can see that they correspond to Boolean, char, int, long, float and double. String and date itself are classes. So there is no concept of packaging class.
When converting between simple data types (auto-convert or cast), we can always use wrapper classes for intermediate transitions.
In general, we declare a variable first, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for the type conversion. For example:
Example 1, when you want to convert the float type to double type:
float f1=100.00f;
Float f1=new Float (F1);
Double D1=f1.doublevalue ();
F1.doublevalue () is a method of returning a double value type for the float class
When you want to convert a double type to an int type:
Double d1=100.00;
Double D1=new double (D1);
int I1=d1.intvalue ();
When you want to convert the int type to double type, the conversion is automatic:
int i1=200;
Double d1=i1;
A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That
Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value
And in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion between different numerical variables, for example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision real variable.
3 Conversion of string type to other data types
By looking at the member methods provided by each class in the class library, you can see that almost all classes derived from the Java.lang.Object class provide the ToString () method, which converts the class to a string. For example, the ToString () method of the Characrer,integer,float,double,boolean,short class is used to convert characters, integers, floating-point numbers, doubles, logical numbers, and short integers to strings. As shown below:
int i1=10;
float f1=3.14f;
Double d1=3.1415926;
Integer i1=new integer (i1);
Generate an Integer class R
Float f1=new Float (F1);
Generate Float Class R
Double D1=new double (D1);
Generates a double class r
Call the ToString () method of the wrapper class to convert to a string, respectively
String si1=i1.tostring ();
String sf1=f1.tostring ();
String sd1=d1.tostring ();
Sysytem.out.println ("SI1" SI1);
Sysytem.out.println ("SF1" SF1);
Sysytem.out.println ("SD1" SD1);
4. Convert character type directly to other data types
The conversion of a character variable to a numeric variable actually has two correspondence, in which we actually convert it to the corresponding ASCII code in the first part of the conversion, but we sometimes need another transformation relationship, for example, ' 1 ' means the value 1, not its ASCII code, for this conversion, We can use the character getnumericvalue (char ch) method.
5, the date class and other data types of mutual conversion
There is no direct correspondence between the integer and date classes, except that you can use the int to represent the year, month, day, time, minute, and second, thus establishing a correspondence between the two, in which you can use the three forms of the date class constructor:
Date (int year, int month, int date): type int for years, months, and days
Date (int year, int month, int date, int hrs, int min): int, month, day, time, minute
Date (int year, int month, int date, int hrs, int min, int sec): type int for years, months, days, hours, minutes, seconds R
There is an interesting correspondence between the long and the date classes, which is to represent a time as a number of milliseconds from 0:0 GMT, January 1, 1970, 0 seconds. For this correspondence, the date class also has its corresponding constructor: date (long date)
Gets the year, month, day, time, minute, second, and week of the date class you can use the date class's getyear (), GetMonth (), GetDate (), getHours (), getminutes (), getseconds (), GetDay ( ) method, you can also interpret it as converting the date class to int.
The GetTime () method of the date class can get the number of long integers that we said earlier, and as with the wrapper class, the date class has a ToString () method that can convert it to a string class.
Sometimes we want to get a specific format for date, for example 20020324, we can use the following methods, first introduced in the file,
Import Java.text.SimpleDateFormat;
Import java.util.*;
Java.util.Date Date = new Java.util.Date ();
/If you want to get the YYYYMMDD format
SimpleDateFormat sy1=new SimpleDateFormat ("YyyyMMDD");
String Dateformat=sy1.format (date);
If you want to get a separate year, month, day
SimpleDateFormat sy=new SimpleDateFormat ("yyyy");
SimpleDateFormat sm=new SimpleDateFormat ("MM");
SimpleDateFormat sd=new SimpleDateFormat ("DD");
String Syear=sy.format (date);
String Smon=sm.format (date);
String Sday=sd.format (date);
Additional:
In addition to these transformations, the basic data types in Java can be implicitly converted to string, for example:
System.out.print ("Convert" +100);//If there is a string in front of the data with + Connect//will be implicitly converted to
String
1. Character type turn time type: 2005-10-1
Reportdate_str = "2005-10-01";
Reportdate_str = reportdate_str + "00:00:00.0"
Reportdate = Java.sql.Timestamp.valueOf (REPORTDATE_STR);
2. Time-type turn character type:
V_date = Reportdate.tostring ();
3. Converting a long type to a string type
Long App_unit = (long) userview.getdept_id ();
String s_app_unit = string.valeof (app_unit);
The basic type S can be converted to string type by string.valeof (s).
Java Basic data type conversions