- Java get number type
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Constructor Detail
- Number
- Method Detail
- intValue
- longValue
- floatValue
- doubleValue
- byteValue
- shortValue
- Java get number type
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Java typeof Operator
- Get the Type of a Variable/Value in Java
- Get the Type of Any Variable/Value in Java
- Related Article — Java Operator
Java get number type
The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte , double , float , int , long , and short . The specific semantics of the conversion from the numeric value of a particular Number implementation to a given primitive type is defined by the Number implementation in question. For platform classes, the conversion is often analogous to a narrowing primitive conversion or a widening primitive conversion as defined in The Java™ Language Specification for converting between primitive types. Therefore, conversions may lose information about the overall magnitude of a numeric value, may lose precision, and may even return a result of a different sign than the input. See the documentation of a given Number implementation for conversion details.
Constructor Summary
Method Summary
Methods declared in class java.lang.Object
Constructor Detail
Number
Method Detail
intValue
public abstract int intValue()
longValue
public abstract long longValue()
floatValue
public abstract float floatValue()
doubleValue
public abstract double doubleValue()
byteValue
Returns the value of the specified number as a byte . This implementation returns the result of intValue() cast to a byte .
shortValue
Returns the value of the specified number as a short . This implementation returns the result of intValue() cast to a short .
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Java get number type
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Java typeof Operator
- Get the Type of a Variable/Value in Java
- Get the Type of Any Variable/Value in Java
This tutorial introduces how to get the data type of a variable or value in Java and lists some example codes to understand the topic.
In Java, to get type of a variable or a value, we can use getClass() method of Object class. This is the only way to do this, unlike JavaScript with the typeof() method to check type.
Since we used the getClass() method of Object class, it works with objects only, not primitives. If you want to get the type of primitives, then first convert them using the wrapper class. Let’s understand with some examples.
Get the Type of a Variable/Value in Java
In this example, we used getClass() to check the type of a variable. Since this variable is a string type, then we can directly call the method. See the example below.
Notice that the getClass() method returns a fully qualified class name, including a package name such as java.lang.String in our case.
public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass()); > >
Hello Type is: class java.lang.String
Get the Type of Any Variable/Value in Java
In the above example, we used a string variable and got its type similarly; we can also use another type of variable, and the method returns the desired result. See the example below.
In this example, we created two more variables, integer and character, apart from string and used the getClass() method.
package javaexample; public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass()); // Integer Integer val = 20; System.out.println(val); System.out.println("Type is: "+val.getClass()); // Character Character ch = 'G'; System.out.println(ch); System.out.println("Type is: "+ch.getClass()); > >
Hello Type is: class java.lang.String 20 Type is: class java.lang.Integer G Type is: class java.lang.Character
The getClass() method returns a complete qualified name of the class, including the package name. If you wish to get only the type name, you can use the getSimpleName() method that returns a single string. See the example below.
package javaexample; public class SimpleTesting public static void main(String[] args) String msg = "Hello"; System.out.println(msg); System.out.println("Type is: "+msg.getClass().getSimpleName()); // Integer Integer val = 20; System.out.println(val); System.out.println("Type is: "+val.getClass().getSimpleName()); // Character Character ch = 'G'; System.out.println(ch); System.out.println("Type is: "+ch.getClass().getSimpleName()); > >
Hello Type is: String 20 Type is: Integer G Type is: Character