1) Data types
Primitive: int, long, double, char, Boolean
Non primitive: Stores more than one value
-------
2) Array
Derived data type
Collection of homogenous data
int a[] = new int []
Int a[] = {100,101,102}
a(0)
a.length()
Enhanced for loop: for (int x :a)
System.println(x)
int a[] [] = new int [] []
Row= a.length()
_____
3) Take input from user
Arrays.toString
Scanner sc = new Scanner()
num.nextInt()
num.nextDouble()
name.next() - String
----------
4) String
s.length()
Welcome.Concat(Text)
s.contains
s.equals
s.replace()
s.substr()
s.split()
s.toCharArray()
str.equalsIgnoreCase(str2)
-----------
RandomStringUtils.randomAlphabetic()
RandomStringUtils.randomNumeric()
import org.apache.commons.lang3
----------
String is immutable- cannot be changed but handled by adding in new variable
Stringbuilder sb = new Stringbuilder(Wel)
sb.reverse()
== Compares object
Equals: Compares values
---------
5) Constructor - Does not return anything
Assign data into variables
No logic should be added
One object can call only one constructor
----------
6) Polymorphism: Achieved through method overloading
Void add();
Same method name
Different number of parameters
Different data type
Different orders
Return doesn't matters
--------
7) Encapsulation
Source- Generate getters and setters
------
8) Errors:
1) Syntax errors
2) Logical error
3) No global variables in Java
( Only class and method variable)
---------
9) Static : Value can be changed
Static variable
Static Method
Static Method and variables can be accessed directly. (Without Object)
-----------
9) Basic Java purpose
System: Predefined Class
Out: Static Variable
Println() - Method under Print stream class
Class name starts with capital letter
---------
Public static void main ( String[] args)
Accepts array as parameters
JVM look for main method
------
10) ArrayList: Dynamic in space
Heterogeneous values
ArrayList mylist = new ArrayList
Accepts Objects
Primitive data type with wrappers<Integer>
al.add()
al.remove()
al.removeAll(mylist2)
ai.clear()
al.get()
Display all elements
Iterator it = ai.iterator()
al.hasNext()
al.next()
-------
Hashset
HashSet myset = new Hashset()
myset.remove(10.5) Direct add value
No insertion allowed
Accessing specific element not possible
convert set to list:
ArrayList al = new ArrayList(myset)
Values can be read using For...each
myset.clear()
myset.add
myset.remove
myset.size()
----------
HashMap
HashMap hm<Integer><String> = new HashMap<Integer><String>()
hm.put(100,amit)
hm.remove(100)
hm.get(100)
hm.keySet()
hm.values()
hm.entrySet()
---------
Typecasting
Upcasting (int--long & float--double)
Downcasting (long--int & double--float)
Rules:
Animal an = new Dog()
Dog dg= (Dog) an
1) Relation between C and D
2) C must be same or child of A
3) The underlying object ype of d must be either same or child of C
---------
Overriding - Related to Inheritance
Same method name and parameters : Implementation is different (Declaration should be same)
----------
Final:
Variable: Cannot change value
Methods: Cannot override
Class: Cannot extend the class
---------
Super: Gives value from parent class
Works for immediate parent class
super(driver) : Calling constructor of parent class and passing driver as argument
---------
Interface
It has final & static variables by default
Has abstract methods
Interface to Interface - Extends
Interface to Class - implements
Java 8 onwards allows implementing body for Static and default methods
All methods are by default public
Can create an object but cannot instantiate it
Shape sh = new Interfacedemo();
Multiple inheritance can be achieved via Interface.
One class can implement multiple interface
Implements intA, intB;
Class A extends Class B implements IntA, intB
--------
Abstraction:
Abstract + non abstract methods
Abstract Class A
Abstract void Method A
----------
Wrapper class
Integer Integer.parseint()
Character Character.parsechar()
Double Double.parseDouble()
Boolean Boolean.parseBool()
int a=10
String var = String.ValueOf(a)
-----------
Access Specifier
Access specifier is Default by default
Protected can be accessed using Inheritance only ( Extends)
-------------
Exception
1) Checked exception
2) Unchecked exception: filenotfound
Try{}
Catch(Exception e)
{
SOP (e.getMessage());
}
Finally() - Executes everytime
Throws Exception name: Handling exception at method level
--------------
Convertors
toLowerCase()
toUpperCase()
toString()
--------
Switch (day=1) //condition
{
case 1: "Sunday"; break;
case 2: "Monday"; break;
default: Otherday; break; return;(to come out)
}
-----------Naming convention-----
Package name lowercase
Class name uppercase
Method lowercase
Variable lowercase
No comments:
Post a Comment