• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interesting example regarding java.lang.String and your own String class

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this question in Java Puzzlers. It was very interesting for me and I want to share it with you:

What is output?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really nice Java puzzler!

Luckily this example shows exactly the same behavior as I expected I don't want to spoil anybody's fun thinking about this Java puzzler and discovering what will happen.
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unfortunately I couldn't found out the correct output But its really an interesting puzzle
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
truly puzzling Mushfiq... Java allows us to create a String class. But not sure if it will allow us to alter the class this way it as it is immutable..so may be it throws an exception.
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:truly puzzling Mushfiq... Java allows us to create a String class.

We can make any class with any name, only the syntax should be correct. The name of the class must be a valid Identifier (not a keyword). And String is perfectly a valid identifier. We can even write

(pardon me if i am incorrect )
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never said anything about String not being valid identifier ...!!!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Java allows us to create a String class. But not sure if it will allow us to alter the class this way it as it is immutable..so may be it throws an exception.

The above code snippet will compile successfully. And you should not forget about packages. As you (should) know: a class is uniquely defined by its fully qualified name. We both could write a MyList class, but yours would be in package com.ramya and mine would be in package com.roel; so both classes are defined in another namespace (package) and thus having a different unique name: com.ramya.MyList and com.roel.MyList. If an application contains both classes and you are developing the FunWithMyList class, either using an import statement or the fully qualified name will determine which of the MyList classes will be used in the FunWithMyList class.
Exactly the same happens with this code snippet as well: the String class in the Java API resides (as you know) in the package java.lang and there is no package statement defined in the custom code snippet. So the custom String class resides in the default package and both classes have a different unique name: java.lang.String and String. So that's definitely allowed.
Finally a String object being immutable has no influence at all on this code snippet. Even if a String object was mutable, you would experience the same behavior. And as we all know, the java.lang.String class is final. But this code snippet uses composition (using a java.lang.String reference variable as instance member of the String class) and doesn't extend from the java.lang.String class (which is of course not allowed as this class is final).

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:We can make any class with any name, only the syntax should be correct.

That's incorrect! You can't create a class with any name. A class name must be a valid identifier. And not every class name is a valid identifier Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! Identifiers cannot be a Java keyword!

And you could write a nice little program to see which characters are valid ones to start a valid Java identifier

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do you think? Will the output be the same if we added a package statement to the original code snippet?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally a String object being immutable has no influence at all on this code snippet.
- I was saying that String Class is immutable and hence final, never mentioned anything about String object. about the putting them in a package ...i dont think it will make difference to the output
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:
Finally a String object being immutable has no influence at all on this code snippet.
- I was saying that String Class is immutable and hence final, never mentioned anything about String object. about the putting them in a package ...

Ah ok! I was a little bit confused/unsure as you mentioned "it will allow us to alter the class this way it as it is immutable". You don't alter Java's String class at all, you just define a new custom String class (which has a java.lang.String reference variable). That's why I have misunderstood you.

Ramya Subraamanian wrote:i dont think it will make difference to the output

Correct! Both code snippets will produce the same output.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the moral of the story is that whenever the simple name of a class clashes with the simple name of an imported class, it’s best to make things clearer by avoiding simple names and using fully qualified names instead.

For example:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:I guess the moral of the story is that whenever the simple name of a class clashes with the simple name of an imported class, it’s best to make things clearer by avoiding simple names and using fully qualified names instead.

You hit the nail on the head!

Because you have declared a custom String class in the same package com.coderanch.test, test, default, or ...), it has the same simple name as java.lang.String (classes from the package java.lang are imported implicitly). So if you use the simple name String in the package where it was defined, Java will use the custom String class (instead of the one defined in the package java.lang). And that's the reason why the main method could not be found in the StrungOut class when executing the original code snippet. The declaration of its main method violates one of the requirements. The main method must have a single argument: an array of elements of type java.lang.String; but because the simple name is used, the custom String class is used in the main method declaration. So to solve this issue, you need to use the fully qualified name of the java.lang.String class (which is actually a very simple fix)Using this main method declaration, the output will be "Hello world" (what you expected in the first place).

And for exactly the same reason the overridden equals method in the custom String class uses the fully qualified name of the java.lang.String class. Otherwise the custom String class would not compile as the equals method would not be a valid override of the equals method of the java.lang.Object class: the return type must be java.lang.String (or a subclass, which is impossible as java.lang.String is final) but it's the custom String class instead.

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i run the main method with String[]

Error: Main method not found in class StrungOut, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


I get this error. what is a JavaFx application...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Error: Main method not found in class StrungOut, please define the main method as:
public static void main(String[] args)

That satisfies the explanation from my previous post

Ramya Subraamanian wrote:what is a JavaFx application...

JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications. It's not on the OCA (nor OCP) exam so no need to worry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic