当前位置:网站首页>Packaging class (mutual conversion between types)
Packaging class (mutual conversion between types)
2022-07-24 13:13:00 【No fish in Haikou】
Packaging
- Packaging class in java.lang As a basic class , They appear to solve the problem that basic types cannot
- Directly participate in object-oriented development , So that basic types can " object " There is a form of .
int a = 123;
Integer i1 = new Integer(a); // Convert a basic type to a reference type
Integer i2 = Integer.valueOf(a);
The wrapper class has a static method :parseXXX You can convert strings to basic types
The premise is that the string correctly describes the value that the basic type can save , Otherwise, an exception will be thrown :
NumberFormatException: The number format is abnormal
String num = "123";
int d = Integer.parseInt(num);
System.out.println(d);
double dou = Double.parseDouble(num);
System.out.println(dou);
long lmax = Long.MAX_VALUE; //long The maximum of
System.out.println(lmax);
long lmin = Long.MIN_VALUE; //long The minimum value of
System.out.println(lmin);
double dmax = Double.MAX_VALUE; //double The maximum of
System.out.println(dmax);
double dmin = Double.MIN_VALUE; //double The minimum value of
System.out.println(dmin);
JDK1.5 A new feature was introduced : Automatic disassembly box
- This feature is recognized by the compiler, not java Virtual machine approved , When the compiler compiles the code, it finds that there are basic types and
- The conversion code will be automatically added when the reference types are assigned to each other , In this way, we no longer need coding to complete mutual conversion in the source code
int d = 123;
/* The following code will trigger : Auto boxing feature , The compiler will complete the conversion code Integer i = Integer.valueOf(d); */
Integer i = d;
/* Automatic dismantling d = i.intValue(); */
d = i;
System.out.println(i);
System.out.println(d);

Point p = new Point(1,2);
The operation of outputting the content of an object to the console in development is very frequent , And this method outputs objects
The output is this object toString String returned by method .
Object This method is defined in , therefore java All classes in have toString, But if
Improper rewriting , Then use Object The string format that will be returned when this method is provided is :
Class name @ Address , This is of little help to our development
notes :java API Most of the provided classes have been overridden toString Method , Usually it's just ourselves
The definition class needs to rewrite this method itself when using it is not ideal
*/
System.out.println(p);
/* java The result of any type connected with a string in is a string , The actual operation is to call Of instances of this type toString Method and connect the returned string with the string */
String line = " This object is "+p;
System.out.println(line);
/* Object Provides a way equals, The purpose is to compare two objects " Content " Are they the same? For reference type variables, the address of the object is saved ,"==" Compare the values of variables , therefore Is to compare addresses , Only two reference type variables have the same address ( Point to the same object ) when For true and equals It doesn't matter whether they are the same object , What we judge is whether their contents are the same notes :Object Of equals For internal use "==" Realization , So it doesn't make sense if the class doesn't rewrite this method . */
Point p2 = new Point(1,2);
System.out.println(p==p2); // Than the address , Determine whether it is the same object
System.out.println(p.equals(p2));
边栏推荐
- Case summary of SSH service suddenly unable to connect
- EAS environment structure directory
- About thread (3) thread synchronization
- Implement is by yourself_ default_ constructible
- Deep and shallow copies of objects, extends
- Knowledge sharing | sharing some methods to improve the level of enterprise document management
- vscode配置用户代码片段(包括删除方法)
- The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
- 1.9. touch pad test
- Analysis of device restart jamming -reboot jamming
猜你喜欢
![[stm32] internal independent watchdog iwdg](/img/7f/752528e9a76097b51c8f9463c33db3.png)
[stm32] internal independent watchdog iwdg

Implement is by yourself_ default_ constructible

Knowledge sharing | sharing some methods to improve the level of enterprise document management

EAS environment structure directory
![[paper reading] mean teachers are better role models](/img/94/f7846023d38c91d803349f43d8d414.png)
[paper reading] mean teachers are better role models

Digital intelligence integration accelerates enterprise business innovation

leetcode第 302 场周赛复盘

The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28

Handler learning

3. Realize snake and basic game interface
随机推荐
Modern data architecture selection: Data fabric, data mesh
Pointer advanced part (1)
On node embedding
现代数据架构选型:Data Fabric、Data Mesh
Get the current month and year and the previous 11 months
SSM online rental and sales platform multi city version
Search engine based on boost library
Windivert:可抓包,修改包
2022.07.15 暑假集训 个人排位赛(十)
Relevant laws of animation movement (judge where to move according to parameters)
Usage of swipemenurecyclerview
EAS approval process related table
Windivert: capture and modify packages
Learn the calculation method of quantile value in n minutes
Teach you how to use power Bi to realize four kinds of visual charts
如何画 贝赛尔曲线 以及 样条曲线?
Symbol
Static attribute, super()
Digital intelligence integration accelerates enterprise business innovation
jsonp