I was reading a program as follows

import java.util.StringTokenizer;
class Tokentester
{
public static void main(String[] arguments)
{
StringTokenizer st1,st2;
String quote1="Vdef 3 -1/16";
st1=new StringTokenizer(quote1);
System.out.println("Token 1:  "+st1.nextToken());
System.out.println("Token 2:  "+st1.nextToken());
System.out.println("Token 3:  "+st1.nextToken());

String quote2="NGNDFBgh 27/32@3/ewtg@tryh@eretgse";
st2=new StringTokenizer(quote2,"@");
System.out.println("Token 1:  "+st2.nextToken());
System.out.println("Token 2:  "+st2.nextToken());
System.out.println("Token 3:  "+st2.nextToken());
}
}

in c++ when we create object of a class we dont write new but in java when we have declared st1,st2 as instance of class stringtokenizer then why we are using new operator
in above program.i tghink StringTokenizer is a constructor which is being called in

st1=new StringTokenizer(quote1);

StringTokenizer is not a constructor there !!

siand s2 are not objects, they are reference variables - like a pointer - that can hold references to a StringTokenizer object.
StringTokenizer st1,st2; declares two reference variables, both null at this stage.
new StringTokenizer(quote1) creates an Object of class StringTokenizer - this is where the meory for that object is allocated and the object is initialised (calling its constructor method.
st1=new StringTokenizer(quote1); creates the object and sets the reference variable st1 to be a reference (pointer)to the new object.
In effect, all java variables (except the primitive types like int, boolean, char float etc) are pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.