Java Flashcards

1
Q

public static void main(String[] args)

A

“public” is the access modifier which specifies from where and “who” can access the program
“static” makes it so that the class can be invoked without creating a new instance (creating a new object)
“void” specifies a return type, in this case since it is void, this method returns nothing
“main” this is not a keyword, instead the JVM (Java Virtual Machine) looks for this as the start of the program
“String[] args” this is the argument or parameter that the method accepts. It is and array of strings called “args”. These are called Java command-line arguments

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

Data Type: Strings (not really a data type)

A

Stores a series of characters, words and sentences.

Takes up different amounts of memory depending on the size of the string.

Defined using double quotation marks.

Technically this isn’t a variable type, but it sort of is, so we will discuss it here.

Note that it starts with a capital letter.

Ex: “Eric”, “Shairose Holborn”, “Hello there”, “Computer programming is fun!”, “10432 McLeod Street”, “K3N 4H9”.

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

Data Type: int

A

Stores positive or negative numbers without decimal places.

Stores values between -2,147,438,648 and 2,147,483,647.

Takes up 32 bits of memory or 4 bytes.

Ex: 45, -234, 0, 65323, -3454

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

Data Type: double

A

Stores positive or negative values with decimal places.

Can store immensely huge numbers.

Can store values accurately up to 15 decimal places.

Takes up 64 bits of memory.

Ex: 12.5, -543.7645, 0, 5465.876, -8722.987999

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

Data Type: Char

A

Stores a single character.

Takes up 16 bits of memory.

Is defined using single quotation marks.

Ex: ‘A’, ‘v’, ‘X’, ‘d’, ‘-‘, ‘%’, ‘?’, ‘.’.

Note that chars are defined in between single quotes

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

Data Type: Boolean

A

Stores either true or false.

Takes up one single bit of memory.

Ex: true, false… that’s it; there are only those two possible values.

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

Why are Strings not a primitive data type?

A
Strings have many methods that are used to manipulate them, thus they were made into a class and not into a primitive data type. 
→ Strings can have a value of null. 
→ Primitive types start with lowercase while non-primitive types start with an uppercase
→ Primitive types have different sizes while non-primitive types have the same size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Data Type: Float

A

Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

Uses 4 bytes

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

Declaring Variables

A

To declare a variable you need a type followed by a name. For example: | double number; |.
→ Make sure to end with semi-colon
→ You can assign a value later | number = 10; | or when you declare the variable you can assign the value | double number = 10; |

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

Why is Java considered WORA

A

Java uses something called a JVM (Java Virtual Machine: interpreter) along with a compiler to run Java programs on any platform.

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

Escape Characters

A

The backslash character (“") is also known has the escape character. It is used to declare special characters or new lines. Just put the backslash before the character you need.

Quotations
System.out.println(“ " This is how to print quotations " “);

Backslash
System.out.println(“ \”); → This is how to print backslash

Newline (“\n”)
System.out.println(“ This is how to create a new line after a string \n”);

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

Variables

A

Memory location in RAM, depending on the type there are different amounts of space taken up. Remember that you can change the values of these variables throughout your program.

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

Data Type: Short

A

The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767.

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

Data Type: Long

A

The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

This is a class not a primitive type

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

Arithmetic Division (A little feature)

A

If I divide

10 / 4 = 2 ← Java has no regard for the remainder or decimal point so it will just tell how many times the number goes into the other

However

10 / 4.0 = 2.5 ← Java now cares about the variable as the second value is a double, so the answer will be a double

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

Casting in Java

A

In Java there are two types of casting: wide and narrow

Widening Casting (Converting a smaller type to a larger type. This is done automatically)
byte → short → char → int → long → float → double
Narrowing Casting (Converting larger type to smaller type. This has to be down manually)
double → float → long → int → char → short → byte
→ Java might cut off data. Like when converting from int to byte, Java will by default round down.
17
Q

Objects and Classes

A

Classes are usually a template for objects. The object can contain methods and attributes which can be accessed with dot syntax after the class is instantiated.

18
Q

Mr. Jeg’s Naming Convention

A

→ Use Camel Case
→ int are declared by “int” at the start of a variable
→ double are declared by “dbl” at the start of a variable

19
Q

Constant in Java

A

Declared with final preceding the definition
→ use all uppercase to denote that it is a constant.

final int BOB = 2;

20
Q

Access Modifiers: Private

A

The most restrictive modifier. It limits access to methods and variables to the class in which they are declared. “private” is chosen when there is no need to use certain methods or variables outside the class

21
Q

Access Modifiers: Default

A

Allows access only from within the current package. If there is no specified access modifier, the method or variable will take on this one. Learn more about the default modifier.

22
Q

Access Modifier: Protected

A

Allows access to a method or variable only from within the current package, unless it is accessed through a child class outside of the package. Learn more about the protected modifier.

23
Q

Access Modifier: Public

A

The least restrictive modifier. It allows access to a class, method or variable not only from within the class in which it is declared, but outside as well.

24
Q

Method Overloading

A

Method overloading is similar to overriding in that it involves methods with the same name. However with overloading, a single Java class can have multiple methods with the same name if they have different parameter lists. Overloaded methods are distinguished by their number and type of parameters.

25
Q

Constructors

A

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

Example ↓

class Geek {
    // data members of the class.
    String name;
    int id;
    // Constructor would initialize data members
    // With the values of passed arguments while
    // Object of that class created
    Geek(String name, int id)
    {
        this.name = name;
        this.id = id;
    }
}
26
Q

Constructor Overloading

A

Constructor overloading is a type of method overloading in which there are multiple constructors in a class. This gives the option to instantiate an object with different sets of arguments.