This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type consistancies and code question

 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a recent post about accessor methods, it was suggested that the following code sample contains type inconsistencies and that I would get compiler errors (I didn't run into any problems in Netbeans 6.5). From my understanding a type is a data type (i.e: int, string, long, array, char, boolean) and an inconsistency I would assume would be assigning a int to a string, using a string as a boolean or something like that.

Below is my simple sample code, please feel free to make suggestions and provide your reasoning of where the type inconsistencies are, the aim of the game is that I understand how to write better code.



Regards,
TY.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,

No type errors here; everything is fine. I looked at your other thread to see what you might be referring to, and I see the comment you mean. I think our friend Mr. Ritchie may have been tired, rushed, or otherwise off his game, because there actually aren't any problems in the code -- there's nothing for you to worry about here.
 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response,
Oh, another thing I have seen that you may be able to see above is:


I am aware that using "(String)" when assigning a vause is sometimes required or suggested by netbeans. In my case I am aware that it's not required, but what does this relate to? (feel free to give me some keywords that I can google)

typing in "using (string) java" didn't help much

(I forgot to ask this in my initial post.)
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "(String)" part is called a cast, and it's required only if the compiler can't determine on its own that the object on the right hand side has the proper type to be assigned to the variable on the left hand side.

What the developer is telling the compiler by that is something like "I know you don't know that this object has the proper type, but take my word for it that it does, and let me assign it to this variable."

If at runtime the object does not actually have the proper type you'll get a ClassCastException (so it's not like in C, where a cast essentially means "bend this object in whichever way you have to to make it fit this other data type").
[ January 01, 2009: Message edited by: Ulf Dittmer ]
 
Thomas Young
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explaining this to me,
Hope you are having a Happy New Year.

Regards,
TY.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hi Thomas,

No type errors here; everything is fine. I looked at your other thread to see what you might be referring to, and I see the comment you mean. I think our friend Mr. Ritchie may have been tired, rushed, or otherwise off his game, because there actually aren't any problems in the code -- there's nothing for you to worry about here.


Obviously I didn't read it right; I must have mistakenly thought he was casting a String to a boolean. Sorry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic