Java 1 Flashcards
Method Overloading
- Used for reusability and readability
- Same name with different parameters
- In this way you can have the same name of method do different and specific tasks and return different values
Parts of Method:
- Name
- Parameters
- Returns
What does continue keyword perform ?
In a loop if you put continue it will start the iterator again from the next index
Word.length
length starts at 1 not 0 index.
Lawn has a length of 4
The index does start at 0
5/2
2
An ArrayList collection ..
Can only store objects and not primitives
Name Standards
packages: always lowercase
ClassNames: start with capital and then do camel case
methods: start with lower case and then camelCase
CONSTANTS: ARE ALL CAPS
What is Stack Memory and what is stored
- primitive values or the object variables
Ex: Car toyota
What is HEAP memory and how is it used ?
- HEAP is larger than STACK
- String Pool is inside HEAP
- Objects are placed into HEAP
Ex: new Toyota
What is String pool ?
Explain what word.toUpperCase does to String value?
Why is a String immutable ?
Special space in HEAP java memory where String objects are places and reused.
- If a string is already exists in String pool java will reuse it. Ex: if “java word” already exists and you do String value = “java word”, the word will not be created in String pool it will simple let value variable name point to “java word”
- To always create a new String Object even if it already exists in the String pool do:
- String word = new String(“New Java Word”)
- it will create a new string object in HEAP outside the String pool
String name = null
- since it is null does not point to any object in Heap String pool
- the name is just sitting in stack
wordNew = word.toUpperCase
- Will go to HEAP and it will point to the uppercase version of the word without changing the word itself. Therefore it is immutable
String word 2 = “hello” + “world”;
- String pool will create hello, world and then hello world
- word2 will point to hello world and the other hello and word will go to garbage collector
String builder vs String Buffer
Both allow us to use other methods to manipulate strings
However, they have different implementations
String input = “Java is good”;
StringBuilding word1 = new StringBuilder(); word1.append(input)
StringBuffer word2 = new StringBuffer(input);
Now you can do word1.reverse();
Encapsulation Principles?
- Used for data hiding/ information hiding
- Make instance variables Private and provide public getter and setter methods to have access
- Private variables can only be accessed/changed in the same class.
Encapsulation Benefits?
- Protects instance variables from invalid values
- We can use java code conditions in setter methods to provide criteria on how a value can be assigned. If criteria is not being met, then value is not assigned.
- Customize getter methods to return data in certain ways
- toString method is a special method that exists in each and every object
In Encapsulation what is immutable class ?
What is Write-only class?
- We only provide getters and no setters
- This means that the constructor must have the “this name = name” to store values
Ex: Person person = new Person(“Johnny”, 25);
person.getName();
person.getAge();
- This means that the constructor must have the “this name = name” to store values
Write only object? Provide only setters and no getters
Constructors facts:
- Must have for every class, if not provide then java will provide a no arg constructor when the class is executed at run time.
- No return type
- no-arg constructor is known as default constructor
- Constructor is called every time we use the “new” keyword
- If you have various constructor with different args, the constructor that will be used will depend on which parameters are being sent.
Person person1 = new Person("Johnny", 20); Person person1 = new Person("Johnny"); Person person1 = new Person(20); Person person1 = new Person();
What is composition ?
When a class has instance variables of type of another class - HAS-A relationship
Bus:
Properties: Driver, Engine
Bus HAS-A Driver
Bus HAS-A Engine