Core Java Notes
Core Java Notes
Define Class?
Ans: It is a structures layout used for generating Objects. OR A class is a collection of
variables, methods and main().
Define Objects?
Ans: Object is memory related to class, which holds the component of class.
2
JVM ARCHITECTURE
3
b) Run-time data area:
This Run-time data area is internally divided into the following blocks:
i) Method area: the memory block where the class code is loaded is known as
method area.
Note:
While class loading the static programming components will get the memory within
the class.
In this process main will get the memory within the class.
Once main got the memory within the class, then it is automatically copied into the
stack area to start the execution.
Once main method copied into the stack area then execution control will come to
java stack area to check the main format as “public static void main (String args
[])”
If the format is available then the execution starts.
ii) Heap area: the memory block where the objects are created is know as heap area.
Note: while object creation, the non-static members of the class will get memory
within the object.
iii) Java stack area: The memory block where main is copied to start the execution.
In the execution process the main will call remaining methods for execution.
Note: This is area where all methods are executed.
“.” In java refers to or belongs to same class, package etc.
iv) PC register area (Program- counter Register Area):
This will record the states of method execution in java stack area.
Every method executing in java stack area will have its own PC register.
All these PC registers are opened in separate memory block known as PC
register area.
v) Native method area:
The method a from JavaLib which are using native keyword are known as
native methods.
Eg: public final native void wait()
Public final native void notify()
While these native methods are used in program construction then these
methods are separated & loaded onto separate memory block known as
native method because these are constructed using C/C++ code.
c) Executing engine:
Execution engine internally contains two translators (interpreter and JIT
compiler).
4
The execution control will come to java stack area starts the execution with
main-method.
The execution process will be started by interpreter (this interpreter will
execute line by line) and executes normal instructions.
When interpreter finds stream instructions then the execution engine transfer
to JIT compiler.
This JIT compiler will execute stream instructions (bulk instruction).
Java language is interpreter language because execution control internally
uses interpreter.
Why do we use interpreter in execution?
Ans: When we use interpreter in execution process then the application can accept request in the
middle of execution, which is preferrable in internet application development.
Define Environment variable?
Ans: Environment variable are parts of Operating system. The variables which are part of
operating system which control and manages the resources of computer system. These
environment variables are categorized into two types
a) User variables: The variables which can be used by the individual users.
b) System variables: These system variables can be used by all the users in the computer
system.
Note: According to java language the variables path of user variables & system variables
can be two types
a) Path: the value in “path” is used by javac and java commands prompt.
b) Class path: The value in class path is used by application class loader part of JVM.
Simple structure of Java programs
In java language all the programming components are binded within the “class” because of this
because of this reason we cannot write a program without class in java.
public class Class_name {
//variable
// methods
public static void main(String args[]) {
//set of instructions
}
}
i) Public: makes the main access outside the class
ii) Static: makes the main available within the class or the main get the memory within
the class.
5
iii) void: makes the main “non-return type” method. (Which does not return any value
after execution)
iv) main() must be passed with String array as parameter because which is ready to
accept any type of data value.
Define Javalib?
Ans: Java library is represented using word “java” and is a collection of variables and methods.
Package are collection of classes and interfaces
Classes and interfaces are collections of variables and methods.
CLASSES AND
JAVA PACKAGES VARIABLES &
INTERFACES
METHODS
Define instruction?
The line of the program which specifies the action to be performed is known as instruction.
These instructions are part of program must be terminated suing ‘;’.
Note: In the process of constructing instruction in java program we use import statement.
Syntax: import java. package-cname.method-name();
Define print method?
i) print method is a build in method, which is used to display messages or result or
combination of both.
ii) Print method is accessed using system class. This system class internally having 3
streams (connections). i.e., in-input, out-output, err-error.
iii) System class belongs to “lang” package
iv) “lang” package belongs to java library.
v) While program coding this “import” statement is divided onto 2 parts
a) Part-1: java library & package (this part must be declared above the class)
import java.lang.*;
b) Part-2: Classname and method name. (this part must be declared within the class)
System.out.print("msg");
Example program:
import java.lang.*;
class Welcome {
public static void main(String[] args) {
System.out.print("Welcome to java");} }
6
Writing, saving, compiling and executing java program
Step-1: Create folder part of any drive.
Step-2: Open any text editor and type the program before typing browse the folder.
Step-3: After typing, save the file with .java extension.
Step-4: Go to destination folder and type cmd in address path & press enter.
Step-5: Compile the program as javac class-name.java.
Step-6: Execute the program as command java class-name.
Example program-2:
Write a program to add tow number and display the result?
import java.lang.*;
class ADD {
int a, b;
void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ADD ob=new ADD();
ob.a=1;
ob.b=2;
ob.add();
}
}
7
non-static variables are categorised into two types.
a) Instance variables: The non-static variables which are declared outside the
methods are known as instance variables. This instance variables will get the memory
within the object while object creation.
Memory location: This instance variable will get the memory within
the object while object creation.
Scope & visibility: these instance variables are accesses by the
methods of same objects.
Lifetime: These instance variables are available until object is available
b) Local variables: The non-static variables which are declared inside the methods
are known as local variables.
Memory location: These local variables will get memory within the
method or method frame, while method execution.
Scope & visibility: These local variables are accessed only within the
method or method frame.
Lifetime: these local variables are available until method is available.
Note:
a) Define Method frame?
Ans: The partition of java stack area where the method is executed is known as Method
frame.
Behaviour of method frame: After method execution completed the method frames
will be destroyed automatically from java stack area.
b) Define object?
Ans: Object is a memory created as part of heap area related to a class holding non-static
members of the class.
c) The instance variables which are not initiated with any value, while memory generation
are assigned with default values.
8
Ex: BufferReader, InputStreamReader
iii) Variables and methods: In variables and methods, the first word must be in lower
case and from second word onwards the starting letter must be capital.
Ex: getSalary(), toUpperCase(), rollNo, empId.
b) Float datatypes:
float 4 bytes
double 8 bytes
9
These non-primitive datatypes will generate object storage.
Define Object Storage?
Ans: The memory which is generated part of java program to hold “group non-static
members of the class” is known as object storage.
Based on javaLang the variables of primitive data types will hold values and non-
primitive datatypes will hold reference.
Class:
Class is a structural layout generating objects.
Class is loaded on to method area of jvm.
Class is a collection of variables, methods and main
Variables will hold the data
Method will give actions
Main will start the program
Class which is holds main is main class.
Class which does not hold main is classed sub-class (holds variables and methods).
The application development we have only one main class but we can have any
number of subclasses.
Classes are divided into two types
User Defined classes: the classes which are defined by the programmer
Build-in classes: The classes which are already existing with the JavaLib are
known as building classes.
Ex: Scanner Class
Scanner class:
It is used to read the data into java program by considering the console input
keyboard.
Scanner class is available from JavaLib.
The following are some non-static methods from scanner class.
Public byte nextByte();
Public short nextShort();
Public int nextInt();
Public float nextFloat();
Public double nextDouble();
Public boolean nextBoolean();
10
Public long nextLong();
Public String nextLine();
Note:
All these methods are return type non-static methods & will get the memory
within the Scanner class object & accessed with the object reference variable
(object name).
Scanner class is used in the java program when we want to read the data from data
input.
Syntax of object creation for Scanner Class:
Scanner s= new Scanner (System,.in);
Operators in java:
The following are some operators in java
Relational operators
Arithmetic operators
Operator Meaning
Operator Meaning
> Greater than
+ Addition >= Greater than or
- Subtraction equal
< Less than
* Multiplication
<= Less than or equal
/ Division == Is equal
% Modulus division != Not equal
Logical operators
Operator Meaning Incremental/ decremental operators
&& Logical AND Operator Meaning
|| Logical OR ++ Increment
! Logical NOT -- Decrement
11
simple if
if else
nested if
ladder if else
switch case
b) Iterative statements: The statements which are used to repeat the set of lines if the
program on some conditions is known as Iterative statement.
Iterative statement types
while loop
do while loop
for loop
c) Branching statement: The statement which are used to transfer the control from one
location to another location of the program.
Branching statement types
break
continue
return
exit
Note: there is not concept of goto function in java.
Methods:
Methods are the actions performed on the variable.
According to java methods are categorised into two types
i) Instance methods:
The methods which are declared without static keywords are known as
instance methods or non-static methods.
Instance methods will get memory within object whether it may be
variable or method.
These instance methods will get the memory within the object, while
object creation and accessed with the object names.
These instance methods are categorized into 2 types:
a) Build-in instance methods: The instance methods which are already
existing within the java library are known as build-in instance
methods.
Note: we use javap command to view all the building methods from
the class or interface.
12
b) User-defined instance methods: The instance methods which are
defined by programmer are know as user-defined instance methods.
Syntax:
return-type method-name(parameter-list) {
//body}
13