Monday 8 November 2021

How to check the type of a variable in Java?

Using 'object.getClass().getName()' we can get type of an object.

 

TypeOfAVariable.java

package com.sample.app.classes;

import java.util.ArrayList;
import java.util.List;

public class TypeOfAVariable {
	public static void main(String args[]) {
		String str = "Hello";
		List<Integer> intList = new ArrayList<>();

		System.out.println("Type of variable str is : " + str.getClass().getName());
		System.out.println("Type of variable intList is : " + intList.getClass().getName());
	}

}

 

One drawback of above approach is, it will not work for primitive types. You can use below utility class to get the type of primitive types along with reference types.

 

TypeUtil.java

package com.sample.app.classes;

public class TypeUtil {

	public static String typeName(byte b) {
		return "byte";
	}

	public static String typeName(short s) {
		return "short";
	}

	public static String typeName(int i) {
		return "int";
	}

	public static String typeName(long l) {
		return "long";
	}

	public static String typeName(float f) {
		return "float";
	}

	public static String typeName(double d) {
		return "double";
	}

	public static String typeName(char ch) {
		return "char";
	}

	public static String typeName(boolean b) {
		return "boolean";
	}

	public static String typeName(Object obj) {
		if (obj == null) {
			throw new IllegalArgumentException("can't deduce type for null");
		}
		return obj.getClass().getName();
	}

}

 

TypeOfAVariable1.java

package com.sample.app.classes;

import java.util.ArrayList;
import java.util.List;

public class TypeOfAVariable1 {

	public static void main(String args[]) {
		byte b = 1;
		short s = 2;
		int i = 3;
		long l = 4;

		float f = 1.1f;
		double d = 1.2;

		char ch = 'c';

		boolean b1 = true;

		String str = "Hello";
		List<Integer> intList = new ArrayList<>();

		System.out.println("Type of variable b is : " + TypeUtil.typeName(b));
		System.out.println("Type of variable s is : " + TypeUtil.typeName(s));
		System.out.println("Type of variable i is : " + TypeUtil.typeName(i));
		System.out.println("Type of variable l is : " + TypeUtil.typeName(l));
		System.out.println("Type of variable f is : " + TypeUtil.typeName(f));
		System.out.println("Type of variable d is : " + TypeUtil.typeName(d));
		System.out.println("Type of variable ch is : " + TypeUtil.typeName(ch));
		System.out.println("Type of variable b1 is : " + TypeUtil.typeName(b1));
		System.out.println("Type of variable str is : " + TypeUtil.typeName(str));
		System.out.println("Type of variable intList is : " + TypeUtil.typeName(intList));
	}

}

 

Output

Type of variable b is : byte
Type of variable s is : short
Type of variable i is : int
Type of variable l is : long
Type of variable f is : float
Type of variable d is : double
Type of variable ch is : char
Type of variable b1 is : boolean
Type of variable str is : java.lang.String
Type of variable intList is : java.util.ArrayList

 

 

 

You may like

Interview Questions

Java: Get the index of first occurrence of a substring

Java: Get the index of last occurrence of a substring

Java: Get all the indexes of occurrence of a substring

Java: How to replace a value in ArrayList or LinkedList

Why do we declare logger with static and final?

No comments:

Post a Comment