Basic Java Program important points
- Any java source file can have multiple classes but they can have only one public class.
- The java source file name should be same as public class name. That’s why the file name we saved our program was
JavaHelloWorldProgram.java
- When we compile the code, it generates byte code and save it as
Class_Name.class
extension. If you look at the directory where we compiled the java file, you will notice a new file createdJavaHelloWorldProgram.class
- When we execute the class file, we don’t need to provide full file name. We need to use only the public class name.
- When we run the program using java command, it loads the class into JVM and looks for main function in the class and runs it. The main function syntax should be same as specified in the program, else it won’t run and throw exception as
Exception in thread "main" java.lang.NoSuchMethodError: main
.
----------------------------------------------------------------------------------------------------------------------------
java.lang.String:
-------------------
1. String is immutable object: always creats new object when manipulates the object. and thread safe
2. getBytes() method to encode the string into a sequence of bytes
ex: byte[] byteArr = str.getBytes("UTF-8");
3.StringConstantPool : for String literals, always checks SCP objects, that if same object content is available then it creates new reference to existing object. so reduces the memory overhead.
4. compareTo() method used to : compare two strings lexicographically.
5. substring() method: get the part of string
Object relations:
--------------------
IS-A (Inheritance) :“one object is type of another” i.e, inheritance.
Has-A (Association) : types of Association are Aggregation and Composition.
Inheritance in java :
-----------------------
1. one object acquires all the properties and behaviors of a parent object is inheritance.
2. benefit of inheritance is code reuse because subclasses inherits the variables and methods of superclass.
3. Private members of superclass are not directly accessible to subclass.
Superclass members with default access is accessible to subclass ONLY if they are in same package.
4. If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception.
5. In the subclass constructor, call to superclass constructor is mandatory in this case and it should be the first statement in the subclass constructor.
6. Java doesn’t support multiple inheritance
We can create an instance of subclass and then assign it to superclass variable, this is called upcasting.
7. When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting. We need to explicitly cast this to Subclass.
8. We can override the method of Superclass in the Subclass. However we should always annotate overridden method with @Override annotation so that Compiler knows that we are overriding a method and if something changes in the superclass method, we get to know at compile time rather than getting unwanted results at the runtime.
9. We can call the superclass methods and access superclass variables using super keyword. It comes handy when we have a same name variable/method in the subclass but we want to access the superclass variable/method. This is also used when Constructors are defined in the superclass and subclass and we have to explicitly call the superclass constructor.
10. We can use instanceof instruction to check the inheritance between objects.
11. We can’t extend Final classes in java.
If you are not going to use Superclass in the code i.e your Superclass is just a base to keep reusable code then you can keep them as Abstract class to avoid unnecessary instantiation by client classes. It will also restrict the instance creation of base class.
12. Java doesn’t support multiple inheritance in classes because it can lead to diamond problem,
Multiple inheritance in interfaces is perfectly fine because the interfaces are only declaring the methods and the actual implementation will be done by concrete classes implementing the interfaces.
static keyword:
------------------
1. Java static variable : A static variable is a class variable and doesn’t belong to Object/instance of the class. and not thread safe.(shared with all its instances).
2. Java static method : same as static variable. A static method can access only static variables of class and invoke only static methods of the class.
3. Java static block : the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.
4. Java Static Class : static keyword can be used for nested classes only. and not for top-level classes.
Collection
------------
1. in Collections Framework root interface : java.util.Collection
2. Collection interface methods: size(), add(), remove(), clear()
3. every collection class is catagorized under infaces : List, Set, Queue, Map
4.Map is the only interface not inherits from Collection interface.
No comments:
Post a Comment