-1

I know this is a weird question and maybe a little vague. Because I myself isn't sure about what I'm really missing. But please hear me out.

In java, there are :

  1. value types (like int, char, ...).
  2. reference types (like string, user-defined classes,...)
> int age; 
> String name; 

I know that the value types are primitive and simple unlike the ref type.

But why is int here just a deceleration keyword and String is an actual class?

I hope this sounds right. I know it's probably not the right question to ask but maybe you can help me figure out what I don't know that I'm missing.

Roo Tenshi
  • 37
  • 8
  • 2
    Basically, a String is a class because the primitive char exists; a String is an array of chars: https://stackoverflow.com/questions/2099257/why-is-there-no-primitive-type-for-string – LuckyBandit74 Jul 22 '21 at 18:57
  • 2
    1. The short answer to a question like _why ...?_ is usually _by design_. 2. There are wrapper classes for primitive value types: `Integer` for `int`, `Character` for `char`, `Double` for `double` etc. – Nowhere Man Jul 22 '21 at 19:04
  • @LuckyBandit74, thank you for sharing the link. It had many useful information. – Roo Tenshi Jul 22 '21 at 22:54
  • @AlexRudenko, 1. by "*by design*" you mean architecture? 2. I will look up the wrapper class because I don't know what it is. If I knew about them I would've probably asked why don't we use *Integer variable;* but I guess that's not how it works. – Roo Tenshi Jul 22 '21 at 22:57
  • 2
    1. _By design_ I meant by the design of Java programming language. [There's an explanation why primitive types exist in Java](https://stackoverflow.com/questions/14477743/why-are-there-primitive-datatype-in-java/14477916): _The main reason primitive data type are there because, creating object, allocating heap is too costly and there is a performance penalty for it._ 2. [Another explanation why primitives are supported](https://stackoverflow.com/questions/2063556/when-we-have-wrappers-classes-why-primitives-are-supported) – Nowhere Man Jul 23 '21 at 05:24
  • @AlexRudenko That's great. Thank you for sharing! :) – Roo Tenshi Jul 23 '21 at 22:33

1 Answers1

3

Unlike int, char etc which are primitive data types, String is a derived datatype as it is derived from primitives (in this case char). After all String is an array of characters (char[]).

A Java String is an object of the class java.lang.String.

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Refer to the documentation.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
K450
  • 691
  • 5
  • 17
  • 1
    Thank you for your answer. Yes indeed, and I have checked that documentation. It was the `String.class` that lead me to this point. Meanwhile, int and other primitives didn't have a class file used in declaring variables. That was the actual question I had. Because if `int` is not a class, then what it is? Which the answer to this is just a primitive data type **that doesn't NEED a class**. Which I knew but for some reason I got confused for my lack of knowledge in data structure and computer architecture. – Roo Tenshi Jul 22 '21 at 23:01