Determine the data types

119 Views Asked by At

I'm very new to programming and still getting confused with some things. Below is a class exercise.

We have to use the context in the below code to determine the data type of the identifiers in the code.

if (a.length( ) > 10)

{

     b = ! ( c < 4 );

     z = ugly ( a, b, c – 9 );

}

if ( z.equals( “I think I got it” ) )

{

     System.out.println ( “Yea” );

}

Here were my answers: a; is int

b; is int

c; is int

z; is String

I'm sure I got a few wrong. I guess I get confused when I see 'a' & 'z' in multiple places. Again, I'm just trying to get a better understanding at identifying them.

2

There are 2 best solutions below

0
On BEST ANSWER
a.length()

length() is a method of the String object;

c < 4 

This is boolean test (result can be true or false) where c can be number (int).

z.equals( “I think I got it” )

equals is a method of Object, but it checks z value in this case with String.

So a is String

b is boolean

c is int or other numeric (maybe long or byte, short)

z is String or otehr non-primitive Object

1
On

a cannot be an int because you cannot call methods like .length() on an int.

b cannot be an int because in the line b = ! ( c < 4 );, the expression on the right side is of type boolean and you cannot assign a boolean to an int.

c might indeed be an int, but it could also be a long.

z might indeed be a String, but it could in principle be any non-primitive type (because all objects have an equals() method).