Java String Class n Annotations (1)
Java String Class n Annotations (1)
1. char[] ch={'j','a','v','a','t','p','o','i','n','t'};
2. String s=new String(ch);
is same as:
1. String s="javatpoint";
CharSequence Interface
The CharSequence interface is used to represent the sequence of characters.
String, StringBuffer and StringBuilder classes implement it. It means, we can
create strings in Java by using these three classes.
The Java String is immutable which means it cannot be changed. Whenever
we change any string, a new instance is created. For mutable strings, you
can use StringBuffer and StringBuilder classes.
We will discuss immutable string later. Let's first understand what String in
Java is and how to create the String object.
1. By string literal
2. By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
1. String s="welcome";
Each time you create a string literal, the JVM checks the "string constant
pool" first. If the string already exists in the pool, a reference to the pooled
instance is returned. If the string doesn't exist in the pool, a new string
instance is created and placed in the pool. For example:
1. String s1="Welcome";
2. String s2="Welcome";//It doesn't create a new instance
In the above example, only one object will be created. Firstly, JVM will not
find any string object with the value "Welcome" in string constant pool that is
why it will create a new object. After that it will find the string with the value
"Welcome" in the pool, it will not create a new object but will return the
reference to the same instance.
Note: String objects are stored in a special memory area known as the "string constant
pool".
2) By new keyword
1. String s=new String("Welcome");//creates two objects and one reference var
iable
In such case, JVM will create a new string object in normal (non-pool) heap
memory, and the literal "Welcome" will be placed in the string constant pool.
The variable s will refer to the object in a heap (non-pool).
Output:
java
strings
example
The above code, converts a char array into a String object. And displays the
String objects s1, s2, and s3 on console using println() method.
Java String class methods
The java.lang.String class provides many useful methods to perform
operations on sequence of char values.
First, we will learn some built-in annotations then we will move on creating
and using custom annotations.
o @Override
o @SuppressWarnings
o @Deprecated
o @Target
o @Retention
o @Inherited
o @Documented
ADVERTISEMENT
@Override
@Override annotation assures that the subclass method is overriding the
parent class method. If it is not so, compile time error occurs.
Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is
better to mark @Override annotation that provides assurity that method is
overridden.
1. class Animal{
2. void eatSomething(){System.out.println("eating something");}
3. }
4. class Dog extends Animal{
5. @Override
6. void eatsomething(){System.out.println("eating foods");}//should be eatSom
ething
7. }
8. class TestAnnotation1{
9. public static void main(String args[]){
10. Animal a=new Dog();
11. a.eatSomething();
12. }}
Test it Now
Output:Comple Time Error
@SuppressWarnings
@SuppressWarnings annotation: is used to suppress warnings issued by the
compiler.
1. import java.util.*;
2. class TestAnnotation2{
3. @SuppressWarnings("unchecked")
4. public static void main(String args[]){
5. ArrayList list=new ArrayList();
6. list.add("sonoo");
7. list.add("vimal");
8. list.add("ratan");
9.
10. for(Object obj:list)
11. System.out.println(obj);
12.
13. }}
Test it Now
Now no warning at compile time.
@Deprecated
@Deprecated annoation marks that this method is deprecated so compiler
prints warning. It informs user that it may be removed in the future versions.
So, it is better not to use such methods.
1. class A{
2. void m(){System.out.println("hello m");}
3.
4. @Deprecated
5. void n(){System.out.println("hello n");}
6. }
7. class TestAnnotation3{
8. public static void main(String args[]){
9.
10. A a=new A();
11. a.n();
12. }}
Test it Now
At Compile Time:
Note: Test.java uses or overrides a deprecated API.
At Runtime:
hello n
1. @interface MyAnnotation{}
Types of Annotation
There are three types of annotations.
1. Marker Annotation
2. Single-Value Annotation
3. Multi-Value Annotation
1) Marker Annotation
An annotation that has no method, is called marker annotation. For example:
1. @interface MyAnnotation{}
2) Single-Value Annotation
An annotation that has one method, is called single-value annotation. For
example:
1. @interface MyAnnotation{
2. int value();
3. }
We can provide the default value also. For example:
1. @interface MyAnnotation{
2. int value() default 0;
3. }
1. @MyAnnotation(value=10)
3) Multi-Value Annotation
An annotation that has more than one method, is called Multi-Value
annotation. For example:
1. @interface MyAnnotation{
2. int value1();
3. String value2();
4. String value3();
5. }
6. }
1. @interface MyAnnotation{
2. int value1() default 1;
3. String value2() default "";
4. String value3() default "xyz";
5. }
How to apply Multi-Value Annotation
1. @MyAnnotation(value1=10,value2="Arun Kumar",value3="Ghaziabad")
@Target
@Target tag is used to specify at which type, the annotation is used.
FIELD Fields
METHOD Methods
CONSTRUCTOR Constructors
PARAMETER Parameter
Example to specify annoation for a class
1. @Target(ElementType.TYPE)
2. @interface MyAnnotation{
3. int value1();
4. String value2();
5. }
@Retention
@Retention annotation is used to specify to what level annotation will be
available.
RetentionPolicy Availability
RetentionPolicy.CLAS refers to the .class file, available to java compiler but not to
S JVM . It is included in the class file.
1. @Retention(RetentionPolicy.RUNTIME)
2. @Target(ElementType.TYPE)
3. @interface MyAnnotation{
4. int value1();
5. String value2();
6. }
File: Test.java
1. //Creating annotation
2. import java.lang.annotation.*;
3. import java.lang.reflect.*;
4.
5. @Retention(RetentionPolicy.RUNTIME)
6. @Target(ElementType.METHOD)
7. @interface MyAnnotation{
8. int value();
9. }
10.
11. //Applying annotation
12. class Hello{
13. @MyAnnotation(value=10)
14. public void sayHello(){System.out.println("hello annotation");}
15. }
16.
17. //Accessing annotation
18. class TestCustomAnnotation1{
19. public static void main(String args[])throws Exception{
20.
21. Hello h=new Hello();
22. Method m=h.getClass().getMethod("sayHello");
23.
24. MyAnnotation manno=m.getAnnotation(MyAnnotation.class);
25. System.out.println("value is: "+manno.value());
26. }}
Test it Now
Output:value is: 10
1.