Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Confused help

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this code:
1. class Example extends Object{
2. public void Increment( Short S ){
3. S = new Short( S.intValue() + 1 ) ;
4. }
5. public void Result( int x ) {
6. Short X = new Short((short) x ) ;
7. Increment( X ) ;
8. System.out.println( "New value is "
9. + X ) ;
10. }
11. }
What happens when a program calls the Result method with a value of 30?
A. The message "New value is 31" goes to the standard output.
B. The message "New value is 30" goes to the standard output.
C. A runtime exception is caused due to the cast in line 6.
D. The message "New value is null" goes to the standard output.

why the answer is B?
THANK YOU.
[ July 12, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should get a compile time error as there is no Constructor with int argument in a Short Class.
[ July 12, 2005: Message edited by: Srinivasa Raghavan ]
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is taken from exam cram book!
the cast will not work in line 6?
please help
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line three should generate a compile time error as an integer is passed to a Short Constructor. Let me know if i'm wrong.
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasa Raghavan
Thank you for your reply.
it seem true, but the question is about passing parameter by value.
need help in this topic.

The question is taken from the java 2 programmer exam cram(310-035) book.
Thank you
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivasa is correct: This won't compile without a cast in line 3.

Assuming the constructor call was fixed, then the answer would be B, because arguments are copies of references (that is, passed by value) and their variables are local to the method.

In line 6, a Short object is created and assigned to variable X. In line 7, the increment method is called and a copy of X's reference is passed. In the increment method, that reference copy is assigned to variable S, which is local to the method. So, when S is reassigned (to a new Short wrapping a different value), this does not effect the original object that X is still referencing.

(Note that these wrapper instances are immutable anyway.)
[ July 12, 2005: Message edited by: marc weber ]
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic