• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Understanding Equality

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why trim() doesn't output HelloWorld?





 
Prathyu Krishna
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here,



Here trim() doesn't change the value of z. Then, why it doesn't refer to the same reference as x?
 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:



Your code will print true. Are you sure you have compiled and run this exact code?

According to the Java API docs

Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.



So, the trim() method just removes the blank spaces in the beginning and end of the String. But if the value has no changes, the trim returns the same String.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I run the code, I get "true" as the result.

Henry
 
Prathyu Krishna
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
When I run the code, I get "true" as the result.

Henry




Code is from Sybex book. I didn't try to run it
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:Code is from Sybex book. I didn't try to run it


First of all, if you use a code snippet, a quote, a code example from another resource, you must QuoteYourSources (in your original post).

Secondly, you should pay very close attention to the actual code snippet in the book when you are trying to reproduce the output. The actual code snippet isAnd although this code snippet has only 1 character more than your code snippet, it makes a very big difference!
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:why trim() doesn't output HelloWorld?


Honestly, that's something you can easily verify if you have a look at the javadoc of the trim() method of the String class...
 
Prathyu Krishna
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the trim() now, but what's the output ?
Both are Hello World, Is true the correct answer?
 
Ranch Hand
Posts: 113
7
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:I understand the trim() now, but what's the output ?
Both are Hello World, Is true the correct answer?


The best way to learn is by doing. You said you didn't run the code yet. Why didn't you? It'll help you understand the code, and you could even try different combinations to discover peculiarities and learn more. Your question would be answered in a matter of seconds.
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathyu Krishna wrote:I understand the trim() now, but what's the output ?
Both are Hello World, Is true the correct answer?


Please ShowSomeEffort!
reply
    Bookmark Topic Watch Topic
  • New Topic