• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String foo = �blue�;
Boolean [] bar = new Boolean [1];
if (bar [0]) {
foo=�green�;
}

What is the result?
A.foo has the value of ��
B.foo has the value of null
C.foo has the value of �blue�
D.foo has the value of �green�
E.An exception is thrown
F.The code will not compile
Answer: F

I tried this code and it compiled fine. And it depends. If it is Boolean, the primitive data type with a small �b�, no exception is thrown and foo takes a value of blue. However, if it is Boolean, the class with a capital �B�, the constructor initializes it to �null� and the NullPointerException is thrown. I am saying that because in this study guide, there is a lot of mixing between capital and small letters. Am I missing something or is it just a study guide mistake.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're definitely right. I think the book is making a mistake. The code will compile fine and throw a NullPointerException.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raef,

short question. Is your study guide for Java 1.4 or 5.0? With 1.4 the code will not compile and with 5.0 you will get a runtime exception.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please quote your sources.


Bu.
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, my Study Guide is for 1.4. However I am preparing for SCJP5.0. So, I am more interested on how version 1.5 would act. I tried posting my sources but Javaranch disallows me to do so.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raef Kandeel:
I tried posting my sources but Javaranch disallows me to do so.


What Burkhard meant is that if you post a question that you copied from somewhere else (your study guide for example), you should mention exactly which study guide this is (title and authors). The FAQ that Burkhard mentioned explains exactly why you need to do this.

What do you mean with "Javaranch disallows me to do so", what exactly did you try to do and did you get an error message?
[ August 06, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this....


class Test{
public static void main (String [] args) {
String foo = "blue";
Boolean [] bar = new Boolean [1];
if (bar [0].booleanValue()) {
foo="green";
}
}
}
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That gave me: Exception in thread "main" java.lang.NullPointerException.

Guys I am quite new to the forum, so if I am unfamiliar with some of its policies, excuse my ignorance. I promise you I would be quick to adapt. Thank you all for being patient.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried out compiling the original code

String foo = "blue";
Boolean [] bar = new Boolean [1];
if (bar [0) {
foo="green";
}
it did not compile.. error was
Type mismatch.Cannot convert Boolean to boolean.

But as someone said it would compile fine in Java 5.. can anyone explain why is it so.. what changes have been brought in Java 5 with respect to boolean and Boolean?

However on using bar[0].getBooleanValue() it compiled fine..
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with you Mr Partho Mukherjee

the code will not compile . why because , the simple thing is we can't pass other than boolean value in to the if as codition.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by partho mukherjee:
what changes have been brought in Java 5 with respect to boolean and Boolean?

Java 5 adds the automatic conversion between primitive types and wrapper classes. For details have a look at 5.1.7 Boxing Conversion and 5.1.8 Unboxing Conversion in the Java Language Specification
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this....


class Test{
public static void main (String [] args) {
String foo = "blue";
Boolean [] bar = new Boolean [1];
//if (bar [0].booleanValue()) {
if ((bar [0] = new Boolean(true)).booleanValue()) { //changed

foo="green";
}
}
}

above code was giving NullpointerException as the Boolean array was not initilized explicitely & hence it was inititilized to default value of Boolean Object which is null.... so when we called booleanValue()) on " bar [0]" (whish is having default value null) we got NullpointerException.Now try this code in which "bar [0]" initilized explicitely & hence code is valid both at compile time & runtime......


hope this will help...

Regards
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah that worked. Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic