Revision

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 52

SARANYA S S

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

Main.java:11: error: method does not override


or implement a method from a supertype
@Override ^ 1 error
Streams

1. A stream is an abstraction that either produces or consumes information.


2. A Stream is linked to a physical layer by java I/O system to make input and
output operation in java.
3. A stream can be defined as a sequence of data.
4. The InputStream is used to read data from a source and the OutputStream is used
for writing data to a destination.
5. InputStream and OutputStream are the basic stream classes in Java.
r
r

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.

1.Using generics, it is possible to create a single class, that automatically works


with different types of data.

2.A class, interface, or method that operates on a parameterized type is called


generic, as in generic class or generic method.

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.

T is used to declare an object called x, as shown here:


T is a placeholder for the actual type that will be specified when a Gen object is
created.
x will be an object of the type passed to T.
Ex: if type String is passed to T, then in that instance, x will be of type String.

Gen’s constructor: its parameter, y, is of type T. The actual type of y is


determined by the type passed to T when a Gen object is created.

The type parameter T can also be used to specify the return type of a
method, as is the case with the get( ) method

It first creates a version of Gen for integers


It second creates a version of Gen for Strings
java.lang.Integer
java.lang.String
88
hello
STRING
It is used in authentication (by equals() method), sorting (by compareTo() method), reference
matching (by == operator) etc.
There are three ways to compare String in Java:
1.By Using equals() Method
2.By Using == Operator
3.By compareTo() Method
The String class equals() method compares the original content of the
string.
It compares values of string for equality.

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

public class CompareToExample{


public static void main(String args[]){
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
String s5="flag";
System.out.println(s1.compareTo(s2));//0 because both are equal
System.out.println(s1.compareTo(s3));//-5 because "h" is 5 times lower than "m"
System.out.println(s1.compareTo(s4));//-1 because "l" is 1 times lower than "m"
System.out.println(s1.compareTo(s5));//2 because "h" is 2 times greater than "f"
}
36

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));
}
}

public class Main {


public static void main(String[] args) {
String s = "Scaler is part of Interviewbit"; Hey
System.out.println("Hey" + s.substring(0, 0));
}
}
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));
}
}

public class Main {


public static void main(String[] args) {
String s = "Scaler is part of Interviewbit"; Hey
System.out.println("Hey" + s.substring(0, 0));
}
}
class TestStringConcatenation2
{ 80Sachin4040
public static void main(String args[])
{
String s=50+30+"Sachin"+40+40;
System.out.println(s);
}
}
returns the character value at the stated index number when searched in a string.

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.

If the string matches, true is returned. Otherwise, false is returned.


Example,
"Foobar".endsWith("bar“) and "Foobar".startsWith("Foo“) are both true.
A second form of startsWith( ), shown here, lets you specify a starting point:
boolean startsWith(String str, int startIndex)

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()

result = Java, is, a, fun, programming, language,


isEmpty()
COLLECTIONS
Java Collection Interface
The Collection interface is the root interface of the Java collections framework. There is no
direct implementation of this interface. However, it is implemented through its
subinterfaces like List, Set, and Queue.

E specifies the type of objects


that the collection will hold

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);
}
}

ArrayList is [Scaler, Topics, Rocks]


cars.get(0); Volvo

[Opel, BMW, Ford, Mazda]

[BMW, Ford, Mazda]

[]

[4]
The LinkedList class is almost identical to the ArrayList

You might also like