The tenth day of learning JAVA-basic type wrapper class & mutual conversion between int and String types

Basic data type wrapper class

The advantage of encapsulating basic data types into objects is that more functional methods can be defined in objects to manipulate the data

Wrapper classes are object types

Basic data type wrapper classTo get an Integer object, you can use the constructors of Integer.valueOf(int s) and Integer.valueOf(String s)

Convert between int and String

There are 2 ways to convert int to String:
1. String s1 = "" +number;
2. String s2 = String.valueOf(number)

String to int:

1. String is first converted to Integer

Integer i = Integer.valueOf(s)

Then Integer is converted to int

int x = i.intValue();

The method called here is: public int intValue();
there is no static modification so it is a non-static method;

Remedial lesson: How to call a non-static method
A static method is a static method, which belongs to a class; a static type can directly call Integer.valueOf through the class name (Integer is the class name, valueOf is a static method)
. It belongs to the method of the object, so if you want to call the non-static method in the static method, you must first create a new object, and then use this object to call the non-static method. The non-static method must be
associated with the object , and you must create an object after , the method can be called on the object, and the static method does not need to create an object when calling, and can be called directly.

2. Use the static method: parseUnsignedInt​(String s)
int y = Integer.parseUnsignedInt​(String s);

Guess you like

Origin blog.csdn.net/weixin_52723971/article/details/110406582