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.
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.
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:
"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."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.
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
.
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.