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

String concatenation

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String name = "levan";
System.out.println("My name is "+name!=null?name:"Not specified");

output is : "Levan"
not : "My name is Levan"
I am wondering why is string concatenation happens before comparison?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will give you the expected output, but I'm a little confused how the compiler is understanding the other code that you posted.



I suppose the comparison that it's doing is:
"My name is levan" != null

which evaluates to true and so it prints name.

Josh
[ February 01, 2006: Message edited by: Joshua Smith ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. String concatenation operator has a higher precedence than 'not equal to' operator.
 
levani dvalishvili
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes , I realized putting a parens around solves the problem, but look at this example then :

String name = "hello";
System.out.println(name=null==null?name:"Not specified");
Output is : hello

here comparison happens first and then assignment.
even without parens, I would asume name would be assigned a null value first then comparison and output would be null.

???
getting confused about precedence order
[ February 02, 2006: Message edited by: levani dvalishvili ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by levani dvalishvili:
yes , I realized putting a parens around solves the problem, but look at this example then :

String name = "hello";
System.out.println(name=null==null?name:"Not specified");
Output is : hello

here comparison happens first and then assignment.
even without parens, I would asume name would be assigned null first then comparison and output would be null.

???
getting confused about precedence order

[ February 02, 2006: Message edited by: levani dvalishvili ]




Why would name be set to null. You are comparing null to null and if they are the same, name is assigned to the value of name. name isn't assigned to null.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that the ternary operator has higher precedence than the assignment operator.
 
levani dvalishvili
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think yo miss understood :

example 1 :


String concatination happens first,I undertand because concatination(which behind the scene yelds to assignment) has higher precendence than comparison, so
output is : "Levan"

example 2

here seems like comparizon happens first and asignment second.
Output is : hello


and if ternary operators have higher precedence than asignment, why output is "levan" in first example? since is type of asignment , right? internaly it means something like


thats what I am asking.
[ February 02, 2006: Message edited by: levani dvalishvili ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you, guys should check out this JavaOne multimedia presentation:

Yet More Programming Puzzlers: TS-3738, 2005
http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-3738.html

They explain this problem very well.
Alex
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic