• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

OCP Java SE 11 Programmer I Study Guide (Sybex)

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys:

Do we consider the primitive char type to be one of the numeric types or not?
Historically, when I first learned programming, before Java was even born, I didn't, despite being used to using them arithmetically and comparing them to integers casually.
I asked each platform whether they were signed or unsigned on our first date.  I love having data type portability!
Then, as we got beyond ASCII, I always did.

On page 41 of OCP Java SE 11 Programmer I Study Guide it sounds like we do NOT:
"All of the numeric types are signed in Java"
on p. 42, if we didn't know it before, we are introduced to the fact that char values are decidedly Un-Signed, and therefore, referring to the previous page, not considered numeric.

Now this had been bothering me since I came back to studying for the Certification Exams -- do we in the Java world collectively consider char a numeric type or not?  The exams seem to be land-mines of details and tricky wording written by double-major CS lawyers....
then I get to page 97 and read "The first four relational operators in table 3.7 apply only to numeric values"
Hmmmm....they definitely apply to char values, tho possibly in a tricky way if the high bit is set -- do they zero-extend properly for comparison?  I usually don't have values that high so I'm not sure...

So they are char values because <, <=, >, >= apply to them but aren't numeric types because they aren't signed?

The way I had been thinking of it were there were three types of primitives in Java: boolean, which can convert to noting else ever even with explicit casts, unless one enlists the help of a ? : operator
char which is the only unsigned type in Java, and relational operators operate on with relish (tho again, not sure what happens with extension to int if they are in the range in which the high bit is set, seems like the kind of detail the exam could nail me with)...same thing about casting to int, does that get sign extended or not?
byte/short/int/long/float/double are clearly numeric types.  All signed, all good with all relational operators, and rules about promotion and casting that I managed to memorize (hopefully correctly) before I started the book even....

I don't know how many more times in the book (or on exams, or in life) it will be important to say whether char is or is not a numeric type, but I feel torn from what I read in the book thru p. 97.

Off to go read more about instanceof, which came up sooner than I expected.  But first I am going to hit up javac and see how char's in the top half of the range convert to int.

Thanks for all your hard work!

jesse
p.s. EDIT -- ok, large valued chars zero-extend as one would hope (well, as I would hope) when cast explicitly, or promoted implicitly, to int.  My crops are saved!  This blows the clouds of mystery away from what happens on any mixed comparisons or other expressions with int and char.  I was all worried about that because I know that char was set as 16-bit unsigned in the old days of Unicode before they included characters for things like "grandma innocently sending eggplant emoji in text because she's making eggplant for dinner" and "driver realizes they left tire inflator in other car" or however we got to 1M + 64k code point space....I still haven't figured out how to work with chars outside of BMP besides memorizing high surrogate and low surrogate ranges from the Java tutorial.  Not sure that is in scope for 819 exam, but I have always worked on code that was internationalized for global use, so I'd care anyway, and it is hinted at when API's take int where you would naively expect char.
 
Marshal
Posts: 70982
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the Java┬« Language Specification (=JLS): a char is an unsigned 16‑bit integer (small i) which is usually displayed as its Unicode equivalent. When, just after the turn of the century, Unicode introduced values ≥ 0x1_0000, Java┬« had to introduce code points which are ints encapsulating two halves of a Unicode code point. I shan't try to explain how UTF‑16 encoding achieves that. I don't know whether knowing UTF‑16 is equired for the exam, but I wouldn't set it if I were setting the exam.
All integer primitives < 32 bits are sign‑extended to ints whenever they are used for arithmetic. Since the char is never negative, sign‑extension means filling with 0s, so it looks exactly the same as zero‑extension.
As you said, you can't cast booleans; the ?: operator doesn't cast a boolean at all but chooses from two options, usually of a different type.
 
Jesse Silverman
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything you said is valuable and as far as I can tell, true.

I've read further in the Study Guide and I can point to some places where it seems that char is clearly considered a numeric type and other spots where it clearly seems that it isn't, so in that sense I consider it not yet answered.

If we say "all numeric types are signed" which the text does on page 41 I think, then clearly char ain't that.

When talking about switch case, the text says that byte/char/short/int being numeric types are all promotable to int, so it is...

I might be chasing after a distinction without a difference, the important thing is in what ways it behaves like byte/short/int and in what ways it does not, rather than what we call it.
"If you wonder how he eats and breathes and other science facts"
"Repeat to yourself it's just a show, I should really just relax."
...
You said: "As you said, you can't cast booleans; the ?: operator doesn't cast a boolean at all but chooses from two options, usually of a different type."

Even tho I hadn't ever considered Java my first language, I was proud that I'd memorized "BOOLEAN CAN'T BE PROMOTED OR CAST TO ANYTHING" and
"NO OTHER TYPE CAN BE PROMOTED OR CAST TO BOOLEAN!!"
This is important to people coming from other languages where that kind of sloppy type punning is not just permitted but downright common and encouraged.

Well right after I posted that I did the questions from Jeanne and Scott's chapter 3.  One was about permissible operations on booleans.

I saw cast and immediately said "NO!!" to myself and left it unchecked.
One end-of-chapter review question wrong right there.
Sure, it is completely pointless and useless in all cases, but you can happily cast any already boolean expression or variable to boolean.
It doesn't do anything, including causing a compile failure, so therefore it is legal and permissible.

Yeah, these exam-writers are tricky, the authors are just trying to prepare us for getting our knowledge cross-examined by language lawyers, I reckon.

Thanks,
Jesse
p.s. I also realized I should have known that char types zero-extend to int automatically by virtue of the face that they can be compared meaningfully, and all comparisons using comparison operators get the operands promoted to int.  If promotion didn't zero-extend in the case of char, then even (c1 > c2) wouldn't work for all legal char values.  Being paranoid about that one was just silly.
 
Campbell Ritchie
Marshal
Posts: 70982
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . you can happily cast any already boolean expression or variable to boolean. . . .

That is called an identity conversion and any identity conversion is “trivially permissible”. Sorry; I had forgotten about that. The JLS link says you can provide a cast operator for clarity: remind readers of the current type of a variable.
 
author & internet detective
Posts: 40282
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anyone reading this, the exam doesn't expect you to know this level of detail.
 
Jesse Silverman
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on Chapter 2 of the Sybex 816 Book, I finally found a good use for "identity conversions" -- they are a great peg to hang an annotation to for expressions.
So this isn't as useless as I thought it was (not sure how often I'd be using it with Binary expressions, but other types, sure!)
 
reply
    Bookmark Topic Watch Topic
  • New Topic