Java - Data Types - Interview Questions

Java data types form the building blocks of a Java program and is an important and core topic in Java programming language. Java data types interview questions are frequently asked in Java programming interviews; on topics such as primitive data types, wrapper classes, scope of primitive data type, memory footprint of primitive data types, pass by reference vs pass by value etc.

A thorough understanding of Java data types is necessary for being successful in your your Java interview; specially for beginner level positions; and for hands-on roles - programmer, designer, architect or manager.

Below Java data types interview questions, answers, coding exercises and tips will help you get grounded in the fundamental concepts of Java data types.

Important keywords are provided at the end of the questions. Review them, make them a part of your Java data types vocabulary, and talk about them confidently during your interview process.

What are primitive data types? What are the primitive data types supported by Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Ancestry, Antra, avaloq, Bloomberg L.P., Charles River, Nokia, Peri Software Solutions, Pinnacle Infotech, Synechron, Unicode Systems, Visa Inc, Vocalink,

Primitive data types are data types that are predefined in Java programming language and named by a reserved keyword. Java programming language supports eight different primitive types - byte, short, int, long, float, double, boolean and char.

byte, short, int and long are integer number types. byte has 8 bits and is the smallest integer number type. long has 64 bits and is the biggest integer number type. The default value for all integer number types is 0.

float and double are floating-point number types. doubles are bigger than floats. The default value for floating-point number types is 0.0

boolean has a true or false value.

char contains a single, 16-bit unicode value.

TypeBitsBytesMinMaxDefault
byte81-28-128-1-1 0
short162-216-1216-1-1 0
int324-232-1232-1-1 0
long648-264-1264-1-1 0
float324>NANA0.0f
double648NANA0.0d
boolean1NANANAfalse
char16NANANA''

Memorize the data types, its bit sizes and the default values. An interviewer may not ask you questions directly on bits or ranges of data types, specially for a senior level position. But knowledge of this information is important in interviews where you are asked to write code, algorithms, etc.

You just need to memorize the data types and bit sizes. The minimum range value can be derived as -2bits-1. The maximum range value can be derived from 2bits-1-1.

What are Primitive Literals?

 FAQ

Primitive Literals are the code representation of values of primitive data types. For example 'a' is a char literal, 100 is an int literal, 'false' is a boolean literal and 2345.456 is a double literal.

Integer Literals: Integer number types in the Java programming language can be representer in four different ways - decimal (base 10), octal (base 8), hexadecimal (base 16) and binary (base 2). You will use decimal representation in most cases; and rarely, if ever, use the other representations.

Floating-point Literals: Floating point literals are defined by a number, followed by a decimal point and then followed by more numbers representing the fraction. Example: 23435363.4336633. Floating-point literals are of type double by default which is 64 bits. If you want to assign a floating-point literal to a float variable you have to suffix the literal with either 'F' or 'f' (like 23435363.4336633F), else you will get a compilation error of a possible loss of precision. If you want to assign a floating-point literal to a double variable can optionally suffix the literal with either 'D' or 'd' (like 23435363.4336633D). It is optional since floating-point literals are of type double by default.

Boolean Literals: Boolean literals are code representations of boolean data types and can be defined only as either 'true' or 'false'. Some programming languages use numbers, usually 0 and 1, to represent boolean data type. But in Java programming language numbers are not allowed to represent boolean data types.

Char literals: char literals are represented by a single character in single quotes

Example:
//Decimal literal assigned to int data type
int i1 = 200;
//Binary literal assigned to int data type
int i2 = 0B00011;
//Octal literal assigned to int data type
int i3 = 011;
//Hexadecimal literal assigned to an int data type
int i4 = 0x071ff;
//Floating-point literal assigned to float data type
float f = 23435.45637F;
//Floating-point literal, with explicit suffix, assigned to double data type
double d = 43536376.3455365D
//Floating-point literal, without explicit suffix, assigned to double data type
double d1 = 4253636.4536;
//Character literal assigned to char data type
char c = 'a';
//Boolean literal assigned to boolean data type
boolean b = true;

What is Primitive Casting in Java programming language?

 Key Concept

Primitive Casting is used to convert primitive values from one data type to another. For example, an int value can be assigned to a float data type, or a double value can be assigned to an int data type. Casting can be either implicit or explicit.

Implicit Casting: In implicit casting the conversion happens automatically, without writing specific code to do the conversion. Implicit casting happens when you convert or assign a smaller value, like a byte, to a larger data type such as an int.

Explicit Casting: In explicit casting code has to be specifically written to perform the conversion from one primitive type to another. Explicit casting is done by using the syntax (data_type) where data_type is the data type that the cast is being applied to. Explicit casting happens when you convert or assign a larger value to a smaller data type.

Example:
//Implicit cast - smaller value is assigned to bigger data type
int i = 200;
long l = i;

//Explicit cast - larger value assigned to smaller data type
float f = 234.345f;
int i = (int)f;

How long do primitive variables exist in memory? Define the various scopes of primitive variables?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Agero, Cavium, CGI, Elliot Management, EquityZen, FDM Group, GitLab, J.P. Morgan, Kayak, OpticsPlanet, PhishMe, ResearchGate, SAP, Vanguard, Yahoo,

After a primitive variable is declared and initialized; how long it lives in memory is dependent on the scope of the variable. Scope of a variable is determined based on where it is declared within a java class. Following are the various scopes of a variable in a java program based on where they are declared.

1. Class variable (Static fields) - Class variables are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.

Class variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.

2. Instance variables (Non-static fields) - Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static' keyword.

Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory...

*** See complete answer and code snippet in the Java Interview Guide.

How does Java programming language pass primitive variables to methods - by value or by reference?

 FAQKey Concept

In Java, primitive variables are passed to methods by value. More specifically, a copy of the primitive value is passed to the method. If the passed value changes in the method, it does not change the original value.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

What are wrapper classes in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Cloudaction, CSL, Daffodil, KTree, Salesforce, Virtusa, Xoriant,

There are many cases where we cannot directly use primitive data types. For example, We cannot put primitives into Java collections since Java collections (Lists, Sets etc.) can only store objects.

Wrapper classes are classes provided by java programming language that enable us to wrap primitive data in Objects....

*** See complete answer and code snippet in the Java Interview Guide.

What is Autoboxing and Unboxing?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ ABC, AvidXchange, BNP Paribas, Citrix, DataMetica, Deloitte, Deltatre, Hotwire, Indus Valley Partners, Deloitte, InnerWorkings, Innoviti, Lockheed Martin, Luxoft, Mindtree, Naaptol, Quovantis, TSO,

Autoboxing is the automatic conversion of primitive data types into their corresponding wrapper classes by Java compiler. Java compiler applies autoboxing when a primitive data type is assigned to a variable of the corresponding wrapper class...

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example