Introduction to Annotations2:16 with Chris Ramacciotti
During this first video, we get a high-level look at what Java annotations look like in code, and at what they can do for us.
The Java compiler you use - likely javac or Eclipse Compiler for Java (ECJ) if you're using Eclipse - is what translates your source code in Java bytecode. As a developer, you code the ".java" files, and from these the compiler creates ".class" files (bytecode). This bytecode is what is actually loaded into the Java Virtual Machine (JVM) when you run your Java application.
What makes Java applications so portable is that in general, Java bytecode can run on any device that contains a valid implementation of a JVM. This implementation could be a JVM written for a Linux machine, Mac, PC, or another platform. The JVM is what will compile bytecode to the machine code that is understood natively by the machine on which it is running. The JVMs for each platform had to be created separately, since how a JVM interacts with Windows is different than how it must interact with OSX, for example.
[MUSIC] 0:00 [SOUND] Hello there. 0:03 I'm Chris and I'm a Java teacher here at Treehouse. 0:05 If you've been working through some of our Java or 0:09 Android coursework, you may have encountered what's called an annotation. 0:11 Annotations are used in Java to give a compiler special instructions. 0:17 Remember that the compiler is what transforms your Java code into byte code, 0:22 which is what can be understood by the Java virtual machine. 0:26 If you want to know more about the Java compiler, check the teacher's notes. 0:29 Beyond compiler instructions, 0:34 you can use annotations to wedge enhancements into your code, 0:36 much like the inclusion of hooks in a plugin architecture such as WordPress. 0:39 The enhancements certainly could be written by you and 0:45 I'll show you how to do this. 0:48 But more likely, you'll be leveraging third party libraries for this and simply 0:49 using annotations to drive in those wedges where you wanna stick your functionality. 0:54 As you may have seen, one of the greatest advantages of Java is its portability. 0:59 That is, Java has an extensive presence on platforms of all types. 1:04 These include mobile devices, enterprise systems like in banks or 1:09 hospitals, or gadgets such as refrigerators or arduinos. 1:13 This versatility has led to an ever expanding set 1:18 of wonderfully robust libraries made available to developers just like you. 1:21 The most popular modern libraries make heavy use of annotations. 1:27 And even though annotations have been available in Java since 2005, 1:31 these libraries have only recently included annotations 1:35 as the de facto way of integrating their libraries. 1:39 Not sure what I'm talking about with all this annotation business? 1:43 Annotations in Java start with the @ sign, and 1:47 there are probably a few that you've seen already. 1:51 We'll start with a few of the simplest and most widely used Java annotations. 1:54 The override, deprecated and suppress warnings annotations. 1:59 These three annotations are built in to the Java language. 2:05 And all give various instructions to the compiler. 2:09 [SOUND] Let's look at each one individually in the next few videos. 2:12
You need to sign up for Treehouse in order to download course files.Sign up