Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of the principle example of Java Number Class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Java Number class principle instance analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Number class

Built-in data types: byte, int, long, double, etc.

Wrapper classes: Integer, Long, Byte, Double, Float, Short,

This wrapper, which is particularly supported by the compiler, is called boxing, so when the built-in data type is used as an object, the compiler boxing the built-in type as a wrapper class. Similarly, the compiler can unbox an object into a built-in type. The Number class belongs to the java.lang package.

Public class Test {public static void main (String args []) {Integer x = 5; x = x + 10; System.out.println (x);}}

When x is assigned an integer value, the compiler boxed x because it is an object. Then, in order to enable x to add, it is necessary to unbox x

Number class methods:

The xxxValue method: converts the Number object to a value of the xxx data type and returns

Public class Test {public static void main (String args []) {Integer x = 5; / return byte native data type System.out.println (x.byteValue ()); / 5 / return double native data type System.out.println (x.doubleValue ()); / / 5.0 / return long native data type System.out.println (x.longValue ()); / / 5}

CompareTo () method: compare number objects with parameters and compare two data of the same type

Returns 0 if the specified number is equal to the parameter. Returns-1 if the specified number is less than the parameter. Returns 1 if the specified number is greater than the parameter.

Public class Test {public static void main (String args []) {Integer x = 5; System.out.println (x.compareTo (3)); / / 1 System.out.println (x.compareTo (5)); / / 0 System.out.println (x.compareTo (8)); / /-1}}

Equals () method: determines whether the number object is equal to the parameter

Integer x = 5; Integer y = 10; Integer z = 5; Short a = 5; System.out.println (x.equals (y)); / / false System.out.println (x.equals (z)); / / true System.out.println (x.equals (a)); / / false

ValueOf () method: returns the native Number object value of a given parameter, which is a static method

Integer x = Integer.valueOf (9); / 9 Double c = Double.valueOf (5); / 5.0 Float a = Float.valueOf ("80"); / / 80.0 Integer b = Integer.valueOf ("444,16); / / use hexadecimal / / 1092

The toString () method: returns a value as a string

System.out.println (x.toString ()); System.out.println (Integer.toString (12))

Parselnt (): parses a string to an int type

Int x = Integer.parseInt ("9"); double c = Double.parseDouble ("5"); int b = Integer.parseInt ("444", 16)

This is the end of the content of "principle instance Analysis of Java Number Class". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report