Page 1 of 1

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

Posted: Mon Jun 23, 2014 2:50 pm
by Brian B
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

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

Posted: Mon Jun 23, 2014 8:41 pm
by admin
The code given in the question is correct. I just verified it.
HTH,
Paul.

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

Posted: Thu Dec 04, 2014 12:41 pm
by hadesgrid@gmail.com
Hello,

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

static char ch;

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

Posted: Thu Dec 04, 2014 9:24 pm
by admin
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.

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

Posted: Mon Feb 15, 2016 2:42 pm
by laltubcr
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

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

Posted: Mon Feb 15, 2016 8:08 pm
by admin
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.

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

Posted: Wed Feb 17, 2016 10:44 am
by NickWoodward
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

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

Posted: Wed Feb 17, 2016 10:50 am
by admin
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.

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

Posted: Wed Feb 17, 2016 2:09 pm
by NickWoodward
hmm, seems a bit strange! but ok, good to know, thanks!

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

Posted: Wed Jan 10, 2018 6:00 pm
by Sergey
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']); // <--------!!!!!! 
    }
}

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

Posted: Thu May 10, 2018 4:29 am
by SoFine
"So, elements of a boolean array are initialized to false. int, char, float to 0"

Isn't float 0.0 instead of 0?

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

Posted: Thu May 10, 2018 7:14 am
by admin
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