Most important Flashcards
What is a constructor
A block of code that is executed when a class is instantiated.
By default if you create a class and don’t specify a constructor the compiler will generate one for you. The one generated is called the default ________
no-arg constructor
What is a method in Java?
A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.
In a Java method, what is the difference between a parameter & an argument?
Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type & order.
What is Method Overloading in Java?
If a class has multiple methods having the same name but different in parameters, it is known as Method overloading.
What are the different ways to overload a method in Java?
- By changing number of arguments
2. By changing the data type
What is State in Java?
State is a behavioral design pattern that allows an object to change the behavior when its internal state changes.
What is the Object Class in Java?
The object class is the parent class of all classes defined in java even if you do not explicitly extend it.
What is Polymorphism?
What is it’s most common use in OOP?
The ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
What is a reference variable in Java?
A link to an instance of an object in memory
What is an API?
Short for “Application Programming Interface”, it is a library of functions you can use to interact with a program or component.
What is the java.lang package?
The default package, it is implicitly imported into all java classes, so it requires no declaration. Many useful classes come from this package including system and string.
The string class in Java is an “immutable” class, what does this mean?
It can not be changed
What is special about the string class in Java?
- It can be instantiated in two different ways unlike all other classes, (with or without the new keyword)
- String objects are often recycled by the JVM instead of creating new ones in what is called the “String Pool” Which saves memory.
What is the difference between equals() & ==?
The operator == is used to compare the value of primitives ex.
int x = 6;
int y = 5;
if (x ==6) { .. }
If you compare two reference variables using the == operator will compare the reference address of the objects each variable points to. It may not return true even if the contents of those two objects are the same.
This is why we use the equals() method with objects. It is expected to that it will help us compare the values of objects with one another ex. String s = "Hello"; String s2 = new String("Hello"); if (s.equals(s2)) {..} This is because the equals() method of the String class has been overridden to compare the contents of the current String and one passed in as an argument.