Help coderanch get a
new server
by contributing to the fundraiser
  • 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

java programming

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Process8
{
boolean b;
public Process8(int x) {
System.out.println((char)x);
}
public void Process8(char c) {
if(b = java.lang.Character.isJavaIdentifierStart(c))
System.out.println(b);
}
public Process8(short s) {
System.out.println((char)s);
}
}
public class Test8
{
Process8 p3 = new Process8('V');

public static void main(String[ ] args)
{
Process8 p4 = new Process8('_');
}
static Process8 p1 = new Process8('J');
static Process8 p2 = new Process8('$');
}
Ans : Prints �J�
Prints �$�
Prints �_�
Question : The ans should be �J�, �$�, �V� and �_� is it so? Why �V� is not printed? Pls explain.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The loophole here is the following method:
public void Process8(char c) {
if(b = java.lang.Character.isJavaIdentifierStart(c))
System.out.println(b);
}

Since it has a void return type, it is not recognised as a constructor for 'char' argument, but a normal method. Hence the discrepancy.
Cheers
Kapil
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'V' is not printed because p3 is an instance variable, not initialized until an object of the class Test8 is created.
'J', '$', and '-' are printed because variables p1 and p2 are declared static and variable p4 is declared and initialized in the main method.
Look at the comments:
public clas test8
{
//Instance variable p3 initialized only when object of class
//Test8 is created
Process8 p3 = new Process8('V');
public static void main(String[ ] args)
{
Process8 p4 = new Process8('_');
// p3 will be initialized if the following line is added
//Test8 t8 = new Test8();


}
//static variables p1 and p2 are initialized when class
//Test8 is loaded at runtime
static Process8 p1 = new Process8('J');
static Process8 p2 = new Process8('$');
}
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aruna,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic