Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If class Number is abstract why I'm I allowed to write Number n = 5?

Number n = new Number(5) is illegal, but Number n = 5 isn't. Why?

like image 811
andandandand Avatar asked Dec 16 '09 16:12

andandandand


People also ask

How do you represent a number in Java?

Syntax : static int parseInt(String s, int radix) Parameters : s - any String representation of decimal radix - any radix value Returns : the integer value represented by the argument in decimal. Throws : NumberFormatException : if the string does not contain a parsable integer.

Is there a number class in Java?

Class Number. The abstract class Number is the superclass of classes BigDecimal , BigInteger , Byte , Double , Float , Integer , Long , and Short . Subclasses of Number must provide methods to convert the represented numeric value to byte , double , float , int , long , and short .

What is number type in Java?

There are two types: float and double . Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.

What is Java Lang number?

Introduction. The java. lang. Number class is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. The Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.


1 Answers

Because of autoboxing. 5 is not an object so it is wrapped into an object (Integer in this case), and Integer is a Number.

like image 118
Bombe Avatar answered Sep 22 '22 05:09

Bombe