• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

intersting

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't get what this meant by

Make the following line of code throw a java.lang.NullPointerException.


long x = y;

Answer


Long y = null;


q 2)

01 public class ConstantsCharsStrings
02 {
03 private ConstantsCharsStrings()
04 {
05
06 }
07
08 public static void main(String[] args)
09 {
10 char c1 = 'x';
11 final char c2 = 'y';
12 int i1 = 7;
13 final int i2 = 8;
14 final int i3 = i1 >>> 3;
15 final int i4 = i2 >>> 4;
16
17 System.out.println("xxx" == (c1 + "xx"));
18 System.out.println("yxx" == (c2 + "xx"));
19 System.out.println("7xx" == (i1 + "xx"));
20 System.out.println("8xx" == (i2 + "xx"));
21 System.out.println("7xx" == (i3 + "xx"));
22 System.out.println("8xx" == (i4 + "xx"));
23 }
24 }


What is the output and why?

a) false false false false false false
b) true true true true true true
c) false true false true false true
d) false true false false true false
e) false true false true false false
f) compile-time error ________
g) runtime exception ________
h) something else ________
[ April 19, 2005: Message edited by: Parameswaran Thangavel ]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For the first ques. if you make y= null then any .operator will result in exception so that could be the answer.

I don't know if it is true or not. But I guessed the ans for 2nd to be "e".
There won't be runtime or compile error as char can be added to String.
I know last two comparisons are false and I assumed the final constant
should evaluate to true on ==. So I chose "e"

Thanks.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long y = null;
long x = y;

This will throw a NullPointerException.
This is one of two new ways of implicitly dereferencing a reference in J2SE 1.5.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran the first question (don't know why the original poster didn't run it) and e is the correct answer.

As for reasoning. The final variables can be resolved at compile time so they are treated as constants.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tony
u mentioned this is one of two new ways of implicitly dereferencing a reference in J2SE 1.5

can i know the other way of implicityl dereferenciung a reference
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String[] a = null;
for(String s : a) // NullPointerException - implicit dereference of a
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tony

i didn't cameacross this type of syntax for(String s : a)in k&B book
is this included in the scjp1.4
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the new Tiger feature: enhanced for-loop.

Nick
 
Let's get him boys! We'll make him read this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic