About Question enthuware.ocajp.i.v7.2.1271 :
Moderator: admin
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1271 :
why isn't the program will print ascii number equivalent to 'a'?
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Because that is how println method of PrintStream is coded. Please see this: http://docs.oracle.com/javase/7/docs/ap ... rint(char)
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Sep 19, 2014 4:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Can someone please tell what would this print if c was incremented, thx!
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Please try it out and then ask if you do not understand the output.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Oct 25, 2014 6:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Why ist the part '((int) (f + d)) of the if-statement valid?
When trying to add float and double value (e.g. float k = f + d;) it does not compile. Why does it work in the if-statement then?
When trying to add float and double value (e.g. float k = f + d;) it does not compile. Why does it work in the if-statement then?
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
f+d returns a double. You can't assign a double to a float without a cast so float k = f + d; doesn't compile. But in case of the if statement, you are not assigning f+d to any variable. Further, there is an explicit cast.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Oct 25, 2014 6:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Thanks for your explanations. I understand now.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
What else shall we know about the characters for the exam? Shall we remember all the code for characters as well as ints? like int 0 is different from '0'?
Should we expect codes like this in the exam?
Regards,
GPAR
Should we expect codes like this in the exam?
Code: Select all
public static void main(String args[])
{
char c=48;
switch(c)
{
case 'a':System.out.println("a"); break;
case '1': System.out.println("One"); break;
case '0':System.out.println("Zero"); break;
case 48:System.out.println("Fourty Eight"); break;
}
//System.out.println(++c);
}
GPAR
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
You only need to know that '0' is not same as zero.
You don't need to know the int values for any character but again, you should know that a char 'a' is not same as int 0xa.
-Paul.
You don't need to know the int values for any character but again, you should know that a char 'a' is not same as int 0xa.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Thanks, for the quick reply.
Regards,
GPAR
Regards,
GPAR
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
I don't understand the rule about == operator. I know that you can't apply it to variables referring to objects not in the same hierarchy, but what about primitives?
You can't compare a byte, char, .. to boolean ->ok
You can compare an int to flaot ->ok since int can be converted to float
but what about comparing int and short to char?
Thanks
You can't compare a byte, char, .. to boolean ->ok
You can compare an int to flaot ->ok since int can be converted to float
but what about comparing int and short to char?
Thanks
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
It works.
short i = 3;
char a= 'a';
System.out.println(i==a); -> print false
but a = i gives a compile error. So I don't understand which is the rule.
short i = 3;
char a= 'a';
System.out.println(i==a); -> print false
but a = i gives a compile error. So I don't understand which is the rule.
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
a = i does not compile because char type has a different range than short type. So unless the compiler knows for sure that the value you are putting in a variable will fit in that type, it will not allow the assignment. It will work if i is final.
This should be covered in any regular Java book. Which book are you following?
This should be covered in any regular Java book. Which book are you following?
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
yes, I know that and this not surprise me. I don't understand why a==i is permitted since I know that == can't be applied to different type. == works if one of the two is promoted to the other type, but as you stated char can't be promoted to short, and short can't be promoted to char.
I'm following Bates & Sierra.
I'm following Bates & Sierra.
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
In your previous post you asked about = and not ==.
a == i works fine because, for primitives, == operator checks the value and it is certainly possible that a and i contain the same value.
My statement about why compiler won't allow a = i was for that. Not for a ==i.but a = i gives a compile error. So I don't understand which is the rule.
a == i works fine because, for primitives, == operator checks the value and it is certainly possible that a and i contain the same value.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Jan 02, 2017 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
how can i be promoted to float
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Java allows int to be promoted to float. For example, the following is valid:
Code: Select all
float f = 0.0f;
int i = 10;
f = i;
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Jan 02, 2017 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Yes but that is during the time of assignment...here comparison is being done...so will int be converted to float
-
- Site Admin
- Posts: 10114
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Yes, you should try System.out.println(f == i);
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Jan 02, 2017 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
ohhk got it
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1271 :
Code: Select all
public class TestClass {
public static void main( String[] args ) {
char c = 'a';
System.out.println( ++c ); // 1 - output: b
System.out.println( c+=1 ); // 2 - output: c
System.out.println( c + 1 ); // 3 - output: 100
}
}
3: c is implicitly widened to int, so output is (int)char.
Who is online
Users browsing this forum: No registered users and 15 guests