Core Java Notes
About the Java Technology
Java technology is both a programming language and a platform.
The Java Programming Language
The Java programming language is a high-level language that can be characterized by all of the following buzzwords:
- Simple
- Object oriented
- Distributed
- Multithreaded
- Dynamic
- Architecture neutral
- Portable
- High performance
- Robust
- Secure
|
|
The Java Platform
The Java platform has two components:
- The Java Virtual Machine
- The Java Application Programming Interface (API)
How Will Java Technology Change My Life?
- Get started quickly:
- Write less code:
- Write better code:
- Develop programs more quickly:
- Avoid platform dependencies:
- Write once, run anywhere:
- Distribute software more easily:
Object
Any entity that has state and behavior is known as an object.
Class
It is a logical entity.
Polymorphism
When one task is performed by different ways
Abstraction
Hiding internal details and showing functionality is known as abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
Object in Java
An entity that has state and behavior is known as an object .
An object has three characteristics:
- state: represents data (value) of an object.
- behavior: represents the behavior (functionality) of an object .
- identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
static keyword
The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class
- class Student8{
- int rollno;
- String name;
- static String college ="ITS";
- }
- Student8 s1 = new Student8(111,"Karan");
- Student8 s2 = new Student8(222,"Aryan");
Exception:
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
The object, called an exception object, contains information about the error, including its type and the state of the program, when the error occurred. Creating an exception object and handing it to the run time system is called throwing an exception.
The Three Kinds of Exceptions
1.checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. Ex:java.io.FileNotFoundException
2.error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw
java.io.IOError.
3.
Java Exception Handling – A New Appoarch
Reflection :-
This is a mechanism to get / modify properties of a class from its object at runtime.public class Demo {
Demo() {
// System.setSecurityManager(new SecurityManager());
}
public String name;
public String getName() { return name;}
private void setName(String name) { this.name = name;}
private void m() {
System.out.println("BayaReddy");
}
}
public class Test {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Demo demo = new Demo();
Class cls = demo.getClass();
AnnotatedType[] anTypes = cls.getAnnotatedInterfaces();
System.out.println(anTypes.length);
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
// demo.setName(""); //error : The method setName(String) from the type Demo is not visible
Method method = cls.getDeclaredMethod("setName", String.class);
method.setAccessible(true);
method.invoke(demo, "baya");
System.out.println(demo.getName());
Method method1 = cls.getDeclaredMethod("m");
method1.setAccessible(true);
method1.invoke(demo);
}
}
Accessing private members outside the class can be very easily a security threat for a java application. Can we restrict it???
is a big question. The answer is YES, using Security Manager.
We have to run the application using java security manager.
Now if we want to restrict the access of private members of class in Reflection,
we need to use the below code inside the constructor of PrivateMember class,
System.setSecurityManager(new SecurityManager());
Now what new SecurityManager() does is, setting the default java security as the application SecurityManager.
By default java security restricts access of private members using Reflection. Default java security is defined in java.policy.
The java policy file can be found inside ${JRE_HOME}/lib/security/.
We can also modify the java policy file. For example if we want to allow Relection to access private members,
we need to add a permission in the java policy file.
grant {
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
};
Same Hashcode for two different strings:
- "FB" and "Ea"
Abstraction :
hiding internal implementation, highlighting set of selected functionality
archive : interfaces & abstract class
Comments
Post a Comment