About Question enthuware.ocajp.i.v7.2.1077 :
Moderator: admin
-
- Posts: 13
- Joined: Mon Jun 16, 2014 1:07 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1077 :
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
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
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
The code given in the question is correct. I just verified it.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Dec 04, 2014 12:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
Hello,
the answer said that ch is a numeric type , but is
static char ch;
the answer said that ch is a numeric type , but is
static char ch;
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
Yes, and char is also considered as one of the numeric types. A char is basically an unsigned number.hadesgrid@gmail.com wrote:Hello,
the answer said that ch is a numeric type , but is
static char ch;
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Feb 15, 2016 2:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
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
System.out.println(ch); ------------ > nothing
System.out.println(Character.isDigit(ch)); ----------- > false
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
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.
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.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
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!!
nick
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!!
nick
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
It is not space. It is null. 32 is space. A null character, when displayed on a console looks like a space/blank.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!!
nick
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
hmm, seems a bit strange! but ok, good to know, thanks!
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
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']); // <--------!!!!!!
}
}
-
- Posts: 1
- Joined: Thu May 10, 2018 4:21 am
Re: About Question enthuware.ocajp.i.v7.2.1077 :
"So, elements of a boolean array are initialized to false. int, char, float to 0"
Isn't float 0.0 instead of 0?
Isn't float 0.0 instead of 0?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1077 :
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
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.
Who is online
Users browsing this forum: No registered users and 150 guests