Core Java
Core Java
Eclipse software
Features Description
Simple Easy like C++, no concept of pointers.
Object Oriented Object, class, inheritance, polymorphism, abstraction.
Portable Works Byte code anywhere..
Platform independent
Secured No concept of pointers, Runs on virtual Machine, class loader.
Robust Exception handling
Architecture neutral Fixed things in java.
Interpreted
High Performance Faster than other interpreted programming languages.
Multi threaded Parallel Execution
Distributed Facilitates users to create distributed applications in Java.
Dynamic Memory
Java is platform independent
Released in 1995.
Version 1.0 to 1.8(2014) Features almost same.
Version 9 in 2017.
Version 17 released in September 2017.
Developing software in Java
Platform Description
Java SE: Standard Edition Basic/Core
Java EE: Enterprise Edition Web, Servlet, Web Services
Java ME: Micro Edition Mobile Application
Java FX Internet Application
All Java platforms consist of a Java Virtual Machine (VM) and an application
programming interface (API). The Java Virtual Machine is a program, for a particular
hardware and software platform, that runs Java technology applications. An API is a
collection of software components that you can use to create other software components
or applications. Each Java platform provides a virtual machine and an API, and this
allows applications written for that platform to run on any compatible system with all the
advantages of the Java programming language: platform-independence, power, stability,
ease-of-development, and security.
JRE Vs JDK Vs JVM
JRE Vs JDK Vs JVM
JRE Vs JDK Vs JVM
ium=variable
64 reserved keyword in java
8 primitive data types in java
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
}
}
Semicolon marks end of the statement.
Datatypes in java:
package capgemini;
public class datatypes {
}
}
if….else statement
package capgemini;
public class datatypes {
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=1;
while(num<=10)
{
System.out.println(num*2);
num++;
}
}
}
Print table of 2 in reverse manner:
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=10;
while(num>=1)
{
System.out.println(num*2);
num--;
}
}
}
break statement-breaks the loop
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=1;
while(num<=10)
{
if(num%5==0)
{
System.out.println("Multiple of 5");
break;
}
num++;
}
System.out.println("congratulations multiple of 5 found");
}
}
do-while loop
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num=1;
do
{
System.out.println(num);
num++;
}
while(num<=10);
}
}
It atleast runs once even if the condition is not satisfied.
Switch case
package capgemini;
public class datatypes {
public static void main(String[] args) {
// TODO Auto-generated method stub
int day=3;String today;
switch(day) {
case 0: today="Sunday"; break;
case 1: today="Monday"; break;
case 2: today="Tuesday"; break;
case 3: today="Wednesday"; break;
case 4: today="Thursday"; break;
case 5: today="Friday"; break;
case 6: today="Saturday"; break;
default: today="Invalid";
}
System.out.println(today);
}
}
Comments and Identation
System.out.print("Hello");
System.out.println("My name is");
System.out.print("Ketaki");
}
}
Debugging
package capgemini;
}
}
Method/Functions-II
package capgemini;
}
}
Method/Functions-III
package capgemini;
}
}
Class
It is a method and has same name as of class with no return type.
It is used to initialize variables of the class.
student(int r, String n, double m, char g)
{
rollno=r;
name=n;
marks=m;
grade=g;
}
public class datatypes {
public static void main(String[] args) {
student s1 = new student(10,"Ketaki",96.75,'A');
Constructor in java:
package capgemini;
class student {
int rollno;
String name;
double marks;
char grade;
student(int r, String n, double m, char g){
rollno=r;
name=n;
marks=m;
grade=g;
}}
public class datatypes {
public static void main(String[] args) {
student s1 = new student(10,"Ketaki",96.75,'A');
System.out.println(s1.rollno);
System.out.println(s1.name);
System.out.println(s1.marks);
System.out.println(s1.grade);
}}
Constructor overloading in java:
package capgemini;
class student {
int rollno;
String name;
double marks;
char grade;
student(int r, String n, double m, char g)
{
rollno=r;
name=n;
marks=m;
grade=g;
}
student(int r, String n, double m)
{
rollno=r;
name=n;
marks=m;
grade='B';
}
Constructor overloading in java cntd:
student(double m, char g){
rollno=51;
name="Nobody";
marks=m;
grade=g;
}
void print() {
System.out.println(rollno+" "+name+" "+marks+" "+grade);
}
}
public class datatypes {
public static void main(String[] args) {
student s1 = new student(10,"Ketaki",96.75,'A');
student s2 = new student(10,"Ketaki",96.75);
student s3 = new student(96.75,'A');
s1.print();
s2.print();
s3.print();
}
}
Memory allocation is java
Suppose there are 500 students in my college, now all instance data
members will get memory each time when the object is created. All
students have its unique rollno and name, so instance data member is
good in such case. Here, "college" refers to the common property of all
objects. If we make it static, this field will get the memory only once.
static:
package capgemini;
class student {
static void print(){
System.out.println("Hello");
}
}
public class datatypes {
static{
System.out.println("Hello there I'm static");
}
static void prints()
{
System.out.println("Hello flocks");
}
public static void main(String[] args) {
prints();
student.print();
}
}
final
Here print().void-parent is
not present as child is over
riding parent method.
This is overriding.
The method must have the
same name as in the parent
class.
The method must have the
same parameter as in the
parent class.
Static and dynamic Polymorphism
The super keyword in Java is
a reference variable which is
used to refer immediate
parent class object.
Encapsulation
static: You can make a method static by using the keyword static. We should call the main() method
without creating an object. Static methods are the method which invokes without creating the
objects, so we do not need any object to call the main() method.
void: In Java, every method has the return type. Void keyword acknowledges the compiler that main()
method does not return any value.
main(): It is a default signature which is predefined in the JVM. It is called by JVM to execute a
program line by line and end the execution after completion of this method. We can also overload the
main() method.
String args[]: The main() method also accepts some data from the user. It accepts a group of strings,
which is called a string array. It is used to hold the command line arguments in the form of string
values.
Array
package capgemini;
package capgemini;
public class datatypes {
public static void main(String[] args) {
int[] arr1= {10,20,30,40,50,60};
int arr2[]= {10,20,30,40,50,60};
int[] arr3= new int[5];
arr3[0]=100;arr3[1]=200;arr3[2]=300;arr3[3]=400;arr3[4]=500;
System.out.println("Array1:");
for(int i=0;i<6;i++) {
System.out.println(arr1[i]);}
System.out.println("Array2:");
for(int i=0;i<6;i++) {
System.out.println(arr2[i]);}
System.out.println("Array3:");
for(int i=0;i<5;i++) {
System.out.println(arr3[i]);
}}}
Printing reverse array
package capgemini;
public class datatypes {
public static void main(String[] args) {
int[] arr1= {10,20,30,40,50,60};
int arr2[]= {10,20,30,40,50,60};
int[] arr3= new int[5];
arr3[0]=100;
arr3[1]=200;
arr3[2]=300;
arr3[3]=400;
arr3[4]=500;
System.out.println("Array1 Reverse:");
for(int i=5;i>=0;i--) {
System.out.println(arr1[i]);
}}}
Enhanced for loop
for(int num:arr1)
Initialization, condition and increment or decrement are
done internally.
This will only print array from start to end i.e arr[0] to
arr[n-1] fro n-length array.
No other condition can be given fort his loop like reverse,
a[1] to a[7] etc.
Enhanced for loop:
package capgemini;
package capgemini;
public class datatypes
{
public static void main(String[] args) {
String[] arr1 = {"Sunday","Monday","Tuesday"};
System.out.println("Array1");
for(String num :arr1)
{
System.out.println(num);
}
}
}
Bubble sort:
package capgemini;
public class datatypes {
public static void main(String[] args) {
int[] arr1 = {50,54,698,48,1,55,5 };
int temp = 0;
for (int i = 0; i <= arr1.length-1; i++) {
for (int j = 0; j <= arr1.length-i-2; j++) {
if (arr1[j] > arr1[j + 1]) {
temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
System.out.println("Bubble Sort:");
for (int i = 0; i < 6; i++) {
System.out.println(arr1[i]);
}
}}
Two Dimensional Array [rows][cols]
Two Dimensional Array
package capgemini;
public class datatypes {
public static void main(String[] args)
{
int[][] arr1 = {{10,20,30},{30,40,40},{50,70,60}};
for (int i = 0;i <= 2;i++)
{
for (int j= 0; j<= 2; j++)
{
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
}
}
Addition of two matrices:
package capgemini;
}
System.out.println();
}
}
}
Star pattern:
package capgemini;
}
System.out.println();
}
}
}
Star pattern:
package capgemini;
}
System.out.println();
}
}
}
Star pattern:
package capgemini;
}
System.out.println();
}
}
}
Star pattern:
package capgemini;
System.out.println();
}
}
}
Star pattern:
package capgemini;
package capgemini;
public class datatypes {
public static void main(String[] args) {
String s="Selenium123";
String str1=" Selenium ";
String str="Selenium-Java-training";
System.out.println(s.charAt(0));
System.out.println(s.concat("456"));
System.out.println(s.indexOf('e'));
System.out.println(s.replace('e', 'E'));
System.out.println(s.length());
System.out.println(s.substring(2));
System.out.println(s.toLowerCase());
System.out.println(s.toCharArray()[0]);
System.out.println(str.split("-")[1]);
System.out.println(str1.trim());
}}
ASCII Code
Unboxing
Type casting
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(integer1);
}
}
Scanner class
}
String Handling
String literal vs String Object
package capgemini;
public class scanner_class {
package capgemini;
public class scanner_class {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder sb=new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
System.out.println(sb.reverse());
}
}
Count number of words in String:
package capgemini;
public class count_words {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="Today is Tuesday";
int spaces=0;
for(int i=0;i<s.length();i++) //Method 1
{
if(s.charAt(i)==' ') {
spaces++;
} }
System.out.println(spaces+1);
String words[]=s.split(" "); //Method 2
for(int j=0;j<words.length;j++)
{
System.out.println(words[j]);
}
System.out.println(words.length);
}
}
Add digits present in String:
package capgemini;
public class count_words {
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date
Date and time in Java:
package capgemini;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class date_time {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Date date=new Date();
Date date1=new Date();
Date date2=new Date();
SimpleDateFormat SDF=new SimpleDateFormat("dd-MM-yyyy");
date=SDF.parse("22-11-2021");
System.out.println(date);
SimpleDateFormat STF=new SimpleDateFormat("HH:mm:SS");
date1=STF.parse("23:43:45");
System.out.println(date1);
SimpleDateFormat SDTF=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:SS'Z'");
date2=SDTF.parse("22-11-2021T23:43:45Z");
System.out.println(date2);
}}
InstanceOf
This keyword checks whether object is an instance of the specified type (class
or subclass or interface).
package capgemini;
class Student{
}
class Child extends Student{
}
public class scanner_class {
public static void main(String[] args){
// TODO Auto-generated method stub
Child s1=new Child();
System.out.println(s1 instanceof Object);
}
}
@overiding-Just an annotation
package capgemini;
class Student{
void print(){System.out.println(“Studnet");}}
class Child extends Student{
@Override
void print(){System.out.println("Child");}}
public class scanner_class {
public static void main(String[] args){
// TODO Auto-generated method stub
Child s1=new Child();
s1.print();
}}
Abstract Class
It is a class
It cannot be instantiated ie you cannot create an object of
abstract class
Abstract is a keyword hence can be used with a method
also
A method is abstract if it has no body to it
Abstract class could have both abstract and non-abstract
methods
Class inheriting abstract class must provide body to the
abstract method
Interface
Interface is a keyword
Interface is not a class
Interface only have abstract and public methods and
variables are final here
Class implements an interface
Interface helps to achieve multiple inheritance in Java
Interface cannot be instantiated
Interface achieves 100% abstraction
Interface tells you what to do, and not how to do
Exception handling
Try
Catch
Multiple catch
Finally
Throw
Throws
Try
package capgemini;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
package capgemini;
public class solve
{
static void add(char b,int... a)
{
int sum = 0;
for (int num : a)
{
sum = sum + num;
}
System.out.println(sum);
}
package capgemini;
import java.util.Arrays;
public class vararr {
}
Collection framework
package capgemini;
import java.util.*;
}
Array list:
package capgemini;
import java.util.*;
class student{
int rollno;
String name;}
public class studentinfo {
public static void main(String[] args) {
student s1=new student();
s1.rollno=1;s1.name="A";
student s2=new student();
s2.rollno=2;s2.name="B";
student s3=new student();
s3.rollno=3;s3.name="C";
List<student> num=new ArrayList<student>();
num.add(s1);num.add(s2);num.add(s3);
for(student i:num)
System.out.println(i.rollno+" "+i.name);
}
}
Set and Map
}
I/O Stream
I/O Stream Types
Buffered Stream(Reader)
package capgemini;
import java.io.*;
public class readfile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("C:\\Users\\Comp\\Desktop\\test1.txt");
BufferedReader br=new BufferedReader(new FileReader(file));
String st;
while((st=br.readLine())!=null)
{
System.out.println(st);
}
br.close();
}
}
Buffered Stream(Writer)
package capgemini;
import java.io.*;
public class writefile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("C:\\Users\\Comp\\Desktop\\test1.txt");
BufferedWriter wr=new BufferedWriter(new FileWriter(file));
wr.write("Hello Ketaki");
wr.close();
}
}
File handling
Junit/Testng
package testcases;
import org.junit.Test;
public class MobileTest {
@Test
public void test1()
{
System.out.println("Test1");
}
@Test
public void test2()
{
System.out.println("Test2");
}
}
Map eg:
package testcases;
import org.junit.Test;
@Test
public void test1()
{
System.out.println("Test1");
}
@Test
public void test2()
{
int i=10/0;
}
}
Map eg:
package testcases;
import org.junit.Assert;
import org.junit.Test;
@Test
public void test1()
{
System.out.println("Test1");
}
@Test
public void test2()
{
Assert.assertFalse(false);
Assert.assertEquals(0,0);
}
}