• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Concatenation of null keyword to a String

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all i have a code bit from www.javachamp.com . The code is as follows:-


In statement2 if i give System.out.println(null) it gives me a compiler error , however null+stmt gives me null null as output . According to me in statement2 the compiler reads the null keyword has to be appended to stmt , so the compiler should give me an error . However the compiler gives me null null as output .

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In statement2 if i give System.out.println(null) it gives me a compiler error



But which overloaded method would a compiler call since null does not belong to any type ?

According to me in statement2 the compiler reads the null keyword has to be appended to stmt , so the compiler should give me an error . However the compiler gives me null null as output .



SOP treats null in a special way. It converts them to "null" strings when concatenated with other string types.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i am not getting this question and solution?
can anyone please detail it?


 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so can we say that System.out.println(anything+string); --> output = string ??
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the documentation of String.valueOf method. String concatenation works on its rules. It says that if null is passed to that method, then the String value "null" is returned. Basically when you write "a"+"b", then the compiler uses StringBuffer class to concatenate these Strings and if you check the documentation of append method of StringBuffer class, you'll see that it works on same principle as String.valueOf method...
 
Greenhorn
Posts: 6
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried looking for the method String.valueOf() in K&B but didnt find it.. im guessin its been ommitted for scjp 6.. but even i did not understand the explanation that was provided related to the null null result.. could someone please elaborate..
thanks in advance..
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here, arguments to print in third statement are concatenated before print takes place. so string buffer is used to concatenate 2 strings. which will result in nullnull. that is printed using SOP statement.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vaibhav teli wrote:i tried looking for the method String.valueOf() in K&B but didnt find it.. im guessin its been ommitted for scjp 6.. but even i did not understand the explanation that was provided related to the null null result.. could someone please elaborate..
thanks in advance..



When in doubt, javadocs are your best friend
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rikesh Desai wrote:so can we say that System.out.println(anything+string); --> output = string ??



Is this correct??
 
author
Posts: 23919
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

Rikesh Desai wrote:

Rikesh Desai wrote:so can we say that System.out.println(anything+string); --> output = string ??



Is this correct??



Have you tried it?

Henry
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic