wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Type consistancies and code question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type consistancies and code question" Watch "Type consistancies and code question" New topic
Author

Type consistancies and code question

Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
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
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
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 ]

Ping & DNS - my free Android networking tools app
Thomas Young
Greenhorn

Joined: Jul 17, 2008
Posts: 29
Thanks for explaining this to me,
Hope you are having a Happy New Year.

Regards,
TY.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type consistancies and code question
 
Similar Threads
What is an Accessor Method?
need help with assignment urgently
Calling a method from another class
JFormattedTextField MaskFormatter issues: Please Help its urgent
Problem with default '' , 0 and 0.0 being inserted into database while using JSTL.