2
class Demo
{
String title;
private int num;
}

String is a class, so when we declare title, is that treated as object or just a variable? I know this is a very basic thing, but i need help. Thanks in advance.

  • I don't know why you ask that. `title` is in fact a variable, but Strings are objects. – assembler Sep 12 '17 at 12:36
  • string variable is treated as object only. – Balasubramanian Sep 12 '17 at 12:36
  • It's an Object. – achAmháin Sep 12 '17 at 12:36
  • 1
    There are only eight primitives in Java (`boolean`, `byte`, `short`, `char`, `int`, `long`, `float` and `double`). Everything else is an object. The variable `title` is a reference to a `String`-object, residing on the heap. – Turing85 Sep 12 '17 at 12:36
  • 2
    By `String title;` you declared *reference variable* which can hold *reference* to String object or to `null`. – Pshemo Sep 12 '17 at 12:38
  • 4
    Possibly related [What is the difference between a variable, object, and reference?](https://stackoverflow.com/q/32010172) – Pshemo Sep 12 '17 at 12:42
  • 2
    Don't try and learn to program from bits and bobs scattered around the Web. Get a book, or course materials, and work through it. It will be much easier. – slim Sep 12 '17 at 12:47

3 Answers3

4

title, like num, is an instance variable (sometimes called an instance field), because it's part of an instance of Demo (as opposed to a static field or class field, which is part of the Demo class itself).

Variables declared with object types like String don't directly contain an object, unlike ones with primitive types like int, such as your num variable. Instead, they contain a reference to the object, or null if they don't contain any reference (e.g. don't refer to any object). So for instance, in your case, title will contain null because you haven't assigned anything to it. This is unlike primitive-typed variales like num, which always contain a primitive (your num will default to 0, for instance).

So initially, with your Demo, if you created an instance yo'd have something like this in memory:

+−−−−−−−−−−−−−−−−−−−+
|  (Demo instance)  |
+−−−−−−−−−−−−−−−−−−−+
| title: null       |
| num: 0            |
+−−−−−−−−−−−−−−−−−−−+

In a constructor or instance method within Demo, you could assign an object to title, and you might assign some other value to num as well:

this.title = "This is the title";
this.num = 42;

Then, you'd have something like this:

+−−−−−−−−−−−−−−−−−−−+
|  (Demo instance)  |
+−−−−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−−+
| title: <Ref1132>  |−−−>|      (String)       |
| num: 42           |    +−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−−−−−+    | "This is the title" |
                         +−−−−−−−−−−−−−−−−−−−−−+

Notice that title doesn't directly contain the string; the string exists elsewhere. title just refers to it. (You can think of an object reference as a number that tells the JVM where the object is in memory. That's not what it really is, but it's useful conceptually.)


I should note that String objects, specifically, are special in Java, for a couple of reasons:

  • They're one of the very few objects for which we have a literal notation ("this is a string"). Normally you create an object via new, but you almost never do that with strings. See the answers to this question, this one, and this one.
  • The compiler and JVM work together to minimize having the same string repeated in memory by having special handling for strings created by literals (more generally, by any constant expression in the code; "foo" + "bar" is a constant expression with two literals; the compiler can [and does] combine them into a single string). (The compiler puts them in a section of the class file called the "constant pool," and the JVM automatically interns them when loading the class.) More about interning in the spec and this question's answers.

Other than that, they're objects just like other objects, but those differences can confuse people.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Fantastic to see such a thorough answer to a question that might otherwise be so easily dismissed. I'm really impressed. – Michael Sep 12 '17 at 13:18
2

Variables in java are either primitives, like int, or references to objects (instances of classes).

title is a variable, and a reference to an object of class String. num is a variable, and a primitive.

They are both also members of Demo.

Phydeaux
  • 2,795
  • 3
  • 17
  • 35
2

Your question isn't clear but I think your question is related to java memory model as well.

When you are declaring a variable whether it's type is a class(e.g. String) or it's a primitive type(like int) they are just reference variables. Now there are different types of memory exist in Java MM but for simplicity I'll talk about Stack and Heap. The stack stores the reference variables i.e. when you declare String a; it's stored in stack only. And when you assign value to it(either by = "abc" or by new String("abs")) then it gets memory at Heap.

Hope this clears your doubt.

N.B.: Don't get confused between Heap and Pool memory for String. You should read about it separately if you want.

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23