-3

Is it possible to assign a string literal to an object in Java?
Like this:

String strLiteral = "String value"; 
MyClass obj = "String"; // Is this possible 
illright
  • 3,991
  • 2
  • 29
  • 54
  • 3
    No, you can't specify user-defined conversions. – Jon Skeet Apr 04 '17 at 12:28
  • have your tried it? did it work? what was your error? – Jpsh Apr 04 '17 at 12:28
  • `String strLiteral = "String value"` is possible, `MyClass obj = "String"` won't work since as Jon Skeet already stated you can't specify user-defined conversions. The closest you could get would be something like `MyClass obj = new MyClass("String")`. – Thomas Apr 04 '17 at 12:30
  • 2
    This feels like an X/Y problem: You want to do X, you think assigning a string to `obj` (Y) will let you do X, so you've asked about Y. What's X? – T.J. Crowder Apr 04 '17 at 12:30
  • How can i create own String like class in java? ie, String literals will create in String pool, We can assign String literal as object also. – Nineesh C V Apr 04 '17 at 18:21
  • @NineeshCV: *"String literals will create in String pool, We can assign String literal as object also."* The strings in the pool **are** objects. All strings are objects in Java. – T.J. Crowder Apr 04 '17 at 20:37

3 Answers3

3

You're not trying to assign it to an object, you're trying to assign it to a variable of type MyClass.

You can do that if String is assignment-compatible to that class. For instance:

Comparable c = "Foo";

is fine, because String is assignment-compatible with Comparable, since String implements Comparable. The object is still a String, it's just that we're accessing it through a variable of type Comparable. (See What does it mean to “program to an interface”? for more on that concept.)

You can't do:

class Foo {
}

// Doesn't work
Foo f = "Foo";

because String is not assignment-compatible with Foo.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you, How can I create own String like class in java? – Nineesh C V Apr 04 '17 at 18:19
  • @NineeshCV: Simply by creating it, like any other class. But there are a couple of things about `String` you can't duplicate: 1. You can't add new literal forms to Java, those have to be defined at the specification level (and all the levels deriving from it). 2. You can't override `+` for your class. – T.J. Crowder Apr 04 '17 at 20:36
0

No you cannot, because MyClass is not a superclass of String.

One option is to make a static factory method fromString on your class and use like so:

MyClass obj = MyClass.fromString("String");

In this method, you can define the conversion from a String to a MyClass.

weston
  • 54,145
  • 21
  • 145
  • 203
-1

String is Final Class you cannot extend string class and could not able to create subclass. So you cannot assign a string to different class name.

dinesh
  • 19
  • 2
  • 4
    `String` being `final` is irrelevant, you can't assign a superclass reference to a subclass variable anyway (without a cast). – T.J. Crowder Apr 04 '17 at 12:34