Java Syntax and Conventions Flashcards

1
Q

What are the primitives in Java?

A
  • char: a single character.
  • byte: an 8-bit number (from -127 to 127).
  • short: a 16-bit signed integer (it has a maximum value of 2^15 - 1 = 32,767).
  • int: a 32-bit signed integer (it has a maximum value of 2 ^ 31 - 1).
  • long: a 64-bit signed integer (it has a maximum value of 2 ^ 63 - 1).
  • float: a 32-bit floating point number (this is a non-precise value that is used for things like simulations).
  • double: like float but with 64 bits.
  • boolean: it has only two possible values (true or false - much like a bit).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can arrays be defined in Java?

A

In Java, we can define arrays of primitives or classes.

For example String[] strArray = {“a”, “b”, “c”}; creates an array of three strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can we change the length of an array in Java after it has been defined?

A

No. If we need a list of expanding size, we can instead use java.util.ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you define a new class in Java?

A

To define a new class in Java, you create a new file called Classname.java and define the class within that file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a property in a class?

A

A property is a value associated with a particular object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a method in a class?

A

A method is a block of code in a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you make a comment in Java?

A

// For a single-line comment
/* */ For a multi-line comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are generics (added in Java 5)?

A

Generics are a feature in Java that allows types (classes and interfaces) to be parameterized over other types.

They are most commonly used to specify what type a collection holds. This reduces the need for casting and improves type safety.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are annotations (added in Java 5)?

A

Annotations in Java are a form of metadata that can be added to code elements such as classes, methods, fields, and parameters. It provides additional information about the code to the compiler, runtime environment, or other tools.

They start with the “@” symbol followed by the annotation name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are concise for loops (added in Java 5)?

A

Concise for loops can be written for an array or any class that implements Iterable.

For example:
~~~
import static java.lang.System.out;
class main {
public static void main(String[] args)
{
String[] strArray = {“a”,”b”,”c”};
for (String str: strArray) {
out.println(str);
}
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are static imports (added in Java 5)?

A

In Java, we can use the words import static to import a static member of another class. This can help our code be more concise. Example:

import static java.lang.System.out;

However, the creators of Java recommend that we use static import very sparringly, so don’t get carried away.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is autoboxing (added in Java 5)?

A

Autoboxing is the automatic conversion of primitive types into their corresponding wrapper classes (e.g. int to Integer, double to Double).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are enums (added in Java 5)?

A

Enums (short for enumerations) are special data types in Java that represent a fixed set of constants.

It allows you to define a set of named values, making code more readable, maintainable, and type-safe.

Example:
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}

// main
Day today = Day.WEDNESDAY;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are varargs (added in Java 5)?

A

Varags (short for variable arguments) is a feature that allows a method to accept a variable number of arguments of the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly