About Question enthuware.ocajp.i.v7.2.1077 :

Moderator: admin

Post Reply
Brian B
Posts: 13
Joined: Mon Jun 16, 2014 1:07 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1077 :

Post by Brian B » Mon Jun 23, 2014 2:50 pm

public class TestClass{
static boolean b;
static int[] ia = new int[1];
static char ch;
static boolean[] ba = new boolean[1];
public static void main(String args[]) throws Exception{
boolean x = false;
if( b ){
x = ( ch == ia[ch]);
}
else x = ( ba[ch] = b ); <---- Missing opening curly brace causes this code to fail to compile.
System.out.println(x+" "+ba[ch]);
}
}

Once fixed, the output is as stated: false false

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by admin » Mon Jun 23, 2014 8:41 pm

The code given in the question is correct. I just verified it.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

hadesgrid@gmail.com
Posts: 2
Joined: Thu Dec 04, 2014 12:37 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by hadesgrid@gmail.com » Thu Dec 04, 2014 12:41 pm

Hello,

the answer said that ch is a numeric type , but is

static char ch;

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by admin » Thu Dec 04, 2014 9:24 pm

hadesgrid@gmail.com wrote:Hello,

the answer said that ch is a numeric type , but is

static char ch;
Yes, and char is also considered as one of the numeric types. A char is basically an unsigned number.
If you like our products and services, please help us by posting your review here.

laltubcr
Posts: 1
Joined: Mon Feb 15, 2016 2:37 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by laltubcr » Mon Feb 15, 2016 2:42 pm

If static char ch; is initialized to 0. Why i am not getting anything when trying below

System.out.println(ch); ------------ > nothing
System.out.println(Character.isDigit(ch)); ----------- > false

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by admin » Mon Feb 15, 2016 8:08 pm

A char is interpreted as a character. For example, if the integral value of a char variable is 65 and if you print it, you will see "A", not 65. Similarly, if the value is 0, you will see the character represented by 0, which is null character. That is why you don't see anything.
Refer to this table : http://www.asciitable.com/

isDigit prints false because the character represented by the number is not a digit. Try it with a value between 48 and 57.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by NickWoodward » Wed Feb 17, 2016 10:44 am

this is good to know, thanks.

was pulling my hair out during the test - i knew char defaults to '\u0000', which is a space, but didn't know if it equalled 0.

i gambled that it didn't. i was wrong, but at least now i won't forget!! :D

nick

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by admin » Wed Feb 17, 2016 10:50 am

NickWoodward wrote:this is good to know, thanks.

was pulling my hair out during the test - i knew char defaults to '\u0000', which is a space, but didn't know if it equalled 0.

i gambled that it didn't. i was wrong, but at least now i won't forget!! :D

nick
It is not space. It is null. 32 is space. A null character, when displayed on a console looks like a space/blank.
If you like our products and services, please help us by posting your review here.

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by NickWoodward » Wed Feb 17, 2016 2:09 pm

hmm, seems a bit strange! but ok, good to know, thanks!

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by Sergey » Wed Jan 10, 2018 6:00 pm

oh my god)) it is working

Code: Select all

public class A {
    static boolean[] ba = new boolean[1];
    static char ch;

    public static void main(String[] args) {
        System.out.println(ba['\u0000']); // <--------!!!!!! 
    }
}

SoFine
Posts: 1
Joined: Thu May 10, 2018 4:21 am

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by SoFine » Thu May 10, 2018 4:29 am

"So, elements of a boolean array are initialized to false. int, char, float to 0"

Isn't float 0.0 instead of 0?

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1077 :

Post by admin » Thu May 10, 2018 7:14 am

There is no difference between 0 and 0.0 in case of float and double. The following code prints true.
float x = 0;
float y = 0.0;
System.out.println(x == y);

But they are different for Float and Double.

Float x = 0f;
Float y = 0.0f;
System.out.println(x == y); //prints false
System.out.println(x.equals(y)); //prints true
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest