• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

tricky!!!! tricky!!

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could someone tell if the tricky stuff like the following will appears in the real test???
public class Test5 {
public static void main (String args []) {
/* This is the start of a comment
if (true) {
Test5 = new test5();
System.out.println("Done the test");
}
/* This is another comment */
System.out.println ("The end");
}
}
What will print out???
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is " the end ".
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure..no variable of type Test5 is being declared in the statement:
Test5 = new Test5();
Will this code compile at all?
Thanks,
Aman

Originally posted by kevin jia:
It is " the end ".



[This message has been edited by Amandeep Waraich (edited July 15, 2000).]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I executed the program and I got 'the end' only. Actually how does 'if (true) or if (false) executed? . Anybody can explain?.
Thanks in advance.
Nirmala
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code will generate a compilation error
Undefined variable : Test5
Class test5 not found
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
you have to watch out this code carefully:
/* This is the start of a comment// of course but not finished
if (true) {
Test5 = new test5();
System.out.println("Done the test");
}
/* This is another comment */ // here comes the end
After setting the right comments, you have to make to
changes:
Test5 test5 = new Test5();
After this, xou will see what you expect!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tian Lau:
Easy questions like this will NOT appear in the real exam. I am 1000% sure !
regards
--Shiny
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO
The only statement outside the comment block is
System.out.println ("The end");
So the main method executes and prints the string literal.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put this code in an IDE that displays comments as a different color or in italics will see that all the code before the end comment tag is treated as a comment like it should. Therefore, you are left with "the end".
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
folks,
after seeing so many comments upon what seemed to be an easy piece of code, i decided to try it out. i cut-pasted the given code, compiled it and ran it and it gave me the output - "The End". so, it should mean that even if you are not instantiating your base class, it will compile and run.
This is the console output that i got.
D:Java\General>javac Test5.java
Symantec Java! JustInTime Compiler Version 3.10.088(i) for JDK 1.1.x
Copyright (C) 1996-98 Symantec Corporation

D:Java\General>java Test5
Symantec Java! JustInTime Compiler Version 3.10.088(i) for JDK 1.1.x
Copyright (C) 1996-98 Symantec Corporation
The end
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I think that it treats that whole portion, where the class is instantiated, as a comment so the compiler ignores it & prints 'The end'. But if you close the comment like this, compiler will complain.
public static void main (String args []) {
/* This is the start of a comment */ (Notice this)
if (true) {
Test5 = new Test5();
System.out.println("Done the test");
}
/* This is another comment */
System.out.println ("The end");
}
}
 
jafarali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
Once compiler see '/*' mark he ignores every thing until he see a closing ie '*/' so for the compiler code look as simple as the following
class Test5 {
public static void main(String args[]){
System.out.println("The end");
}
}
I hope this will help everybody
And I agree that it is good question which covers the comments in java lang.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic