当前位置:网站首页>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));
边栏推荐
- [C language] dynamic memory management
- Data + AI summit 2022 PPT download
- LeadTools 22 kit LeadTools super set
- 如何画 贝赛尔曲线 以及 样条曲线?
- Knowledge sharing | sharing some methods to improve the level of enterprise document management
- How to quickly learn Embedded
- 29. Right view of binary tree
- 2022.07.15 暑假集训 个人排位赛(十)
- 20201127 use markdown to draw UML diagrams, graphviz installation experience hematemesis finishing
- How to draw Bezier curve and spline curve?
猜你喜欢

Digital intelligence integration accelerates enterprise business innovation

Cluster construction based on kubernetes v1.24.0 (III)

34. Add two numbers

SSM在线考试系统含文档
![[datasheet] interpretation of phy lan8720 network chip](/img/77/b99c926db2c99df32cecb188c1f1e1.png)
[datasheet] interpretation of phy lan8720 network chip
![[C language] detailed knowledge of document operation](/img/2a/f2976d80212d9a38ea0457916a1db9.png)
[C language] detailed knowledge of document operation

Custom scroll bar

SSM hospital inpatient management system

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

Embedded cognitive network card PHY self negotiation
随机推荐
About thread (4) thread interaction
29. Right view of binary tree
Windivert:可抓包,修改包
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
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
Vscode configuration user code snippet (including deletion method)
开山之作造假!Science大曝Nature重磅论文学术不端,恐误导全球16年
How can flinksql run in perjob mode on yarn? I submit tasks on SqlClient
I realize large top stack with C I
2022.07.21
Nacos deployment
【C语言】动态内存管理
Sorting method: bubble sorting (use an array to arrange a string of numbers in order (from large to small or from small to large))
About thread (3) thread synchronization
25. Middle order traversal of binary tree
mysql select延迟的场景对应的是所有数据库查询语句都会延迟吧,我这边场景注入后,执行了一条
Usage of swipemenurecyclerview
setAttribute、getAttribute、removeAttribute
Getting started with SQL join use examples to learn left connection, inner connection and self connection
如何画 贝赛尔曲线 以及 样条曲线?