Page 1 of 1

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

Posted: Mon Mar 03, 2014 12:13 am
by shining_dragon
why isn't the program will print ascii number equivalent to 'a'?

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

Posted: Mon Mar 03, 2014 12:18 am
by admin
Because that is how println method of PrintStream is coded. Please see this: http://docs.oracle.com/javase/7/docs/ap ... rint(char)

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

Posted: Mon Sep 22, 2014 9:16 am
by nikolaj.mattrup
Can someone please tell what would this print if c was incremented, thx!

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

Posted: Mon Sep 22, 2014 10:06 am
by admin
Please try it out and then ask if you do not understand the output.

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

Posted: Sat Oct 25, 2014 6:09 am
by caminata
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?

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

Posted: Sat Oct 25, 2014 10:09 am
by admin
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.

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

Posted: Sun Oct 26, 2014 4:25 am
by caminata
Thanks for your explanations. I understand now.

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

Posted: Tue Feb 03, 2015 2:29 am
by gparLondon
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?

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);
	}
Regards,
GPAR

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

Posted: Tue Feb 03, 2015 2:44 am
by admin
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.

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

Posted: Tue Feb 03, 2015 2:46 am
by gparLondon
Thanks, for the quick reply.

Regards,
GPAR

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

Posted: Thu May 21, 2015 4:55 am
by pbonito
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

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

Posted: Thu May 21, 2015 5:35 am
by admin
What happened when you tried it out?

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

Posted: Thu May 21, 2015 3:47 pm
by pbonito
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.

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

Posted: Thu May 21, 2015 7:55 pm
by admin
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?

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

Posted: Fri May 22, 2015 12:31 am
by pbonito
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.

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

Posted: Fri May 22, 2015 9:32 pm
by admin
In your previous post you asked about = and not ==.
but a = i gives a compile error. So I don't understand which is the rule.
My statement about why compiler won't allow a = i was for that. Not for a ==i.


a == i works fine because, for primitives, == operator checks the value and it is certainly possible that a and i contain the same value.

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

Posted: Sat Jan 07, 2017 11:46 am
by aar2416
how can i be promoted to float

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

Posted: Sat Jan 07, 2017 11:39 pm
by admin
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;

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

Posted: Sun Jan 08, 2017 10:48 am
by aar2416
Yes but that is during the time of assignment...here comparison is being done...so will int be converted to float

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

Posted: Sun Jan 08, 2017 11:19 am
by admin
Yes, you should try System.out.println(f == i);

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

Posted: Mon Jan 09, 2017 12:56 pm
by aar2416
ohhk got it

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

Posted: Sat Oct 07, 2017 4:13 am
by JuergGogo

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      
    }
}
1+2: char remains char, then printed like a String, since SOP uses toString().
3: c is implicitly widened to int, so output is (int)char.