• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

asking about STRING ASSOCIATIVITY

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a question regarding string precedence given below:
string x="my name is jitender kumar"
sir i want to ask that due to associativity ,the (=)sign starts functioning from RIGHT SIDE but in the string ,it starts executing from left.
it is really confusing,either to use index method in string ,even then,it starts counting from left rather than from right.for me this is a great confusion.
i hope that sir you would reply this asap.
take care
thank you very much in advance
with regards
kumar abhay
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I get what you are asking...
The statement
String x = "some string";
declares variable x as being a String and initializes it to reference the literal "some string". Assignment always goes from right to left, i.e., the value on the right is assigned to the variable on the left.
Strings are indexed 0 to n starting from the left because that's how we normally read: left to right.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
kumar abhay
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
thank u very much.but my question is still there.if string r read from left to right then how would you fix (=)operator which's functionality is to work from right to left.String simply denies its functionality.i hope that u have underestood my question.
take care
with regards
kumar abhay
 
Junilu Lacar
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar abhay:
hi there,
thank u very much.but my question is still there.if string r read from left to right then how would you fix (=)operator which's functionality is to work from right to left.String simply denies its functionality.i hope that u have underestood my question.


Sorry, you've really got me now...
fix (=) operator ??? Why? It works just fine!
What do you mean when you say "String simply denies its functionality"?
Perhaps you could help us understand your issues by posting some Java code that you have doubts about.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by JUNILU LACAR (edited October 20, 2001).]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He seems to be talking about operator ascosiativity.
left right ascosiative etc.
But even i do not get his question yet.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kumar abhay:
i have a question regarding string precedence given below:
string x="my name is jitender kumar"
sir i want to ask that due to associativity ,the (=)sign starts functioning from RIGHT SIDE but in the string ,it starts executing from left.
it is really confusing,either to use index method in string ,even then,it starts counting from left rather than from right.for me this is a great confusion.


You're right. indexOf() in the String class counts from left to right. Some Java operators, including the '=', are right associative. That's just the way it is. You'll have to take your questions up with the creators of Java as to why they made it that way. It won't change, so you will just have to learn which operators are right associative to be able to code Java properly. Here are some more:
'++', '--' (when used as pre-increment operators)
'~', '!', '-'(arithmetic negation), '+'(opposite of the previous operator, not additive)
'?:' (conditional operator)
'=', '*=', '/=', '%=', '+=', '-=', and other assignment operators.

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kumar,
Are you thinking that each char in the string is assigned individually?? For example, the 'r' in 'kumar' and then the 'a' in 'kumar', etc?? So how can x[0] contain the 'm' in 'my'?? If so, that is not what happens. The entire string object is created and the reference to that object is assigned to 'x'.
The right associativity for '=' relates to statements like

The assignements take place right to left. '1' is assigned to 'd', 'd' is assigned to 'c', 'c' is assigned to 'b', 'b' is assigned to 'a'.
In your example, <code>String x = "my name is jitender kumar";</code> the string on the right is fully evaluated before the assignment takes place. A new String object is built to hold the string and then a reference to that object is assigned to 'x'. The assignment is still made from 'right' to 'left'.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic