📄 wrapperclassdemo.java
字号:
/**
*A Wrapper class demonstration program
*2004.12.27.xhcprince
*/
class WrappedClassDemo
{
public static void main(String args[])
{
/*-------------------------------------------------------------------------
A wrapper class object is constructed by passing the value to the wrapped
into the appropriate constructor
Each primitive data has a corresponding wrapper class
-------------------------------------------------------------------------*/
int ptyp = 10;
Integer pobj = new Integer(ptyp);
System.out.println(pobj);
Integer x = new Integer(10);
int i = x + 16;
/*-------------------------------------------------------------------------
Note that the second line of the above code smippet is incorrect
The corrsct way to add an integer value with the one wrapped inside a
class is as follows:
(strangely there's no compile error in my computer?!)
-------------------------------------------------------------------------*/
int j = x.intValue() + 16;
System.out.println(i);
/*-------------------------------------------------------------------------
The wrapper classes have another important function,which is to group
together a set of methods that operate on actual primitive types
-------------------------------------------------------------------------*/
int t = 123456;
String t1 = Integer.toHexString(t);
String t2 = Integer.toOctalString(t);
String t3 = Integer.toBinaryString(t);
System.out.println(t1);
System.out.println(t2);
System.out.println(t3);
/*-------------------------------------------------------------------------
Wrapper classes also contain methods for converting primitive types to
Strings and vice versa
-------------------------------------------------------------------------*/
Double temp = new Double("11.66");
double result = temp.doubleValue();
System.out.println(result);
/*-------------------------------------------------------------------------
Wrapper classes also define constants for a given primitive type
-------------------------------------------------------------------------*/
System.out.println(Byte.MAX_VALUE);
System.out.println(Character.MAX_VALUE);//why the output is "??".
// System.out.println(Boolean.MAX_VALUE); //there's no MAX_VALUE in Boolean!
System.out.println(Short.MAX_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println(Double.MAX_VALUE);
/*-------------------------------------------------------------------------
Wrapper classes have some other useful methods such as follows:
parseInt():takes a String argument and returns the corresponding int.
toString():converts an int argument to String
(also the same with Double,Long,Short,Float...)
isInfinite():returns true if the value being tested is infinitely
large or small magnitude
isNaN():returns true if the value being tested is not! a number
NaN refers to "Not an Number"!
-------------------------------------------------------------------------*/
String s1 = "111";
int i1 = Integer.parseInt(s1);
System.out.println(i1);
int i2 = 666;
String s2 = Integer.toString(i2);
System.out.println(i2);
Double d1 = new Double(1/0.);
Double d2 = new Double(0/1.);
System.out.println(d1.isInfinite() + " " + d1.isNaN());
System.out.println(d2.isInfinite() + " " + d2.isNaN());
/*-------------------------------------------------------------------------
I'd like to omit the demonstration of some methods such as:
isDigit(),isLetter(char),isLetter(),isWhiteSpace(),
isLowerCase(),isupperCase()
toLowerCase(),toUpperCase().
--------------------------------------------------------------------------*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -