Java: Basic Concepts Flashcards
What happens in java when we write 7/10? Why?
7/10 in java will return zero. If we want to divide 7 by 10, then we must write 7.0/10 (which will return 0.7
Reason: 7/10 presents the problem as involving 2 integers. Java returns an integer because you asked for an integer. It runs the calculation, sees where the decimal is, and truncates the decimal. If you want it to return a float, you must use a float.
What is casting?
Typecasting is when one object reference can be type cast into another object reference.
Java has two casting scenarios. What are they?
Upcasting and downcasting
What is upcasting?
.Upcasting is when we cast an instance of a subclass to a variable of a superclass. Remember, an instance of a subclass is ALWAYS an instance of its superclass.
What is an implicit cast?
Instead of writing:
Object o = new Student(); m(o);
We can use an implicit cast:
m(new Student());
This works because an instance of Student is an instance of Object.
What is an explicit cast?
Sometimes we must explicitly tell the compiler that an object of its own class is in fact an object of its class.
Student b = o; [does not work]
Student b = (Student)o; [works]
We use this to downcast correctly.
What is downcasting, and why do we do it?
A downcast is when we cast an instance of a superclass to a variable of its subclass. We must use explicit casting to confirm our intention to the compiler with the (SubclassName) cast notation.
First, make sure the object to be casted is an instance of the subclass. use instanceof
Good analogy, pg 426. Fruit is a superclass. Apple and Orange are subclasses. We can easily assign an instance of Apple to a variable for Fruit. But because a fruit is not necessarily an apple, we must explicitly assign an instance of Fruit to a variable of Apple.
myObject.getDiamter [doesn’t work because the Object class doesn’t have getDiameter]
((Circle)myObject.getDiameter()); [DOES work because we told the compiler that the variable is an instance of a superclass, in this case, Circle]
What is a unary operator? Give example
A unary operation is an operation with only one operand. A negative number’s -x for instance
How does preincrement work, and why do we do it?
++var
Increments a variable and then returns that variable. (does not return the variable and then increment, as postincrement does)
If a variable does nothing but increment, then ++var and var++ are identical.
BUT
i = 1; int j = ++i; // j is 2, i is 2 [the new var is used in the statement]
i = 1 int j = i++; // j is 1, i is 2 [ the original var value is used in the statement]
Increments j by 1, then returns j
(whereas postincrement returns j, then increments j by 1)
How does predecrement work, and why do we do it?
–var
Decrements a variable and then returns that variable. (does not return the variable and then increment, as postdecrement does)
If a variable does nothing but decrement, then –var and var– are identical.
BUT
i = 1; int j = --i; // j is 0, i is 0 [the new var is used in the statement]
i = 1 int j = i++; // j is 1, i is 0 [ the original var value is used in the statement]
Decrements j by 1, then returns j
(whereas postdecrement returns j, then increments j by 1)
What is a literal?
A literal is the value assigned to a variable.
double weight = 0.305;
the literal is 0.305
What are augmented assignment operators?
+= -= *= /= %=
example:
i += 8
i = i + 8
these are the same
Describe the char data type
a single 16-bit unicode character
Describe what a string is
A string is an object from the string class.
A string is immutable
A string is “changed” through methods that actually make a new string object
What does \t do
inserts a tab in the text at this point
what does \b do
inserts a backspace in the text at this point
what does \n do
inserts a newline in the text at this point
what does \r do
inserts a carriage return in the text at this point
what does \f do
inserts a formfeed in the text at this point
what does ' do
inserts a single quote character in the text at this point
what does " do
inserts a double quote character in the text at this point
what does \ do
insert a backslash character in the text at this point
what does next() do
Part of Scanner class. Finds and returns the next complete token from this scanner. whitespace is the default delimiter.
this is called a token reading method
what does nextInt(), nextFloat(), etc do
Scans the next token of the input as an int, double, etc (read the Scanner class for more)
this is called a token reading method
Say we want a list of random numbers, 1-50. how do we do it?
We use java’s Random class. Remember, if you write:
Random rand = new Random(); int pickedNumber = rand.nextInt(40);
This will pick a number 0-39
To get 1-40:
int pickedNumber = rand.nextInt(40) + 1;
Basically, add the number of the range that you need. 5-35 needs:
int pickedNumber = rand.nextInt(31) + 5;
what is an off by one error?
an off by one error is when a loop iterates one time too many or too few.
mistake is usually made when using a “is less than or requal to” where “is less than” should have been used in a comparison or fails to take into account that a sequence starts at zero rather than one (as with array indicies in many languages)
In what situations is a for loop best?
a for loop is best when we know how many repetitions we need.
pretest loop
is what situations is a while loop best?
a while loop is best when we don’t have a fixed number of repetitions
pretest loop
is what situations is a do-while loop best?
a do-while can replace a while if the loop body has to be executed before the continuation condition is tested.
posttest loop
use this if code must execute at least one time
what is a sentinel value
a special value whose presence guarantees termination of a loop that processes structured (especially sequential) data.
for example, a user may enter “0” to signal to the computer that he or she is done entering data
what is input and output redirection?
if we have a lot of data to enter, it can be a pain to do it from a keyboard. we can store the data in a textfile and separate it with whitespace.
in input redirection the program takes the input from the text file rather than asking the user to type in the data at runtime from a keyboard.
output redirection writes output to a file rather than display it on the console
describe the keywords break and continue
break breaks out of a loop
continue breaks out of an iteration
int sum = 0; int number = 0; while (number < 20) { number++; if (number == 10 | | number == 11) continue; sum += number; }
System.out.println(“The sum is “ + sum);
this program sums all the numbers except 10 and 11. the continue escapes that iteration and then continues
what is “the stack”?
A call stack has several purposes. Mainly, it is for keeping track of the point to which each active subroutine should return control when it finishes executing.
what happens with the stack when a method is invoked?
the system creates an “activation record” (also called an “activation frame”).
Stores parameters and variables for the method and places the activation record on the stack.
what happens with the stack when a method’s work is finished?
when the work is finished and the data returns to its called, its activation record is removed from the call stack
how does the stack store activation records?
last in, first out. the activation record for the method that is invoked last is removed first from the call stack
m1 calls m2 and m3
m1 is pushed onto the stack first, then m2 and m3.
m3 is finished, activation record removed
m2 is finished, activation record removed
m1 is finished, activation record removed
Describe a void method
The void return type does work for us, but doesn’t return a value to us.
can a void method use “return”?
yes, we can use “return” in a void method if we simple want to pop a frame from the call stack and return control to the line following the function call
what is a static method
a static method belongs to the class, and not to the object(instance)
a static method can only access static data (cannot access instance variables)
a static method can call only other static methods. cannot call a non-static method
a static method can be accessed directly by the class name. doesnt need an object
a static method cannot refer to “this” or “super” keywords
Syntax: .
Integer.toHexString(whatever);
This is a static method from the Integer class
*** There’s only one copy of this no matter how many objects we’ve created (or even if we haven’t created any objects yet) because it belong to the class. it does not belong to the instances of the class we create.
**Note: a static method can be called without creating an instance of the class
what is an static variable
a static variable belongs to the class, not to the object
a static variable is initialized ONCE, at the start of execution. initialized before instance variables.
a static variable is a single copy to be shared by all instances of the class
a static variable can be accessed directly by the class name and doesn’t need any object
Syntax: .
Basically, you would create this in a class as a data field. for a bike class, if you wanted to keep track of how many objects of that class have been created, you would do:
private static int numberOfBikes = 0;
to reference it (to increment it, in this instance):
Bike.numbeOfBikes
*** Use this when you want all the instances of a class to share data. Static variables store values for the variables in a common memory location. Because of this common location, if one object changes the value of a static variable, all objects of the same class are affected.
pass by value
the method copies the value of an argument into the formal parameter of the subroutine.
as for people who say java passes objects by reference, this is not true. There is no difference between passing primitive data types and objects when talking about method arguments. You always pass a copy (not the object, and not the reference itself) of the bits of the value of the reference
if primitive, these bits will contain the value of the primitive data type itself
if object, the bits will contain the value of the address that tells the JVM how to get to the object
pass by reference
java doesn’t pass by reference. java passes the value of the reference and not the reference itself (and not the object). see pass by value
name 3 advantages to modularizing code
- methods and such can be used by other programs
- errors are confined to particular methods, easier to debug
- isolating methods makes the logic easier to follow, easier to read
Break down what this means:
System.out.println(“whatever);
the dot operator connects classes and objects to members.
System is the name of a class. out is one of that class’s static fields. println is a method in that class. So System.out.println(); invokes the println() method of the System.out object.
Said another way: The “System” object is actually a class from which you are accessing the static object out. Then you access the println method on that object.
explain the dot operator in java
the dot operator means that you are accessing a member (a variable or a method) of an object.
page 304 in textbook, but it’s a bit crappy..