#Do you always have to create an object for a string like String ex = new String(whatever))

26 messages · Page 1 of 1 (latest)

pliant maple
#

I'm not sure if you have to instantiate a string or could you just say String ex = "this is a string" vs String ex = new String("this is a string").

stiff sableBOT
#

This post has been reserved for your question.

Hey @pliant maple! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

wheat locust
#

"this is a string" is instantiating a string

#

new String("this is a string") is instantiating a string then instantiating another string by passing it to the String constructor

pliant maple
#

so new string

#

instantiates "this is a string"

#

and also instantiates ex

#

by passing "this is a string" to it?

#

oh hi chris :D

#

oh yea

#

how do you use a method

#

like in another class

#

how would you call it to use it

#

and would it be different if the method was static vs nonstatic (like belonging to an object)

wheat locust
#

new String(...) instantiates a new string based on the data
making a string literal, "text", is also instantiating a new string

wheat locust
wheat locust
#

System.out.println access the println instance method of PrintStream, the type of System.out

pliant maple
#

What if you had an add function

#

For an object

#

How would you call it in another class

wheat locust
#

you would need an instance of that object, suppose it's in a variable called obj, then you would call it like obj.add(...)

feral hull
#

😐

candid grail
#

u can just do String str = "string here"; no need to use new String()