• 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

Constructors

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class sample
{
sample(String s)
{
System.out.println("String");
}
sample(Object o)
{
System.out.println("Object");
}
}
class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}
y the answer is String , and y it is not declared as error
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shuaib ahmad:
class sample
{
sample(String s)
{
System.out.println("String");
}
sample(Object o)
{
System.out.println("Object");
}
}
class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}
y the answer is String , and y it is not declared as error


Null can be a String value as well as an Object value.
When null is passed as an argument, the constructor with the most appropriate version of the argument is executed ... null is most specifically a String .
If the constructor with the string parameter was missing , the other constructor would have been executed, resulting in an object.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS (section 3.9)

while null might appear to be a keyword, it is technically the null literal


So null is a String (literal), not an object.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but null is not a String. However, System.out.println does print the string "null" whenever a null reference is passed.
From JLS 3.10.7 The Null Literal


The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters. A null literal is always of the null type.


As far as I know, String is not the null type :roll:
literal does not always mean "String", there are character literals ('a', 'b', ...), integer literals (1,2,3,...), float literals (1.2f,...), etc.
From JLS 3.10 Literals


A literal is the source code representation of a value of a primitive type, the String type, or the null type.


[ July 31, 2002: Message edited by: Valentin Crettaz ]
 
David Poglitsch
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, knew that last sentence was a mistake after I posted it (Where's that "Unpost" button when you need it!)
Also from JLS 15.18.1.1 String Conversion

If the reference is null, it is converted to the string "null"


So "sample s1 = new sample(null)" invokes the sample method for strings.
Adding:
sample s2 = null; // or Object s2 = null;
sample s3 = new sample(s2);
will invoke the sample method for objects.
Dave
P.S. Bartenders rock!
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, knew that last sentence was a mistake after I posted it (Where's that "Unpost" button when you need it!)
When you click on the "Edit" icon above the message you posted, there is a "Delete" checkbox. Just check it and confirm...
P.S. Bartenders rock!
Yihaaah
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic