What are the different primitive data types supported in Java?
There are 8 primitive data types supported by Java. Apart from below 8, everything is object in Java.
What are the default values of object and primitve types. When is it necessary to assign values?
Default values of any object type is null. If the variables are defined as instance variables then a default value is assigned but for any local variable default values are not assigned by default and we have to initialize it explicitly. Without initialization, we cannot use the local variables and doing so will give compile time error. Following are the default values of primitive data type-
- byte, short, int, long – 0
- float, double- 0.0
- boolean – false
- char- ‘\u000’
In how many ways an integer value can be represented?
There are three ways to represent integer value –
- Decimal – it is a base 10 representation. This is default format.
- Octal – it is a base 8 representation. We need to use prefix 0 with values.
- HexaDecimal – it is a base 16 representation. We need to use case insensitive prefix 0x.
Explain primitive casting?
Primitive casting is casting a primitive data type to another primitive data type. Primitive casting can be implicit or explicit.
Explain implicit and explicit primitive casting?
Implicit primitive casting is implicit and we need not mention cast. This happens when we cast a small thing to big. On the contrary explicit variable is explicit and we have to cast explicitly when a big type needs to be assigned to a small type.
What do you mean by wrapper classes?
Answer- Corresponding to each primitive data type there is a wrapper class. Wrapper class is wrapping a primitive type to get an object. Following is the list of wrapper classes-
- boolean- java.lang.Boolean
- byte- java.lang.Byte
- char- java.lang.Character
- double- java.lang.Double
- float- java.lang.Float
- int- java.lang.Integer
- long- java.lang.Long
- short- java.lang.Short
How to convert a primitive type to wrapper class?
There are two ways in which we can convert primitive data type to a wrapper objects.
- Constructor- Every wrapper class constructor takes the primitive value and returns corresponding wrapper object.
- Utility Class- Every type has a utility classes which exposes static methods valueOf() and takes the primitive value like Float.valueOf(“2.3f”)
How to convert a wrapper object to primitive data type?
Primitive values can be obtained from wrapper classes by calling xxxValue() methods of wrapper class where xxx is primitive data type like
Integer it = new Integer(23);
int primitive= it.intValue();