aspose file tools
The moose likes Java in General and the fly likes What is type safety? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "What is type safety?" Watch "What is type safety?" New topic
Author

What is type safety?

Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 37
I don't understand the concept of type safety.

I recently implemented metamodels to build queries with the criteria api. Metamodel use is recommended for "type safety." It was a challenging and interesting to build these in maven and eclipse, but I'm not sure why I did it. What is the difference between this:



and this:



?

TDR
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 194
It kind of seems like you do know what type safety is, in that you are using Generics. So are you asking what is the difference in type safety as used specifically between the two statements you posted?

~Bill
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

It looks like the only difference is that in the first version you are using a constant Logs_.hostname that is defined somewhere, while in the second you're using a literal "hostname". That difference doesn't have anything to do with type safety.

Why are you asking what type safety is together with a completely different question about the difference between those two lines?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4749
    
    7

Tanya Ruttenberg wrote:I don't understand the concept of type safety.

I have to admit, I'm quite interested in the 'rt.<String>' construct. Never seen it before (or maybe I have and I'm just being dumb).

Other than that: what the others said.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

Type safety means that you use the type system of the programming language so that the compiler can check if you're using all the right types for variables etc. If you make a mistake, the compiler will show you an error. The good thing is that mistakes will be caught early, at compile time, and not later, when you run the program (with an unexpected exception that crashes the program).
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3793
    
    1

There is a parallel between what you've got there and type safety, though, that might explain where the confusion came from.

Consider your second example: if "hostname" appears once in your application it makes little difference. But if you're using it several times, then it's easy to mistype it somewhere. The program will compile as usual, but it won't work correctly. And sometimes it can be very hard to work out exactly what the problem was. In your second example, you've (probably) got a constant Logs_.hostname. If you spell that wrong, the compiler will warn you, because it won't recognise the variable you're referring to.

With type-safety, as Jesper said, the idea is that the compiler will check that the type of a value. So in both cases you're increasing the ability of the compiler to notice your mistakes. And this is a good thing, because it's far faster to identify and fix errors your compiler can catch compared to ones that you can only see when you run the code.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What is type safety?
 
Similar Threads
Problem in javaScript for Having more Action per Form . it does not set variabl is JS
Problem with localization (MyFaces and JSTL)
how to transfer data in a object to another class
Weblogic 10.4.5 404 Not Found
how to transfer data in a object to another class