What is the significance of class data type in java? -
noob question.
i have been programming @ basic level quite while have trouble understanding class data type.
when int = 9; means of data type int meaning can contain integers.
like wise string, boolean, double, float etc.
but consider following code:
class node { node next = null; int data; public node(int d){ data = d; } void append(int d) { blah blah blah .............. } } what node next = null; mean? can understand effort create object with
node next = new node(); and try manipulate next object.
this code:
node next = null; declares variable of type node. node class, value of next reference - either object of type node or subclass, or null reference doesn't refer object @ all... , in case variable starts off value of null.
it's important understand value of next never node object itself... it's ever reference. suppose have:
node next = new node(10); node foo = next; here, next , foo separate variables, each independent values... we've assigned value of next initial value of foo, means both refer same object. if print out foo.data, 10.
i think of variables pieces of paper - , in case of variables of reference types, what's written on piece of paper address of house, or word "null". if 2 pieces of paper have same address written on them, refer same house - 2 pieces of paper independent. changing value of 1 variable (crossing out current address , writing one) doesn't change other variable... changes house itself (e.g. painting door red) visible whichever piece of paper use there.
note in question, you've lumped string in int, double , boolean... whereas int, double , boolean primitive types (where value of variable data - number etc), string class, it's reference type. value of string variable isn't text itself, reference.
Comments
Post a Comment