Revision
Revision
Revision
ASSISTANT
PROFESSOR, CT-UG
KEC
REVISION
East
South
West
North
South
7
7
7
12
Built-in Annotation in Java
1. @Override
2. @Deprecated
3. @SuppressWarnings
Types of Annotations
1.Marker Annotation - The purpose of marker annotation is to label a declaration. It
does not contain any kind of data or members.
EX : @Override
2.Single value Annotation - An annotation that only contains one data value or method
is called Single value annotation. The data element is named as value by default.
Ex: @Annotaionexample("DataMemberName")
3.Full Annotation - This kind of annotation has multiple data elements split by using a
comma.
Ex: @Annotaionexample(value1=" DataMemberName", value2=" DataMemberName")
Spelling mistakes
hello everyone
hello everyone
BYTE STREAM
READ & WRITE
Reading contents from the File using FileInputStream class
FileContents :
Welcome to geeksforgeeks
Writing contents into the File using FileOutputStream class
CHARACTER
STREAM
READ & WRITE
GENEREICS -
PARAMETERIZED
TYPES
Definition:
Generics in Java is a mechanism that allows writing code for one type (say T)
that is applicable for all types of data, instead of writing separate classes for
each type.
3.Object is the superclass of all other classes, an Object reference can refer to any
type object.
java.lang.Integer
88
java.lang.String
hello
T is the name of a type parameter
T is used within Gen whenever the type parameter is needed
Gen is a generic class, which is also called a parameterized type.
The type parameter T can also be used to specify the return type of a
method, as is the case with the get( ) method
class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));// true
false true
} false
}
The == operator compares references not values.
class Sample
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true
System.out.println(s1==s3);//false
}
} true
false
compareTo() method compares the given string with the current string
lexicographically. It returns a positive number, negative number, or 0
1.if s1 > s2, it returns positive number
2.if s1 < s2, it returns negative number
3.if s1 == s2, it returns 0
10
public class Main { The substring is: Scaler
public static void main(String[] args) { The substring is: Welcome
String s = "Welcome to Scaler“;
// 1st method
System.out.println("The substring is: " + s.substring(11));
// 2nd method
System.out.println("The substring is: " + s.substring(0, 7));
}
}
string.charAt(int index)
class Main {
public static void main(String[] args)
{
String str = "Learning Java";
System.out.println(str.charAt(0)); Lg
System.out.println(str.charAt(7));
}
}
class Sample
{
public static void main(String args[])
{
String str = "Java“;
char x = str.charAt(-1);
System.out.println(str);
System.out.println(x);
String index out of range: -1 }
}
The startsWith( ) method determines whether a given String begins with a
specified string.
The endsWith( ) determines whether the String in question ends with a
specified string.
Here, startIndex specifies the index into the invoking string at which point the search
will begin.
Example,
"Foobar".startsWith("bar", 3) returns true.
STRINGBUFFER
public class ReplaceExample1
{
public static void main(String args[])
{
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');
System.out.println(replaceString); replaces all occurrences of 'a' to 'e'
}
}
public class ReplaceExample2
{
public static void main(String args[])
{
String s1="my name is khan my name is java";
String replaceString=s1.replace("is","was");
System.out.println(replaceString);
}
} replaces all occurrences of "is" to "was"
public class StringTrimExample
{
public static void main(String args[])
{
String s1=" hello string ";
System.out.println(s1+"javatpoint");//without trim()
System.out.println(s1.trim()+"javatpoint");//with trim()
}
}
split()
The List interface is an ordered collection that allows us to add and remove elements
1. List Interface like an array.
The Set interface allows us to store elements in different sets similar to the set in
2. Set Interface mathematics. It cannot have duplicate elements.
The Queue interface is used when we want to store and access elements in First
3. Queue Interface In, First Out(FIFO) manner.
Methods of Collection
import java.util.*;
public class ScalerTopics {
public static void main(String args[])
{
ArrayList<String> str = new ArrayList<String>();
str.add("Scaler");
str.add("Topics");
str.add("Rocks");
System.out.println("ArrayList is" + str);
}
}
[]
[4]
The LinkedList class is almost identical to the ArrayList