Comp sci: Overloading

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

20 Terms

1
New cards

Overloading

When two or more methods in the same class have the same method name. To be valid, any 2 definitions of the method name must have different signatures. A signature consists of the name of a method together with its parameter list. Differing signatures must have different numbers and/or types of parameters.

2
New cards

Overloading and automatic type conversion

If Java cannot find a method signature that exactly matches a method invocation, it will try to use automatic type conversion.

3
New cards

No argument constructor

If you do not include any constructors in your class, Java will automatically create a default or no-argument constructor that takes no arguments, performs no initializations, but allows the object to be created.

4
New cards

Overloading based on the type returned.

Java does not permit methods with the same name and different return types in the same class.

5
New cards

Default variable initializations

Instance variables are automatically initialized in Java. Boolean types are initialized to false, other primitives are initialized to the zero of their type. Class types are initialized to null.

6
New cards

Lists

Java has various forms of lists. Example of an ArrayList that stores strings is:
ArrayList<String> strlist = new ArrayList<String>();

7
New cards

Wrapper types

The process of converting from a primitive to an object is called “boxing“, the reverse process is called “unboxing“. In Java 5, the compiler converts back and forth itself and is known as autoboxing.

8
New cards

Array

Arrays in Java have some of the features of Python lists, including indexing, but unlike lists, their number of elements is fixed at creation time and is unchangeable after that.

9
New cards

Switch statement

It implements multiway branching. When a switch statement is evaluated, one of a number of different branches is executed. The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch. The controlling expression must evaluate to a char, int, short or byte. Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements. Each case label must be of the same type as the controlling expression. There can also be a section labeled default. It is optional and is usually last.

10
New cards

Example of a switch statement

knowt flashcard image
11
New cards

Equals vs ==

The == operator checks for reference equality, meaning it only returns true if both references point to the same memory location. The .equals() method is designed to check for value equality and is more appropriate for comparing the content of objects.

12
New cards

Example of the equals method in a class

knowt flashcard image
13
New cards

Iterate over a list

ArrayList lst = new ArrayList(); lst.add(1); lst.add(1); lst.add(2); lst.add(3);
for (Integer i : lst) {
System.out.println(i)
}

14
New cards

Iterate over an array

int l[] = {1,1,2,3};
for(int i : l) {
System.out.println(i);
}

15
New cards

Boolean expressions

When 2 Boolean expressions are combined using ‘and‘ operator, the entire expression is true provided both expressions are true. When 2 expressions are combined using ‘or‘ operator, the entire expression is true as long as one of the expressions is true.

16
New cards

Short circuit evaluation

Java can take a shortcut when the evaluation of the first part of a Boolean expression produces a result that evaluation of the second part cannot change.

17
New cards

A do while statement

Is used to execute a portion of code and then repeat it based on the evaluation of a Boolean expression. The Boolean expression is checked after each iteration of the loop body
▪ If true, the loop body is executed again
▪ If false, the loop statement ends

18
New cards

Example of a do while statement

int a, b, c;
do {
System.out.println(“Enter 3 sides for a triangle.");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
if (a*a + b*b != c*c) {
System.out.println("Not a right triangle: Try again!");
}

} while (a*a + b*b != c*c);

19
New cards

Break statement

When a break statement is executed, it ends the nearest enclosing switch or loop statement.

20
New cards

Continue statement

When executed, the continue statement ends the current loop body iteration of the nearest enclosing loop statement ▪ Note that in a for loop, the continue statement transfers control to the update expression
When loop statements are nested, remember that any break or continue statement applies to the innermost, containing loop statement.