• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

What is type safety?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic