Java Basics Flashcards
Learn some basic Java info like what year/who created Java
Who owns Java?
Oracle
What two programming languages are most similar to Java?
C++ and C#
What type of programming language is Java?
Ex. Logic programming, scripting programming
An object-oriented language
Is Java a proprietary, closed source, open source, or free software?
Open source
What year was Java created?
1995
Every line of code that runs in Java must be inside a ___?
class
Should a class always start with an upper case or lower case letter?
Upper case
Would Java read these as being the same? “MyClass” and “myclass”
No, Java is case sensitive
What’s a statement in Java?
A statement is a complete instruction terminated by a semicolon.
Ex.
“x = 5 + 5;” is a statement
What’s an expression in Java?
An expression is a combination of operators, literals, method calls, constants, and variables. It does not include data types or semicolons. Expressions are part of statements.
Ex.
Double “x = 5 +5”;
System.out.println(““this is an expression””)
What can expressions can do?
Produce a value: int “x = 1+1”;
Assign a variable: int “v = 10” ;
Produce no result but might have a side effect. Side effects occur when an expression changes the value of any of its operands. Ex.
int “product = a * b” ;
The only variable changed in this expression is product, a and b are not changed. This is called a side effect
What are the three types of Java statements?
Expression statements
Declaration statements
Control-flow statements
What do declaration statements do?
They declare variables ex. x = 5;
What do control-flow statements do?
They determine the order that statements are executed
Can variables created in a code block be used outside of the code block?
No, variables created in a code black can only be used in the same code block
Can you call a method above were it is defined?
Yes, unlike variables you can call methods before the method appears in your code
What’s a code block?
All code inside { }
In IntelliJ, what does a light grey method mean?
That it is not being used
What’s a parameter?
The values that are apart of a functions header Ex. //A & B are parameters public static int Example (int a, int b) { return a + b; }
What’s an argument in Java?
When a function is called, the values that are passed in the call are arguments. Also called actual parameters.
Ex.
public static void main (String[ ] args) {
int x = 2; int y = 5; //X & Y are the arguments int sum = multiply (x, y);
Can you put a method into a variable?
Yes
Should methods be created outside, or inside of the main method?
Outside. You then can call methods inside of the main method that are created outside of the main method.
In what area should you call methods?
Inside the main method
In the code, what is some unnecessary code that can be deleted?
if (score >= 1000) { return 1; } else if (score >= 500 && score < 1000) { return 2; } else { return 3; }
” && score < 1000”
“ else {“
return 3;
Resin being is the second if condition of “score < 1000” will always be true.
And
Reason for removing “else {“ is that in if statements, the last line of code will run if all statements above are false.
What’s method overloading?
When you create two methods with the same name (with different parameters)
What is concatenation?
When you use the + symbol to add strings together
What’s a char?
A single character ex. “F”
Can you declare (create) a constant / static variable inside a method?
No it must be declared outside of a method
Which of these formats should a constant be declared? A. CONSTANT_VARIABLE B. Constant_Variable C. constant_variable D. constantVariable
A. CONSTANT_VARIABLE
This is pretty much the universal way of writing a constant. You can technically write it however you want though, but it’d be a bit like wRItInG lIKe tHiS.
What 4 primitive types can be used with switch statements?
byte, short, char, and int
Do you define a char with ‘ ‘ or “ “?
’ ‘ why? I don’t know but characters are defined with “ “
What can you do to make a value of “24.0048630” print a double with only two decimal numbers?
String.format(“%.2f”, double value)
What is this new line of code doing? What are three things it can be called?
House blueHouse = new House (“blue”);
It’s creating a new variable called ‘blueHouse’ , which is also a new instance of the ‘House’ class. The code is also called a reference meaning it’s referencing to the House class. So that code can be called a new variable, instance, or reference. Also it assigns blue to blueHousre