amitshekhariitbhu / android-interview-questions

Your Cheat Sheet For Android Interview - Android Interview Questions and Answers
https://outcomeschool.com
Apache License 2.0
11.47k stars 2.26k forks source link

String listed as Java primitive data type #109

Open swapnilmadavi opened 2 years ago

swapnilmadavi commented 2 years ago

For the question Can you list 8 primitive types in java? in Objects and Primitives section, String is listed as an extra Primitive data type.

According to the Java documentation (here),

The String class is not technically a primitive data type

MalaRuparel2023 commented 7 months ago

In Java, there are eight primitive data types:

byte: 8-bit signed integer. Range: -128 to 127. short: 16-bit signed integer. Range: -32,768 to 32,767. int: 32-bit signed integer. Range: -2^31 to 2^31 - 1. long: 64-bit signed integer. Range: -2^63 to 2^63 - 1. float: 32-bit floating point. Range: approximately ±3.40282347E+38F (6-7 significant decimal digits). double: 64-bit floating point. Range: approximately ±1.79769313486231570E+308 (15 significant decimal digits). char: 16-bit Unicode character. Represents a single character. Range: '\u0000' to '\uffff'. boolean: Represents true or false values.

KaranSuthar099 commented 6 months ago

Java provides eight premitive data types which include -

  1. byte: 8-bit signed integer (-128 to 127). Saves memory in large arrays. byte b = 100;

  2. short: 16-bit signed integer (-32,768 to 32,767). Also useful for memory savings in large arrays. short s = 10000;

  3. int: 32-bit signed integer (-2^31 to 2^31-1). Can be used as an unsigned integer (0 to 2^32-1) in Java SE 8+. int i = 100000;

  4. long: 64-bit signed integer (-2^63 to 2^63-1). Can be used as an unsigned integer (0 to 2^64-1) in Java SE 8+. long l = 100000L;

  5. float: 32-bit single-precision floating point. Not for precise values like currency. float f = 10.5f;

  6. double: 64-bit double-precision floating point. Default for decimal values, not for precise values. double d = 10.5;

  7. boolean: Represents true/false values. boolean flag = true;

  8. char: 16-bit Unicode character ('\u0000' to '\uffff'). char c = 'A';

String in Java

Is a reference type. Strings are an object they are instances of the java.lang.String class. For string manipulation wide range of methods are provided which includes concatenation, comparison, and substring extraction. String objects are immutable, any modification results in the creation of a new String object.

Strings being an important DataType have been provided a special initialization technique: String str1 = "Hello, World!";

however, java also support initialization using new keyword like reference types : String str2 = new String("Hello, World!");