This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Confused about output and error  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone help me with what will be the output and why ?


The error I got is :
zunayeed@Zunayeed-hp:~$ javac Fizz.java
Fizz.java:14: error: <identifier> expected
z.x = 6 ;
  ^
Fizz.java:15: error: illegal start of type
return z;
^
Fizz.java:15: error: <identifier> expected
return z;
       ^
3 errors

Then when I try the next command,  got false true as answer :
   zunayeed@Zunayeed-hp:~$ java Fizz
    falsetrue

 
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You obviously have an old .class file left over from a previous compilation attempt. I copied your code onto JShell and it compiled without error. I don't know how you got those errors. You appear to have introduced errors when you included the spaces in the print statement.

It should be obvious why your output is falsetrue. Look at the effects of the == operator, and you should be able to work it out for yourself. I don't know what the swtchFizz() method will do because I haven't seen how you are using it.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the new keyword in front  of reference  Fizz f3. Now when I run the program, I get true true . Here my question is z is referring to null eventhough it is a Fizz type variable . will it make f3 null as well ?
Revised version:

class Fizz {
int x = 5 ;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2); // removed new keyword
System.out.println((f1 == f3) + "  "+ (f1.x == f3.x));

}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6 ;
return z;
}
}
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your changes to remove the error messages make my reply look like nonsense; I have changed them back.

Don't try writing new FizzSwitch() because FizzSwitch isn't a type. It is inappropriate to start a method name with F; start it with f. If you change line 6 to read...then,
  • 1: Where does it say anything about nulls?
  • 2; What is the reference returned from that method? Is it the same as f1, the same as f2, or different?
  • 3: What changes has that method made to f1 and f2? Or has it made no changes?
  •  
    We don't have time for this. We've gotta save the moon! Or check this out:
    ScroogeXHTML - small and flexible RTF to HTML converter library
    https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!