Core Java
Language: It is used for communication between two people.
Programming language: It is used for communication between human and system.
Communicating with system means giving commands using software i.e using software. And to develop
that software we required programming language.
1. Need of java:
Using C language if we develop a software that software we can run on same o.s because C language is
platform dependent.
So if we have to develop platform independent software we have to use java.
First Java Program:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello");
Output: Hello
2. What is class and Object?
Class:-
Class is a keyword which is used to develop user defined data type.
Or class is a user defined data type.
Example:-
public class Student {
int rollNo;
String name;
}
Object:-
Object is a physical existence of a class or it is instance of a class.
3. Non-Static variable:-
The variable declared at class level and without static keyword is called as non static variable.
Nonstatic variable gets memory on object creation. When we create object of a class , JVM allocates
memory for it’s non static variables.
To access nonstatic variable we required object. Without object nonstatic variable can not be exist.
Ex.
public class Student {
int rollNo;
String name;
public static void main(String[] args) {
// Storing first student info
Student s = new Student();//object creation statement
// storing value
s.rollNo = 101;
s.name = "sam";
// storing second student info
Student s1 = new Student();
s1.rollNo = 102;
s1.name = "ram";
// retriving data
System.out.println(s.rollNo+" "+s.name);
System.out.println(s1.rollNo+" "+s1.name);
}
Output:
101 sam
102 ram
4. Static Variable:-
The variable which is declared at class level with static keyword called as static variable.
Static variable will get memory after class loading and before main method invocation.
Static variables are available throughout the class and accessible to all objects of that class.
public class Student {
String student_name;
static String college_name = "JSPM";
public static void main(String[] args) {
Student s = new Student();
s.student_name = "sam";
Student s1 = new Student();
s1.student_name = "ram";
System.out.println(s.student_name+" "+s.college_name);
System.out.println(s1.student_name+" "+s1.college_name);
Output:
Sam JSPM
Ram JSPM
5. Local variable:-
The variable which is declared inside a method or block is called as local variable.
Local variable will get memory when we call that method and this memory will get deallocate once
method execution complete. I.e lifetime of local variable is from method call to method execution.
class Demo {
public static void m1(){
int m = 10;
System.out.println(m);//10
System.out.println(n); //-- 2
public static void main(String[] args) {
int n = 20;
m1();
System.out.println(n);//20
System.out.println(m);// -- 1
6. Types of method:
Parameterized and non parameterized method.
non parameterized:-
public class Demo {
public static void add() {
int c = 10 + 20;
System.out.println(c);
public static void main(String[] args) {
System.out.println("main method");
add();
}
}
Parameterized :-
public class Demo {
public static void add(int m, int n) {
int c = m + n;
System.out.println(c);
public static void main(String[] args) {
System.out.println("main method");
add(10,20);
Void and non void method:-
If method has return type void then we cannot return any value from it. We cannot return any value but
we can write return statement.
If method return type is non void then it returns respective value i.e if return type is float then we can
written float and its compatible values i.e byte, short, int, long etc.
public class Demo {
public static void m1() {
System.out.println("void method");
public static void main(String[] args) {
m1();
}
public class Demo {
public static void m1() {
System.out.println("void method");
return;
public static void main(String[] args) {
m1();
Output?
class Test {
public static float m1(){
return 10.5f;
public static void main(String[] args) {
int a = m1();// statement 1
Remove error at statement 1
Output?
class Test {
int a = 10;
public static void m1(Test t1){
System.out.println(t1.a);
t1.a = 200;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.a);
m1(t);
System.out.println(t.a);
7. Types of Access Specifiers:-
In java we have four Access Specifiers and they are listed below.
1. Public
2. Private
3. Protected
4. Default (no specifiers)
8. Data Hiding:
Hiding internal confidential data from outsiders and provides only when he provides proper
authentication is called as data hiding.
In java we achieves data hiding using private keyword.
9. Abstraction:
Hiding internal implementation from end user and provides only services is called abstraction.
Note: In data hiding we hides data and in abstraction we hides internal coding or implementation.
10. Encapsulation:
Encapsulation is a process of binding variable and methods into single unit.
Class is a best example of encapsulation.