A simple Sessionbean creation process (JBX+WL8.1SP3)

Source: Internet
Author: User
Tags date format constructor integer numeric value variables variable tostring
session| Create | Process Some novice Java friends may experience the distress of conversion between Java data types, such as conversion between integers and float,double, conversions between integers and string types, and handling, display time problems, and so on. The following author on the development of some of the experience introduced to everyone.
We know that Java data types are grouped into three categories, Boolean, character, and numeric, and the numeric type is divided into integers and floating-point types, and the Java variable type is Boolean, as opposed to the data type; character char; integer byte, short, int, long ; float float, double. The 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, string and date. The mutual conversion between these variable types is often used in our programming, and in the following discussion we will explain how to implement these transformations.
1 Kinds of data type conversions \ r
The conversion of Java data types is generally divided into three categories, respectively:
(1). Conversion between simple data types
(2). Conversion of strings to other data types
(3). Other practical data type conversions
Here we discuss the three types of conversions separately.
2 conversions between simple data types
In Java, integral, solid, and character types are treated as a simple data type, from low-level to advanced, respectively
(Byte,short,char)--int--long--float--double
The conversion between simple data types can be divided into:
Low-level to advanced automatic type conversions
Advanced to low-level forced-type conversions
Wrapper class transition type can be converted
2.1 Automatic type Conversion
Lower-level variables can be directly converted to advanced variables, which I call automatic type conversion, for example, the following statements can be passed directly in Java:

byte b;int i=b;long l=b;float f=b;double d=b;

If the low-level type is char, the conversion to the Advanced Type (integer) 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 peers, so they cannot be automatically converted to each other, and you can use the following coercion type conversions.

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 integral type, so if you are working with integer data, it is best to use the int type uniformly.
2.2 Coercion Type conversion
When you convert an advanced variable to a low-level variable, the situation is a bit more complicated, and you can use coercion 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 can be imagined, this conversion is certainly likely to lead to overflow or precision degradation, so I do not recommend the use of this conversion.
2.3 Wrapper class Transition type conversion
As we discuss the conversion of other variable types, we need to understand the Java wrapper class, the so-called wrapper class, is that you can directly represent a simple type of variable as a class, in the execution of variable types of mutual conversion, we will use the bulk of these wrapper classes. Java has a total of six wrapper classes, Boolean, Character, Integer, long, float, and double, literally we can see that they correspond to Boolean, char, int, long, float and double. and string and date themselves are classes. So there is no concept of packaging class.
When converting between simple data types (automatic or cast), we can always use the wrapper class for intermediate transitions.
In general, we first declare a variable, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for type conversion. For example:
Example 1, when you want to convert a float to a double type:

float f1=100.00f; Float f1=new Float (F1); Double D1=f1.doublevalue ();//f1.doublevalue () method for returning a double value of float class

When you want to convert the double type to an int type:

Double d1=100.00; Double D1=new double (D1); int I1=d1.intvalue ();

When you want to convert an int to a double, automatically converts:

int i1=200; Double d1=i1;

A variable of a simple type is converted to the appropriate wrapper class, which can take advantage of the constructor of the wrapper class. That
Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value
and in each wrapper class, the total visible as Xxvalue () method, to get its corresponding simple type of data. By using this method, we can also realize the transformation 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 the classes in the class library, you can see that all classes derived almost from the Java.lang.Object class provide the ToString () method that converts the class to a string. For example, the ToString () method ToString () method for classes such as Characrer,integer,float,double,boolean,short is used to convert classes such as 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 \rfloat f1=new float (F1); Generate float class \rdouble d1=new Double (D1); Generates a double class \r//the ToString () method of the wrapper class, respectively, into a 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);

Convert character type to other data type directly as numeric value
There are actually two kinds of correspondence between converting character variables to numeric variables, which we actually convert to the corresponding ASCII code in the transformation we're talking about in the first part, but sometimes we need another transformation relationship, for example, ' 1 ' means the value 1, not its ASCII code, and 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 integral type and the date class. You can just use the int type to represent the year, month, day, time, minute, and second, so that you have a corresponding relationship between the two, and you can use the three forms of the date class constructor when making this transition:
Date (int year, int month, int date): For years, months, and days in int
Date (int year, int month, int date, int hrs, int min): type int to represent years, months, days, hours, minutes
Date (int year, int month, int date, int hrs, int min, int sec): Years, months, days, times, minutes, seconds, in int type \ r
An interesting correspondence between the long and the date classes is to represent a time as the number of milliseconds from 0:0 Greenwich Mean Time of January 1, 1970 to 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 in the date class you can use the Date class 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 long integer corresponding to the time we mentioned earlier, and like the wrapper class, the date class also 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, and we can use the following methods, starting with the introduction of 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 wish to be separated by the 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);

Conclusion:
Of course, the author's discussion is only one person's view, if you want more information about Java data type conversion knowledge, I suggest referencing Java class library java.util.* below the
Integer class \ r
Boolean class \ R
Character class \ R
Float class \ R
Double class \ R
String class \ R
Date class
Depending on your needs, refer to the various member methods of different classes for conversion between 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.