JavaNotes
JavaNotes
**Class Name and File Name**: In Java, the class name and the file name must be the
same.
2. **Platform Independence**: Java is platform independent.
3. **Case Sensitivity**: Java is case sensitive. This means that `string` and `String` are
different.
4. **Object-Oriented Programming (OOP)**: Java is an Object-Oriented Programming
language.
5. **Pillars and Features**: Java has 4 pillars and 7 features.
6. **Class Requirement**: To work with a Java application, you must work with at least
a single class.
7. **Default Access Specifier**: In the default access specifier, class members can be
accessed within the same package only.
8. **Compilation and Execution**:
a. After the successful compilation of a `.java` file, a `.class` file is created.
b. The `.class` file contains bytecode, which is later converted to machine code
by the JVM (Java Virtual Machine).
9. **Object-Oriented Nature**:
c. Java is a pure object-oriented language, whereas C++ is a semi object-
oriented language.
d. Pure object-oriented means that OOP concepts must be used in every
program. In semi object-oriented languages, OOP concepts may or may not
be used in the program.
10. **Object Class**: In Java, the `Object` class is the default parent class. Therefore, any
program in Java cannot be written without inheritance.
11. **Strings in Java**:
e. In Java, a string does not have a null character at the end because Java treats
strings as objects, unlike languages like C or C++.
f. Strings are objects of the `String` class, which is an inbuilt class in Java. It has
various methods like `length()`, `charAt()`, `substring()`, etc.
12. **Scanner Class Input Methods**:
g. `String a = xyz.next();`
h. `String b = xyz.nextLine();`
The behavior observed here is due to the way the `next()` and `nextLine()` methods of the
`Scanner` class work:
i. When `next()` is used, it reads the next token and leaves the cursor at the end
of that token, right before the whitespace.
j. When `nextLine()` is called after `next()`, it reads the rest of the line from the
cursor position, which is just the newline character left by the previous
`next()` call. Therefore, `nextLine()` returns an empty string.
13. Abstract class can have abstract methods (one or many) and non-abstract
methods(one or many).
14. If abstract class contain more than one abstract method then all method must be
override where abstract class get inherit
15. Abstract class cannot create its object but can create its reference.
16. Key Points about Method Overloading
Different Parameter List: The methods must have a different number of parameters
or parameters of different types.
Return Type: The return type of the method can be the same or different, but it is not
enough to differentiate methods on its own.
Same Class: The overloaded methods must be in the same class or a subclass.
17. Can we use static keyword with object which is declared in class as a variable
Yes, you can use the static keyword with an object in Java. When you declare an object as
static, it means that the object belongs to the class rather than to instances of the class.
Here’s what happens when you use the static keyword with an object:
1. Class-Level Variable:
o The object is shared among all instances of the class.
o There is only one instance of the static object, regardless of how many
instances of the class are created.
2. Initialization:
o Static objects are initialized when the class is first loaded into memory.
o They are created and initialized before any instances of the class are created.
3. Access:
o Static objects can be accessed using the class name.
o They can also be accessed by instances, but it’s a common practice to use the
class name for clarity.
4. Memory Management:
o Static objects are stored in the static memory area (or method area), not in the
heap space where instance variables are stored.
18. Java is hybrid language in terms of using compiler and interpreter . It uses both
compiler generates byte code (.class file) and interpreter (JVM) generates machine
code and then execution is done.
19. Difference between Compiler and Interpreter
Compiler will translate the source code Interpreter translate the source code
only once every time whenever we want to execute
Compiler will generate separate machine It doesn’t create separate machine code
code file file
Compiler programs works independently Interpreter programs doesn’t work
independently.
They run faster because they work They are slower because they run inside
independently the interpreter.
20. JVM is Interpreter.
21. We can overload the main method.
22. scannerObj.next() : this method which is used to take string as input from the user will
stop reading when space occur
23. scannerObj.nextLine() : this method read spaces also and read complete line
24. Traditionally Interface contains only abstract methods but from java 8 Interfaces can
contain abstract, static, default, private methods also
25. Java 8, released in March 2014, is a significant update to the Java programming
language, introducing several major features and enhancements.
26. String is a class in java which is immutable class and if we want to use mutable string
then we have to use StringBuffer and StringBuilder classes in java
27. hashCode is not an address of any object
28. Default package of java is java.lang so we not need to import it
29. Ananymous inner class is a class without class keyword declaration and which is used
only once so it is declared by using another class object and can called by using that
object.