Java SE Learning (ii) arrays and strings, data types

Source: Internet
Author: User
Tags dateformat java se

First, array and string characteristics

1, once the array is initialized, its capacity is limited

2, the string once initialized, its length is fixed, the content is fixed, if you want to change the value, you can only discard the original memory space, re-request memory space.


Ii. types of data type conversions

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

Iii. conversions between simple data types

In Java, Integer, real, and character types are treated as simple data type, which are made up of low-to-high-level
(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

3.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.
3.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.

3.3 Wrapper class transition type conversion


Basic data Types the corresponding packaging class
Byte Byte
Short Short
Int Integer
Long Long
Char Character
Float Float
Double Double
Boolean Boolean

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:

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. namely:
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. By using this method, the conversion between different numerical variables can also be realized,

For example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision variable.

Iv. 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 Integer class

Float f1=new Float (F1); Generate Float class

Double D1=new double (D1); Generating a double class

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);

wrapper Class A common function is to convert the data type of the string type to the corresponding base data type , the following example:
String str = "123";
int a = Integer.parseint (str);

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.

Vi. Conversion of date classes to other data types

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, such as 20020324,

Import Java.text.simpledateformat;import java.util.*;p ublic class Helloworld {public    static void Main (string[] args) {    date date = new Date ();    If you want to get YYYYMMDD format    simpledateformat sy1=new simpledateformat ("YyyyMMdd");    String Dateformat=sy1.format (date);    If want to separate get 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);    System.out.println (dateformat+ "\ n" +syear+smon+sday);}            }

Output:

20141106
20141106





Java SE Learning (ii) arrays and strings, data types

Related Article

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.