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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post by shining_dragon »

why isn't the program will print ascii number equivalent to 'a'?

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

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

Post 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)
If you like our products and services, please help us by posting your review here.

nikolaj.mattrup
Posts: 2
Joined: Fri Sep 19, 2014 4:07 am
Contact:

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

Post by nikolaj.mattrup »

Can someone please tell what would this print if c was incremented, thx!

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

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

Post by admin »

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.

caminata
Posts: 2
Joined: Sat Oct 25, 2014 6:05 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

caminata
Posts: 2
Joined: Sat Oct 25, 2014 6:05 am
Contact:

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

Post by caminata »

Thanks for your explanations. I understand now.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Thanks, for the quick reply.

Regards,
GPAR

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

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

Post 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

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

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

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

Post 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.

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

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

Post 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?
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

aar2416
Posts: 10
Joined: Mon Jan 02, 2017 9:41 am
Contact:

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

Post by aar2416 »

how can i be promoted to float

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

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

Post 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;
If you like our products and services, please help us by posting your review here.

aar2416
Posts: 10
Joined: Mon Jan 02, 2017 9:41 am
Contact:

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

Post by aar2416 »

Yes but that is during the time of assignment...here comparison is being done...so will int be converted to float

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

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

Post by admin »

Yes, you should try System.out.println(f == i);
If you like our products and services, please help us by posting your review here.

aar2416
Posts: 10
Joined: Mon Jan 02, 2017 9:41 am
Contact:

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

Post by aar2416 »

ohhk got it

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 115 guests