• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java programming

 
Aruna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
kapil apshankar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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
 
Vagisha Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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('$');
}
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic