• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why should I use annotations and what are those?

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I am Chaitanya, I am new to annotations, I don't know anything about it, I searched the web, I dint understand the usage, can anyone give some examples where to use annotation, where in programming one can come across the usage of annotations and what is meant by annotations.

Actually I want to learn hibernate, people advised me to learn few thing, in those annotations is one part. So please can anyone help me with annotations.

I will be very much thankful to you people and thanks in advance. Have a nice day.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start here: http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr.Lester, first I went to that site only, I dint understand anything and I came here. They are not talking about where to use, they are focusing on how to use and its not in detailed manner.

Please help me.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations are a manner in which you can add meta-data to your code. For instance that a methods shouldn't be used anymore (@Deprecated). You could see annotations as a kind of flag with information. Another program can spot those flag and perform logic based on that. For instance annotations are heavily used for ORM with JPA to create a database model.
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhakar Reddy Bokka wrote:I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.


Maybe that annotations for Java SE aren't that useful but for Java EE they're great. The don't save a few lines of code but allow us to configure servlets, components, JPA settings, do validation directly on the entity classes and much more. Not to mention that they replace lot's of xml files and overall reduce the complexity of the code. With them coding would be much harder.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you in reducing lot's of xml files in configuration. But i feel it creates more complexity in understanding code and configurations. It all depends on the perception of the developer.

If the developer is new to annotations, they definitely feel a lot of complexity in understanding them.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhakar Reddy Bokka wrote:I do not see any great use of annotations in java, apart from they used for suppress warnings in jdk5 and above, and for showing deprecated methods.

We generally use these in EJB3, JPA, Hibernate, since these saves developers few lines of code, and we have predefined annotations available in the jar files.


How can you not see the use, then list a bunch of uses?

Spring uses annotations, various frameworks use annotations, Lombok uses annotations, they're all over the place besides EJBs and JEE stuff. They are of tremendous value under some circumstances.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Override is a great timesaver during development, and much more important than either @Deprecated or @SuppressWarnings (which are mere conveniences).
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:
Spring uses annotations, various frameworks use annotations, Lombok uses annotations, they're all over the place besides EJBs and JEE stuff. They are of tremendous value under some circumstances.



I feel they all are using annotations at the cost of complexity to understanding.
Btw, could you please eloborate the uses?? apart from programatic configurations and less code to write(since predefined annotations provided).
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhakar Reddy Bokka wrote:I feel they all are using annotations at the cost of complexity to understanding.


It's a different *kind* of complexity, but I wouldn't call it *more* complexity.

While I still prefer XML configuration for some things, without good IDE support, maintaining a separate configuration file is *more* work, and doesn't provide compile-time type safety like annotations do. Not only are there more artifacts, but configuration isn't localized to the code that uses it.

Btw, could you please eloborate the uses??


Well, I kind of already did, but okay.

Consider Project Lombok. Consider Spring Contracts. Consider being able to define method access control via annotations. Consider field validation driven by annotations. Consider test framework annotations (both JUnit and TestNG use them in similar, but different, ways). State machine transitions. Consider annotating code for completely arbitrary business or documentation reasons. Consider annotating code for any run-time class-loading tricks (like Lombok, but it can be *anything*, and it's *powerful*).

The uses of annotations are limited only by your imagination and technical skill.

Consider Gag annotations.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote: . . . Gag annotations.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Yeah--I already dropped it into a couple of projects--I love it.)
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, thank you all for the replies, really saying I dint understand the user, I think it is because you all experts, you all are talking in a higher level, I cant understand that because I am new to annotations.

Please if you don't mind can anyone give a general example

Suppose take an example. Suppose some one asked me why to use charAt(int) function. I explain the person like this, suppose you have a string variable and you want to check it whether it is an integer or string so that you can convert it into integer using the parseInt(String) method of Integer class. So you have to check each and every character in the string from first to end of string, so to read each and every character you need a function, that is charAt(int) function, for this purpose you can use it.

I ll say it in this way.

Please can anyone tell me such kind of example. Please don't mind and thank you all in advance.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Override Example

Suppose you're writing a class that extends another class.

The new class overrides a method of the superclass.

That method can be annotated with the @Override annotation.

If the method is overridden incorrectly, for example, you leave out a parameter, or change the return type, it no longer overrides the superclass's method. If it's annotated with @Override, the compiler will complain and give a compilation error saying that the method *doesn't* override anything.

So in this case the annotation provides a compile-time check to see if what you *intended* is what you actually *implemented*.

Hibernate Example

See Using @Transactional.

For example, we want to make every method within a service run within a transaction. Along with some Spring configuration, it's as simple as adding the annotation.That annotation also takes arguments, thusly:Not sure what else to tell you: until you start using them you won't understand why you'd use them.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you David for your reply and patience in reading my post, I understood the first example, I dint understood the second one, as you said, may be I have to start using annotation.

Once again thanks a lot David, have a good day.
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic