Java Data Types

Q. Explain different data types in Java.
There are 2 types of data types in Java as mentioned below:
  1. Primitive Data Type
  2. Non-Primitive Data Type or Object Data type
Primitive Data Type: Primitive data are single values with no special capabilities.
There are 8 primitive data types:
  - boolean: stores value true or false
  - byte: stores an 8-bit signed two's complement integer
  - char: stores a single 16-bit Unicode character
  - short: stores a 16-bit signed two’s complement integer
  - int: stores a 32-bit signed two’s complement integer
  - long: stores a 64-bit two’s complement integer
  - float: stores a single-precision 32-bit IEEE 754 floating-point
  - double: stores a double-precision 64-bit IEEE 754 floating-point
The default value of the byte datatype in Java is 0.
The default value of the float is 0.0f and of double is 0.0d in Java.
Non-Primitive Data Type: Reference Data types will contain a memory address of the variable's values because it is not able to directly store the values in the memory.
Types of Non-Primitive are mentioned below:
  - Strings
  - Array
  - Class
  - Object
  - Interface

Q. When a byte datatype is used?
A byte is an 8-bit signed two-complement integer. The minimum value supported by bytes is -128 and 127 is the maximum value. It is used in conditions where we need to save memory and the limit of numbers needed is between -128 to 127.

Q. What is the Wrapper class in Java?
Wrapper is referred to a larger entity that encapsulates a smaller entity. 
In Java, the wrapper class is an object class that encapsulates the primitive data types. 
The primitive data types are the ones from which further data types could be created. 
For example, integers can further lead to the construction of long, byte, short, etc. 
Java contains 8 wrapper classes. 
1, Boolean
2. Byte
3. Short
4. Integer
5. Character
6. Long
7. Float
8. Double.
Further, custom wrapper classes can also be created in Java. 
We create our own wrapper class with the required data types.

Q. Why do we need wrapper classes?
The wrapper class is an object class that encapsulates the primitive data types, and we need them for the following reasons:
1. Wrapper classes are final and immutable.
2. Provides methods like valueOf(), parseInt(), etc.
3. It provides the feature of autoboxing and unboxing.


Q: Java is always pass-by-value?

Java is always pass-by-value, even when dealing with objects. 
Core Concepts:

Pass-by-Value (Java’s actual behavior):
- Java passes a copy of the variable’s value to methods.
- For primitive types (like `int`, `double`), this means the actual value is copied.
- For objects, the value being copied is the reference to the object, not the object itself.
void modify(int x) {
    x = x + 10;
}
int a = 5;
modify(a);
// a is still 5 — because x was a copy

Pass-by-Reference (Java does NOT do this):
- Would mean passing the actual memory address, allowing the method to modify the original variable.
- Java doesn’t allow this directly, but it can look like it when modifying object fields.
class Dog {
    String name;
}
void rename(Dog d) {
    d.name = "Fido";
}
Dog myDog = new Dog();
myDog.name = "Max";
rename(myDog);
// myDog.name is now "Fido" — because the reference was copied, and the object it points to was modified

Real-World Implications:
- Modifying primitives inside methods won’t affect the original.
- Modifying object fields inside methods _will_ affect the original object.
- Reassigning object references inside methods won’t affect the caller’s reference.



Popular posts from this blog

IT Software Services

Java Architect

Agile Ceremonies