Core Java smart theory

Data types in java

Integer

  1. int 
  2. long

Decimal

  1. double

String

  1. String

Boolean

  1. boolean
 

variable in java

Syntax

data_type variable_name;

example

int age;
double salary;
String name;
boolean isLogin;

variable initialisation

age=20;
salary=12000.90;
name="capscom technology";
isLogin=true;

printing variable in java

System.out.println(age);
System.out.println("Salary:"+salary);
System.out.println("Name:"+name);
System.out.println("is user login:"+isLogin);

constant in java

final keyword is used to create a constant in java

statements in java

  1. if
  2. if else
  3. if else if 
  4. nested if 
  5. switch statement 
  6. conditional statement 

Loop in java

  1. for loop 
  2. while loop
  3. do while loop 
  4. nested loop 
  5. variable: array
 
class Capscom{

public static void main(String[] args) {

int a[]={10,20,30,40,50};

for(int i : a){ 

// where a is an array and i contain single item of an array

System.out.println(i);

}
}
}


Array in java

SDA (Single dimension array)

data_type array_name[]=new data_type[size];

or 

data_type array_name[]={n1, n2, ....}

MDA (Multi dimension array)

data_type array_name[][]=new data_type[row][col];

or 

data_type array_name[][]={

{n00, n01, ...},

{n10, n11, ...},

{......}

};

Methods in java 

A function declared within a class is called a method.

Syntax

data_type function_name(){

}

Example 

void msg(){

System.out.println("Welcome");

}

class MethodExample{

public static void main(String[] args) {

msg();
/*
will through an error says msg is non static method and calling from static context,
to call msg() method from here either we have to create an object of current class or make msg() method a static method.
*/
}

void msg(){
System.out.println("Heyy.");
}
}

Method calling using class object 

class MethodExample{
public static void main(String[] args) {
MethodExample obj=new MethodExample();
obj.msg();
}
void msg(){
System.out.print("Heyy.");
}
}

Method calling without using object or by creating static method.

class MethodExample{
public static void main(String[] args) {
msg();
}
static void msg(){
System.out.print("Heyy.");
}
}

return statement in method 

class MethodExample{

public static void main(String[] args) {
double result;
result=obj.sum(100, 200);
System.out.println("Sum:"+result);
}

static double sum(double x, double y){
return x+y;
}
}


Type casting in java

class CapscomExample{
	public static void main(String[] args) {
		// implicit type casting
		int x,y;
		double z;
		x=100;
		y=200;
		z=x+y;
		System.out.println("Sum:"+z);

		// explicit type casting
		double a,b;
		int c;
		a=200.45;
		b=45.9;
		c=(int)a-(int)b;
		System.out.println("Subtraction:"+c);

	}
}

Method overloading in java

If multiple methods have same with different parameters is called method overloading in java
int add(int x, int y){
return x+y;
}

double add(double x, double y){
return x+y;
}

Java constructors

A function declared within a class with the same name as class name and does not have a return type and call automatically when the class object created is called constructor in java.
class CapscomExample{
// we are creating a method with a name as class name without a return type
CapscomExample(){
System.out.println("Hello, this is constructor method.");
}

public static void main(String[] args) {
CapscomExample obj=new CapscomExample();
// constructor called automatically
}
}

uses of constructor 

To initialize the object


Exception Handling in java

try-catch block

To execute a java code, an error may arise and the rest of the java program terminated automatically, to keep the java program in running mode when an error arises, exception handling is used.

these errors may arise due to wrong input, source not found extras.
class error_handling{
	public static void main(String[] args) {
		try{
			int a,b,c;
			a=100;
			b=0;
			c=a/b;
			System.out.println("Divide: "+c);
		}
		catch(Exception e){
			System.out.println("Something wrong");
		}		
	}
}

finally block

finally block allow us to execute code or statement either error may arise or not

class error_handling{
	public static void main(String[] args) {
		try{
			int a,b,c;
			a=100;
			b=0;
			c=a/b;
			System.out.println("Divide: "+c);
		}
		catch(Exception e){
			System.out.println("Something wrong");
		}		
		finally{
			System.out.println("This statement will run always.");
		}
	}
}

Access modifiers in java 

public - code accessible from anywhere
class parent{
int parent_amount=12000;
}

class child extends parent{
public static void main(String[] args) {
child c=new child();
System.out.println("Parent amount:"+c.parent_amount);
}
}

private - code accessible within a class only
class parent{
private int parent_amount=12000;
}
class child extends parent{
public static void main(String[] args) {
child c=new child();
System.out.println("Parent amount:"+c.parent_amount);
}
}
protected - code accessible within a package and sub class only.
class parent{
	protected int parent_amount=12000;
}

class child extends parent{
	public static void main(String[] args) {
		child c=new child();
		System.out.println("Parent amount:"+c.parent_amount);
	}
}

Non access modifiers

final

when a class declared with a final, it can not be inherit.

final class parent{
	protected int parent_amount=12000;
}

// unable to inherit parent class
class child extends parent{
	public static void main(String[] args) {
		child c=new child();
		System.out.println("Parent amount:"+c.parent_amount);
	}
}

abstract 

when a class declared with an abstract keyword, we can not create object of that class.
class parent{
	protected int parent_amount=12000;
}

abstract class child extends parent{
	public static void main(String[] args) {
		// unable to create an object of child class that is declared with abstract keyword
		child c=new child();
		System.out.println("Parent amount:"+c.parent_amount);
	}
}

Inheritance in java 

  1. simple Inheritance
  2. multilevel Inheritance
  3. hierarchical Inheritance
  4. multiple Inheritance(not supported by class, but supported using interface)


Java polymorphism


Poly means many and morph means form. "many form"
When a super class inherited with a method and subclass contain method with the same name is called polymorphism in java.
class parent{
	void msg(){
		System.out.println("Msg method in parent class");
	}
}

class child extends parent{
	void msg(){
		// accessing same method from super class
		super.msg();
		System.out.println("Msg method in child class");
	}
	public static void main(String[] args) {
		child c=new child();
		c.msg();
	}
}

Encapsulation in java 


Encapsulation means hide all sensitive data from end users and to achieve encapsulation we declare all properties with private access modifiers and provide get and set methods to access and update properties.
class CapscomExample{
	private int age=20;
	public int getAge(){
		return age;
	}	
	public void setAge(int x){
		this.age=x;
	}
}

class child extends CapscomExample{
	public static void main(String[] args) {
		CapscomExample c=new CapscomExample();
		System.out.println("Age:"+c.getAge());
		c.setAge(100);
		System.out.println("Age:"+c.getAge());
	}
}

Abstraction in java 


To create security in java, Abstraction is used, it means hiding all sensitive data from user and showing some essential information only.
Note:
  1. Abstract class can not be initiated.
  2. Abstract class can contain abstract, non-abstract method.
  3. Abstract method can not have body.

Interface 

Interface is used to achieve 100% abstraction in java.
Note:
  1. Like abstraction interface also not used to create an object.
  2. interface can not be extends only implements with a class always.
  3. interface contain only abstract methods.
  4. if we are implemeting interface in a class, it is mandatory to override all the abstract methods of a interface.
  5. All properties of interface are final, static by default.

Enumeration in java(Enum)

it is a class or group of constant, these values are also unchangeable.
Note:
  1. enum values must be in uppercase.
  2. enum constants are public, static and final.
  3. it can be used in a project or program when values are not changing in whole program.
enum Colors{
	RED,
	GREEN,
	YELLOW,
	BLUE
}

Example
enum Colors{
	RED,
	GREEN,
	YELLOW,
	BLUE
}

class child{
	public static void main(String[] args) {
		// access enum values using dot syntax		
		System.out.println(Colors.RED);
	}
}

Wrapper class

wrapper class provide a way to use primitive data type as an object.
class CapscomExample{
	public static void main(String[] args) {
		Integer x=100;
		System.out.println(x); 
		// 100
		System.out.println(x.toString().length());
		// 3(Number of characters)
	}
}

Nested class or inner class

Class within a class is called nested class.

class outer{
	String name="outer class";
	class inner{
		String name="inner class";
	}
}

class test{
	public static void main(String[] args) {
		outer outer_object=new outer();
		outer.inner inner_object=outer_object.new inner();
		System.out.println(inner_object.name);
	}
}

Create an object of inner class

outer outer_object=new outer();
outer.inner inner_object=outer_object.new inner();

Access properties of inner class

System.out.println(inner_object.name);

Math methods

sqrt(arg) Return square root
pow(base, index) Return power 
cbrt(arg) Return cube root
sin(arg) Return sin
cos(arg) Return cosine
tan(arg) Return tangent
exp(arg) Return exponential
ceil(arg) Round up to nearest integer
floor(arg) Roud down to nearest integer
max(x, y) Return max value
min(x, y) Return min value
round(x) Normal round
toDegrees(x) Convert radian value into degree
toRadians(x) Convert degree value into radian
log(x) Return natural log
log10(x) Return the base 10 logarithm of x

String methods

contains() Check sub string is in a string
length() Return length of a string
toString() Convert into a string
toLowerCase() Convert into lowercase
toUpperCase() Convert into uppercase
trim() Remove white space from left, right side of a string
substring() Return substring of a string
startsWith() Check string starts with specified character
replace() Replace part of string with given string
isEmpty() Check string is empty or not
indexOf() Return index of specified character
equals() Compare two string, return true if strings are equal otherwise false
equalsIgnoreCase() Compare two string by ignoring cases
endsWith() Check string is ending with specified character
concat() Concat two string
charAt() Return character of specified index

Java ArrayList class

ArrayList class is a resizable Array, where we can add, remove items dynamically.

Package:

import java.util.ArrayList;

Create ArrayList Object

ArrayList<String> persons=new ArrayList<String>();
add(item) Add new item
set(index, value) Modify item of specified index
get(index) Get item of specified index
remove(index) Remove item from specified index
clear() Remove all items
size() Return size

Java LinkedList class

It is same as ArrayList class, only difference is that it store every item in a container and every container linked with each other

Package:

import java.util.LinkedList;

Create LinkedList Object

LinkedList<String> persons=new LinkedList<String>();
addFirst(item) Add item at first
addLast(item) Add item at last
getFirst() Return first item
getLast() Return last item
removeFirst() Remove first item
removeLast() Remove last item

Java HashMap class

Java HashMap store items in pair of key/value, where type of key and value any type.

Package:

import java.util.HashMap;

Create HashMap Object

HashMap<String, String> persons=new HashMap<String, String>();
put(key, value) Add item with key and value
get(key) Get item of specified key
remove(key) Remove item of specified key
clear() Remove all items
size() Return size of collection
keySet() Return array of keys
values() Return array of values

Java HashSet class

Java HashSet class store unique items.

Package:

import java.util.HashSet;

Create HashSet Object

HashSet<String> persons=new HashSet<String>();
add(item) Add item
contains(item) Check item exist or not
remove(item) Remove specified item
clear() Remove all items
size() Return size of collection

 

© 2016 - 2022, All Rights are Reserved.