-
compiler
translates our English into 0s and 1s for the processor to understand
-
source code
Human written code
-
byte code
a class file, which contains 0s and 1s also called machine code
-
case-sensitive language
Capital letters and lowercase letters are interpreted differently
-
public class
The start of every Java program and contains the code: public class {}
-
main method
public static void main(String[ ] args) {}
-
Code to print to system?
System.out.println(“text”);
-
memory
What a computer uses to store information it needs
-
variable
is placeholder for data in a computer program
-
identifier
The name of a variable
-
keyword/reserved word
Words that are set aside for use in the Java language
-
int
Whole Numbers (5, 10, 100,-50)
-
double
Decimal Numbers (5.5,10.0583)
-
char
A single Character (‘A’,‘b’, ‘&’)
-
-
-
initialization of the variable
Giving a variable an initial value
-
-
How does Java compute concerning the equal sign?
The right side of the equal sign is computed first.
-
5 % 2 calculates what?
The remainder so the answer would be 1.
-
Syntax error
grammatical problem with the source code.
-
Runtime error
when your source code compiles, but when you execute it an error occurs
-
Logic error
is when the code has correct syntax, causes no errors during execution, but it produces undesired results
-
Comments
are sections of our source code that are ignored by the compiler
-
Single Line Comments
// comment
-
Area Comments
/* comment */
-
-
All _____ files start with public class and the name of the class?
Java
-
The ______ tells Java where your program should begin execution?
main method
-
promotion
When we divide two numbers where one is an int and the other is a double, the integer value is temporarily changed into a double for the operation.
-
casting
- When we want to force Java to temporarily change a value’s type, we can cast the type into something else.
- (double) 5 // Casts 5 into 5.0
-
constant
is a variable whose value can not be changed. Any attempt to do so will cause a syntax error.
-
how do we declare a variable as a constant?
- we use the keyword final before the declaration
- final double PI = 3.14;
-
methods
is a group of code that performs a single task
-
How does a simple method begin?
public static void ( ) {}
-
calling or invoking a method means what?
New methods you write must be told to run from the main method which is invoking or calling the method.
-
parameters
When a method needs information to complete its task, it accepts that information through the use of parameters
-
give an example of a parameter
public static void method(int x)
-
argument
The value you provide in the parenthesis gets stored into the parameter in the method. The value you provide is known as an argument
-
promotion with integers concerning doubles
Java will “promote” an integer value into a double value if an integer is given where a double is expected
-
local variable
Any variable declared inside a method only exists inside that method
-
A variable declared inside
a method is known as one that is ______ to
that method ?
local
-
class variables and instance variables
kinds of variables that may be used across every method
-
The Basic Method
public static void method_name(){}
-
Methods can send back a value to the location in which the method was called, this is called what?
returning a value
-
We write void in the method what does it mean?
void is written when the method does not return a value
-
If we want a method to return a value, we replace the word void with the data type that will be returned. What is this called?
This portion of the method signature is called the method’s return type
-
The Keyword return
To return a value, we use the keyword return, followed by the value to be returned.
-
tags
contained in a Javadoc and begins with @ and followed by author or version
-
How would you give someone information about your code without giving them source code?
javadoc
-
A example method signature is what?
public static (params) {….
-
public static <return_type> <method_name>(params) {} , define the bold terms
- Where return_type: is the type of value that the method returns. If nothing is returned, we use the keyword void
- method_name: is the name of your method
- params: are all the parameters the method uses to accept value
-
How do you call a method from main ?
- we simply write the method name followed by parenthesis
- public static void main(String[ ] args){methodName();}
-
How can call a method from
inside another method ?
- It is no different than calling a method from within the main method.
- public static void method1(){method2();}
-
method overloading
Have two methods in a class that have the same name as long as they accept a unique set of parameters, which allows a user to call a method to calculate the area of a circle and specify their own precision for PI, but also allow them to call it and use a default value of 3.14
-
When calling overloaded methods if two methods in a class share the same name, the first method has one parameter, the second method has two parameters and we call a the method with only one parameter of type double, which one will excute first and why?
The method with only one parameter will execute because in method overloading the set of arguments we give determines which method gets called.
-
Local Variables
Variables declared inside a method are local to that method. They do not exist in any other method.
-
Class variables
are variables that can be used in all methods of the class they are declared and is declared inside the class, but not inside a method
-
How would you declare a class variable of x = 10 ?
Immediately following the class line of code you would write: public static int x = 10;
-
How does Java determine the order for variable use when two variables share the same name ?
- First look for a local variable with the name we specified, and if it finds it, it will assume we are talking about the local variable
- If it does not find a local variable, it then looks for a class variable with the name we specified and assume we want to use it
- If it finds neither; then there is no variable with that name, and Java gives a syntax error
-
If we have a local and class variable with the same name, Java will use the local variable. What if we actually want Java to use the class variable ?
We can tell Java to use the class variable by writing the name of the class first, followed by a period, followed by the variable name: Class_name.variable_name
-
How do you reuse code ?
All you have to do is write the name of the class that contains the method or class variables that you want to access, and then write a period and the name of what you want to access: Class_Name.method_name() Class_Name.class_variable
-
Rules of reusing code are ?
- The class you want to use code from must be in the same directory as the class you are currently writing in order to compile
- One exception is that you can also compile by setting your class path to know the location of the external code you wish to use
-
What is each class consist of ?
class variables and methods
-
When two classes work together what do they do ?
they make use of each other’s methods and variables to complete their individual tasks
-
What happens when a certain class contains a method or a variable that it does not want an outside class to have direct access to?
The method or variable should be scoped as private
-
Scope
refers to the context in which something is visible in programming
-
Visibility
refers to whether or not something in our program can be accessed from a given context
-
public scope
is the scope we give a class, method, or variable when we want it to visible and thus accessible by all classes everywhere
-
private scope
We can keep outside classes from being able to see or access methods and variables in a class by giving them the
-
protected scope
The scope allows the given class, method, or variable to only be visible to other classes within the same package as the class they are declared in
-
package
In Java, we have the ability to group a set of classes together
-
What happens if we forget to specify a scope before a method or class variable ?
Java, by default, gives classes, methods, and variables a default scope of protected
-
static variables and methods
belongs only to the class they are associated with
-
An instance of a class
The variables used when a class is called without being specified static
-
instance variables
variables that no longer belong to the class as objects
-
An object
a specific instance of a class
-
How do you decalre an object ?
To declare an object, we write the name of the class we are instantiating, then a variable name for the object, followed by a semicolon: Person myself;
-
How do you create an object ?
To create an object, we set the variable name equal to the keyword new, followed by the name of the class again with parenthesis after it: Person myself = new Person();
-
How do you use an object ?
we can use its instance variables the same way we used to use class variables, with the difference being we specify the object name, and not the class name
Person myself = new Person(); myself.name = “Dan Longo”; myself.age = 21;
-
What is Boolean logic ?
logic that evaluates to either true or false
-
What is a boolean equation ?
two values with a boolean operator in between
-
-
-
-
-
greater than or equal to
> =
-
less than or equal to
< =
-
What is an if statement ?
When we only want a part of our code to be executed under a certain condition
-
What is the syntax for an if statement ?
if ( <condition> ) {// code to be executed}
-
When is an if statement executed ?
The only time the code in the {} is executed is when the conditon is true.
-
How would you declare a primitive variable type boolean ?
boolean <variable> = true;
-
Set a boolean variable equal to a boolean equation
boolean x = 8 > 5; // x is equal to true
-
Once a boolean variable has a value, it can then be used by itself as the condition for an if statement
- boolean x = 8 > 5;
- if( x )
-
Primitive type boolean used as a parameter type
public static void example(boolean x)
-
Primitive type boolean used as a return type
- public static boolean isLarger(int x, int y)
- if(x > y)
- {
- return true;
- }
- return
- false;
-
Express how else works in if statements
- if(d != 0)
- {
- denominator = d;
- }
- else
- {
- System.out.println(“You entered 0”);
- }
-
nested if statements
- if(x > 50)
- {
- if(y < 20)
- {
- System.out.println(“yes”);
- }
- }
- else
- {
- System.out.println(“no”);
- }
-
Can you have an if inside an else ?
- int x = 5;
- if(x == 10)
- {
- System.out.println(“X is 10”);
- }
- else
- {
- if(x == 5)
- {
- System.out.println(“X was 5, not 10”);
- }
- }
-
multiple conditions applied in a single if statement
and boolean operator: if( x > 10 && x< 20)
-
When using the and boolean operator what conditions must be true
False AND False = False
False AND True = False
True AND False = False
True AND True = True
-
short circuiting
When the && operator is used, and the first condition evaluates to false, Java does not even look at the second condition:
if( 5 > 10 && 6 == 6)….
-
The boolean or operator
- if(x >5 || x < 0)
- {
- // only one of the above need to be
- // true for this to execute
- System.out.println(“yes”);
- }
-
When using the XOR boolean operator what conditions must be true
true^ true – evaluates to FALSE
true^ false – evaluates to TRUE
false^ true – evaluates to TRUE
false^ false – evaluates to FALSE
-
When using the or boolean operator what conditions must be true
False or False = False
True or False = True
False or True = True
True or True = True
-
Exclusive OR operator written as XOR
^
-
The NOT Operator
InJava, the NOT operator is represented by the ! symbol, placed directly before a boolean equation
boolean tf = false;
- if(!tf)
- {
- System.out.println(“TRUE!”);
- }
-
switch statement
- A switch statement is a structure that looks at the value of a variable and goes through a list of possible values
- Based on the value and the list of possible cases, it will jump to the part of your code where that value is in the list
-
The possible values in a switch statement list are called ...
cases
-
switch statement syntax
switch(month)
{
case 1:
case 2:
}
-
A value for the case variable that is not in the domain of a switch statement
default:
- switch(month)
- {
- case 1: date = date + 0;
- break;
- case 2: date = date + 3;
- break;
- default: System.out.println(“no match”);
- break;
- }
-
in a switch statement's case list should have this included
break; The keyword break tells Java to immediately exit the switch statement
-
if/else and switch statement
- A switch statement can be
- rewritten as an if/else structure
- An if/else structure (for this specific
- scenario) can be rewritten as a switch statement
-
List the cases when converting an if/else to a switch
When converting an if/else to a switch, each of your if statements would be a case, ending with a break
If you have an if statement that uses the || (or) operator, you would have two cases together without a break in the middle. There would be a break after both the cases.
&& (and) can not be applied here since a switch only looks at a single variable, which has a single value
-
give an example of converting an if/else to a switch
- if/else:
- if(month == 1)
- {
- date = date + 0;
- }
- if(month == 4 || month == 7)
- {
- date = date + 5;
- }
- switch:
- switch(month)
- {
- case 1: date = date + 0;
- break;
- case 4:
- case 7: date = date + 5;
- break;
- }
|
|